当先锋百科网

首页 1 2 3 4 5 6 7

获得技能

掌握SpringBoot热部署技能

本节对应教学视频

SpringBoot从入门到精通 SSM/MyBatis/Redis/KafKa/SpringCloud​ke.qq.com
08c9013f68eab23cd98c267a9de4f98a.png

知识要点

  • 增加spring-boot-devtools依赖打开热部署
  • 热部署原理:动态ClassLoader
  • 通过配置打开和关闭热部署

通过重新编译的方式完成SpringBoot项目重启,热部署可以快速重启项目,一般只用在开发环境,生产环境不要使用。

图解热部署原理

SpringBoot项目将系统的类拆分到两个ClassLoader。

  • 静态ClassLoader
  • 动态ClassLoader
    使用静态ClassLoader和动态ClassLoader隔离的加载方式。热部署的时候,静态类加载器保持不变,仅仅让动态类加载器加载所需要的类,加快JVM类的加载速度

ac797e239a8e6acdaf89be7ff1a0aee7.png
参考资料 官方文档
The restart technology provided by Spring Boot works by using two classloaders. Classes that do not change (for example, those from third-party jars) are loaded into a base classloader. Classes that you are actively developing are loaded into a restart classloader. When the application is restarted, the restart classloader is thrown away and a new one is created. This approach means that application restarts are typically much faster than “cold starts”, since the base classloader is already available and populated.
If you find that restarts are not quick enough for your applications or you encounter classloading issues, you could consider reloading technologies such as JRebel from ZeroTurnaround. These work by rewriting classes as they are loaded to make them more amenable to reloading.

上机实验

  1. 实验: 打开热部署
  • 增加spring-boot-devtools依赖打开热部署
  • 打开热部署配置 【可选项,系统已经默认打开】

实验: 关闭热部署

    • 关闭热部署配置

增加spring-boot-devtools依赖打开热部署

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
 </dependency>

打开热部署配置

spring.devtools.restart.enabled: true

关闭热部署配置

# 关闭热部署
spring.devtools.restart.enabled: false