头图
When you usually use SpringBoot to develop applications, you need to restart after modifying the code to take effect. If your application is large enough, it may take several minutes to start. Is there any way to speed up the startup process and let us develop application code more efficiently? Today I recommend a SpringBoot official hot deployment tool spring-boot-devtools , which can quickly and automatically restart the application after modifying the code!

SpringBoot actual combat e-commerce project mall (40k+star) address: https://github.com/macrozheng/mall

spring-boot-devtools Profile

The official SpringBoot development tool, if your application integrates it, you can achieve hot deployment and remote debugging.

Realization principle

Why does the application start faster with this tool? Mainly because it uses two different class loaders. The basic class loader is used to load classes that will not change (such as classes in third-party libraries), and the restart class loader is used to load classes in your application. When the application starts, the classes in the restart class loader will be replaced, which means the restart will be faster than a cold start!

Hot deployment

Next, we will integrate devtools to demonstrate the hot deployment function.
  • First, you need to add the dependency of devtools in pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>
  • In order to facilitate the test, we have added the following test interface to the project;
/**
 * Created by macro on 2021/3/25.
 */
@Api(tags = "TestController", description = "SpringBoot Dev Tools测试")
@Controller
@RequestMapping("/test")
public class TestController {

    @ApiOperation("测试修改")
    @RequestMapping(value = "/first", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult first() {
        String message = "返回消息";
        return CommonResult.success(null,message);
    }
}
  • Then start the project, access the interface through Swagger after the startup is successful, the return result is as follows, the access address: http://localhost :8088/swagger-ui.html
{
  "code": 200,
  "message": "返回消息",
  "data": null
}
  • Since devtools will automatically restart the project when the project is built, and IDEA does not use automatic build by default, we can modify the application startup configuration to set the project to be built automatically when IDEA loses focus;

  • To modify the code in the Controller, just modify the message variable;
/**
 * Created by macro on 2021/3/25.
 */
@Api(tags = "TestController", description = "SpringBoot Dev Tools测试")
@Controller
@RequestMapping("/test")
public class TestController {

    @ApiOperation("测试修改")
    @RequestMapping(value = "/first", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult first() {
        String message = "返回消息(已修改)";
        return CommonResult.success(null,message);
    }
}
  • After losing the focus, waiting for the project to be automatically built, at this time the access interface has a 404 problem;
{
  "timestamp": "2021-03-29T07:09:05.415+00:00",
  "status": 404,
  "error": "Not Found",
  "message": "No message available",
  "path": "/test/first"
}
  • Due to the difference between the detection time of devtools and the compilation time of IDEA, when IDEA has not been compiled, devtools has restarted the application, which caused this problem. Modify the application.yml configuration file and add the following configuration;
spring:
  devtools:
    restart:
      poll-interval: 2s
      quiet-period: 1s
  • Access the test interface again at this time, the display content is as follows, and the modified code has been automatically applied.
{
  "code": 200,
  "message": "返回消息(已修改)",
  "data": null
}

Remote debugging

In addition to supporting hot deployment, devtools also supports remote debugging. Next, we deploy the application into a Docker container, and then try remote debugging!
  • Since the default package of SpringBoot will not include devtools, we need to modify pom.xml first;
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <!--打包时不排除Devtools-->
        <excludeDevtools>false</excludeDevtools>
    </configuration>
</plugin>
  • Next, you need the application.yml file and add the remote access password for devtools;
spring:
  devtools:
    remote:
      secret: macro666
  • Next, package the project into a Docker image, and then run it with the following command;
docker run -p 8088:8088 --name mall-tiny-devtools \
--link mysql:db \
-v /etc/localtime:/etc/localtime \
-v /mydata/app/mall-tiny/logs:/var/logs \
-d mall-tiny/mall-tiny-devtools:1.0-SNAPSHOT
  • Add a startup configuration, modify the startup class to org.springframework.boot.devtools.RemoteSpringApplication , the configuration information is as follows;

  • Start the configuration, the console output the following results, indicating that the remote connection is successful;
2021-03-29 15:49:50.991  INFO 7848 --- [           main] o.s.b.devtools.RemoteSpringApplication   : Starting RemoteSpringApplication v2.3.0.RELEASE on DESKTOP-5NIMJ19 with PID 7848
2021-03-29 15:49:51.003  INFO 7848 --- [           main] o.s.b.devtools.RemoteSpringApplication   : No active profile set, falling back to default profiles: default
2021-03-29 15:49:51.664  WARN 7848 --- [           main] o.s.b.d.r.c.RemoteClientConfiguration    : The connection to http://192.168.5.78:8088 is insecure. You should use a URL starting with 'https://'.
2021-03-29 15:49:52.024  INFO 7848 --- [           main] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2021-03-29 15:49:52.055  INFO 7848 --- [           main] o.s.b.devtools.RemoteSpringApplication   : Started RemoteSpringApplication in 2.52 seconds (JVM running for 4.236)
  • Next, we modify the test code in the Controller again, as long as we modify the message variable;
/**
 * Created by macro on 2021/3/25.
 */
@Api(tags = "TestController", description = "SpringBoot Dev Tools测试")
@Controller
@RequestMapping("/test")
public class TestController {

    @ApiOperation("测试修改")
    @RequestMapping(value = "/first", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult first() {
        String message = "返回消息(远程调试)";
        return CommonResult.success(null,message);
    }
}
  • If the remote debugging is automatically built, it will cause the remote service to restart frequently. At this time, we can use IDEA to build manually, and the build button can be found in the right-click menu of the project;

  • After the build is successful, you can find that the remote service will automatically restart, and apply the modified code, access the test interface and return the following information;
{
  "code": 200,
  "message": "返回消息(远程调试)",
  "data": null
}

to sum up

Although you can use SpringBoot's official devtools for hot deployment, this method is more like a hot restart. If you want a faster hot deployment experience, you can use JRebel.

Project source address

https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-devtools

This article GitHub https://github.com/macrozheng/mall-learning has been included, welcome to Star!

macrozheng
1.1k 声望1.3k 粉丝