Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy Spring-Boot-Devtools

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to deploy Spring-Boot-Devtools". In daily operation, I believe many people have doubts about how to deploy Spring-Boot-Devtools. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to deploy Spring-Boot-Devtools". Next, please follow the editor to study!

Spring-boot-devtools

SpringBoot,StringBuilder,StringBuffer, commonly known as the three SB of the Java world. SpringBoot, in particular, is very easy to use, mainly due to its autoconfig, which relies on conventions to standardize development.

The problem is that SpringBoot loads so many Jar packages that it takes a long time to start. For SpringBoot services, spring-boot-devtools is like a timely rain, moistening students who stare at the screen in a daze.

Although this thing has been out for a long time, I find that people still use it less in real projects. But it is very easy to use.

You only need to add the following jar package to the pom file of the project to get a second-level service reload (hot deployment).

Org.springframework.boot spring-boot-devtools runtime true

Since the default value in the starter file is true, the configuration in yml below is not required.

Spring: devtools: restart: enabled: true

Let's check it out. It came very quickly.

Create a new simple controller and output halloworld.

@ Controller public class DemoController {@ GetMapping ("/ test") @ ResponseBody public String test () {return "halloworld";}}

Modify the code to change hallo to hello. The console will start scrolling the output log and loading the project code. At this point, visit the browser and find that our changes have taken effect.

# Log shows Started MbyeApplication in 1.731 seconds (JVM running for 51.115)

The console also outputs the time of this restart, which takes less than 2 seconds, which can be said to be very fast.

In order to be able to trigger compilation in real time after code changes, you need to do the following configuration in IDEA. If this configuration doesn't work, you need to click Build manually (note that it's not rebuild).

Why can hot deployment reload so quickly? Because its restart is not a complete restart of the entire application, but only restart our application code.

By configuring the META-INF/spring-devtools.properties file, you can specify that third-party jar packages are loaded each time you restart. But this kind of scene is rare. Of course, there is include and the old exclude, as shown in the following example.

Restart.exclude.somejar=/somejar- [\\ w -] +\ .jar restart.include.ajar=/ajar- [\\ w -] +\ .jar

Noticed an interesting thing. When we start with IDEA, the console output looks like this.

2020-09-18 21 c.g.javarunfast.mbye.MbyeApplication 33 Starting MbyeApplication on LYCYs-MacBook-Pro.local with PID 59.495 INFO 4635-[restartedMain] c.g.javarunfast.mbye.MbyeApplication: Starting MbyeApplication on LYCYs-MacBook-Pro.local with PID 4635 (/ target/classes started by xjjdog in / Users/xjjdog/codes/javarunfast/mbye) 2020-09-18 21 21 Swiss 33 Starting MbyeApplication on LYCYs-MacBook-Pro.local with PID 59.495 INFO 4635-[restartedMain] c.g.javarunfast.mbye.MbyeApplication: No active profile set Falling back to default profiles: default 2020-09-18 2121 restartedMain 34Multiple Spring Data modules found 00.355 INFO 4635-[restartedMain] .d.r.c.RepositoryConfigurationDelegate: Multiple Spring Data modules found, entering strict repository configuration mode! 2020-09-18211414141400.355 INFO 4635-[restartedMain] .d.r.c.RepositoryConfigurationDelegate: Bootstrapping Spring Data Elasticsearch repositories in DEFAULT mode. 2020-09-18 21 Finished Spring Data repository scanning in 1ms 3414 00.357 INFO 4635-[restartedMain] .s.d.r.c.RepositoryConfigurationDelegate. Found 0 Elasticsearch repository interfaces. 2020-09-18 21 Multiple Spring Data modules found 34 INFO 00.362 INFO 4635-[restartedMain] .s.d.r.c.RepositoryConfigurationDelegate: Multiple Spring Data modules found, entering strict repository configuration mode! 2020-09-18 21INFO 3421 INFO 4635-[restartedMain] .s.r.c.RepositoryConfigurationDelegate: Bootstrapping Spring Data Reactive Elasticsearch repositories in DEFAULT mode.

The startup thread inside is restartedMain. But when we use java-jar * jar to start, the main process is not restartedMain, but main.

This is because it makes no sense to turn on devtools in an online environment.

This sentence is said for the time being, because it will hit you in the face.

More Featur

To understand what devtools does, let's take a look at its source directory structure.

Filewatch and classpath needless to say, hot start can be achieved by listening for changes in files. In principle, it uses a separate ClassLoader (specifically RestartClassLoader) to complete the replacement after loading.

By learning this part of the code, you can have a better understanding of Java's classloader.

LiveReload

Then there is the livereload function.

LiveReload is often used when doing front-end development.

Devtools will also open a LiveReload Server in the background, and the browser will maintain a long connection with the Server. When the backend resources change, it will notify the browser to refresh and achieve hot deployment.

Below is the address of the Remote Live Reload plug-in for Chrome. Install it to have this cool feature.

Https://chrome.google.com/webstore/detail/remotelivereload/jlppknnillhjgiengoigajegdpieppei?hl=en-GB

Remote deployment

This is much more interesting. As we mentioned above, it is meaningless to turn on devtools in the online environment, now let's hit the face.

You may have a relatively low performance of your own machine, leaving the code to run on the remote end and code development locally. At this point, you can use remote hot deployment.

There are a lot of steps that need to be done to turn this on.

Step one.

You need to make the following changes to spring-boot-maven-plugin in pom.xml.

Org.springframework.boot spring-boot-maven-plugin false

Step two.

Set a key in yml for the connection between the server and the debugger.

Spring: devtools: remote: secret: test

Step three.

Package the SB service into jar and start it.

Mvn-Dmaven.test.skip=true-Pdev package java-jar-Xdebug\-Xrunjdwp:server=y,transport=dt_socket,suspend=n\ mbye-0.0.1-SNAPSHOT.jar

You can see that we added a lot of parameters when we started, which is what it means to enable remote flirting.

Step four.

Edit a Java file in the local IDEA and insert our server address (the same as the application address) in the startup variable.

Import org.springframework.boot.devtools.RemoteSpringApplication; / * * @ date 2020-09-19 * / public class Remote {public static void main (String [] args) {RemoteSpringApplication.main (new String [] {"http://localhost:8080"});}})

Step five.

To verify. Edit any file where you can see the effect, then click build.

The following is a screenshot of the IDEA developer.

The following is a screenshot of the server. You can see that the service has been reloaded, but very fast.

Listening for remote restart updates on /. ~ ~ springcolor bootleg restart Started MbyeApplication in 1.961 seconds (JVM running for 249.452)

Visit the web page and find that the code has been uploaded successfully.

In fact, spring-boot-devtools, is not the most powerful. Because it uses ClassLoader to reload the project's class file every time. If you have a lot of project files, it is also relatively slow.

At this point, the study on "how to deploy Spring-Boot-Devtools" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report