spingbootspringbootalready

加速 SpringBoot 应用开发,官方热部署神器真带劲!

2021-04-15  本文已影响0人  程序员知识圈

平时使用SpringBoot开发应用时,修改代码后需要重新启动才能生效。如果你的应用足够大的话,启动可能需要好几分钟。有没有什么办法可以加速启动过程,让我们开发应用代码更高效呢?今天给大家推荐一款SpringBoot官方的热部署工具spring-boot-devtools,修改完代码后可快速自动重启应用!

spring-boot-devtools简介

SpringBoot官方开发工具,如果你的应用集成了它,即可实现热部署和远程调试。

实现原理

使用该工具应用为什么启动更快了?主要是因为它使用了两种不同的类加载器。基础类加载器用于加载不会改变的类(比如第三方库中的类),重启类加载器用于加载你应用程序中的类。当应用程序启动时,重启类加载器中的类将会被替换掉,这就意味着重启将比冷启动更快!

热部署

接下来我们将集成devtools,来演示下热部署功能。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>
/**
 * 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);
    }
}
{
  "code": 200,
  "message": "返回消息",
  "data": null
}
/**
 * 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);
    }
}
{
  "timestamp": "2021-03-29T07:09:05.415+00:00",
  "status": 404,
  "error": "Not Found",
  "message": "No message available",
  "path": "/test/first"
}
spring:
  devtools:
    restart:
      poll-interval: 2s
      quiet-period: 1s
{
  "code": 200,
  "message": "返回消息(已修改)",
  "data": null
}

远程调试

devtools除了支持热部署之外,还支持远程调试,接下来我们把应用部署到Docker容器中,然后试试远程调试!

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <!--打包时不排除Devtools-->
        <excludeDevtools>false</excludeDevtools>
    </configuration>
</plugin>
 spring:
  devtools:
    remote:
      secret: macro666
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
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)
/**
 * 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);
    }
}
{
  "code": 200,
  "message": "返回消息(远程调试)",
  "data": null
}

总结

虽说使用SpringBoot官方的devtools可以进行热部署,但是这种方式更像是热重启,如果你想要更快的热部署体验的话可以使用JRebel

上一篇下一篇

猜你喜欢

热点阅读