Spring boot hot deployment - springloader and devtools

During the development process, we are often wasted time by some unimportant things. I personally think that restarting the service is the biggest pitfall, because now a system of the maintenance company is a project 12 years ago, the structure is more complicated, and the code is bloated. During the development process, it took half an hour to start and start Tomcat, and so did junit. Therefore, in the process of subsequent project development and learning, we are looking for the function of hot deployment and release, and it is no longer cheap to restart the service because of a line of code or a line of logs.

There are two ways of hot deployment of
sprinigboot: 1. Hot deployment through springloader

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin </artifactId>
    <dependencies>  
       <dependency>  
           <groupId>org.springframework</groupId>  
           <artifactId>springloaded</artifactId>  
           <version>1.2.4.RELEASE</version>
       </dependency>  
    </dependencies>  
    <executions>  
       <execution>  
           <goals>  
               <goal>repackage</goal>  
           </goals>  
           <configuration>  
               <classifier>exec</classifier>  
           </configuration>  
       </execution>  
    </executions>
</plugin> 

To add springloader, you only need to add the above plugin to the pom file, and then start it through maven command mode. You can also use run as javaApplication to start in ide. Some configuration is required when starting in this mode:
- Download springloaded-1.2.4.RELEASE.jar to the specified folder
- Add the running command: -javaagent:.\lib\springloaded -1.2.4.RELEASE.jar -noverify (the specific addition method is set according to the different IDE)
2. The developer module provided by springboot: devtools

.....
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
   <scope>true</scope>
</dependency>
.....
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <!--fork :  如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart -->
        <fork>true</fork>
    </configuration>
</plugin>

Compared with springloader, devtools first needs to introduce dependencies in the pom file, followed by adding devtools compilation plugins. After these two steps are implemented, you don't need to set other content, you can start directly

devtools implementation principle

Devtools uses two ClassLoaders, one ClassLoader loads classes that will not change (third-party Jar packages), and the other ClassLoader loads classes that will change, called restart ClassLoader
, so that when code changes, the original restart ClassLoader is used. Discard and recreate a restart ClassLoader. Since there are fewer classes to be loaded, a faster restart time (within 5 seconds) is achieved.

Comparing the two ways

  • Springloader hot deployment can only modify existing methods and pages. If it is a newly added method or class, it cannot be automatically deployed. And devtools monitors all file changes under the classpath, and automatically deploys if changes occur. Including configuration files, classes, pages, etc.
  • Devtools can implement hot page deployment (that is, the page will take effect immediately after modification, which can be achieved by configuring spring.thymeleaf.cache=false directly in the application.properties file (note that the configuration of different templates is different). Network information, no pro Measurement.
  • If springloader is a normal startup, you need to download the jar package and configure the relevant startup parameters

Notes for devtools development:

  1. Select the corresponding spring-boot version, try not to set the version number yourself when adding dependencies, springboot will choose the appropriate version
  2. Add plugin and attribute true to pom
  3. Determine whether the IDE has automatic compilation enabled. I use idea myself. Automatic compilation is turned off and needs to be deployed. Ctrl + F9
  4. If SpringApplication.setRegisterShutdownHook(false) is set, automatic restart will not work.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324669070&siteId=291194637