Hot deployment with springboot

Time:2021-7-22

preface

In the actual development process, every time you modify the code, you have to restart the project and re deploy it. For some large applications, the restart time costs a lot of time. For a back-end developer, the restart process is really hard. In the field of java development, hot deployment has always been a difficult problem to solve. The current Java virtual machine can only realize the hot deployment of method body modification. For the structure modification of the whole class, it still needs to restart the virtual machine and reload the class to complete the update operation. Let's see how to implement hot deployment for simple class modification.

principle

The deep principle is that two classloaders are used. One loads the classes that will not change (third-party jar package), and the other loads the classes that will change, which are called restart classloader. In this way, when the code changes, the original restart classloader will be discarded and a restart classloader will be created. Because there are fewer classes to load, So it achieves faster restart time.

Start configuration

Add dependencies to pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Devtools hot deployment -- >
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
            <scope>true</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>

Configuration of devtools

Configure devtools in application. YML

spring:
  devtools:
    restart:
      Enabled: true # set to enable hot deployment
      Additional paths: Src / main / Java # restart directory
      exclude: WEB-INF/**
  freemarker:
    Cache: false # the page does not load the cache, and the modification takes effect immediately

Configuration in idea

When we modify the class file, idea will not compile automatically. We have to modify the idea settings.

  • (1)File-Settings-Compiler-Build Project automatically
  • (2) CTRL + Shift + Alt + /, select registry, and check compiler automake allow when app running

Hot deployment with springboot

Hot deployment with springboot

This work adoptsCC agreementReprint must indicate the author and the link of this article

Recommended Today

ES6 Usage Summary

Destructuring assignment Realize variable exchange let a =1; let b=2 [a,b]=[b,a] console.log(a,b)//2,1 Default value assignment [a,b,c=3]=[1,2] console.log(a,b,c)//1,2,3 Get function return value directly function fc(){ return [1,2] } let a,b; [a,b]=fc() console.log(a,b)//1,2 Nested deconstruction assignment of objects let metadata={ title:’currentTitle’, test:[{ title:”deepTitle”, desc:’description’ }] } let {title:titleone,test:[{title:titlescond}]}=metadata; console.log(titleone,titlescond)//currentTitle,deepTitle String extension If the Unicode code is greater […]