1. 热部署
热部署,顾名思义,就是随时准备部署代码。换句话说,就是当代码发生改变时,立即将更新后的代码重新编译、运行。其实现原理大抵是这样的,系统会开两个ClassLoader,一个ClassLoader负责加载无改动的代码,另一个ClassLoader负责动态加载变动的代码。当代码发生改变时,负责动态加载变动代码的ClassLoader就会将代码重新编译、运行。
其实我一直在想,为什么会出现热部署这个功能?答案可能只有一个字,那就是懒。程序员大都是比较懒的,每次修改完代码后,还需要去点run那个按钮,我相信任何一个程序员都不愿意进行这样的操作。所以,哪里有需求,哪里就有解决方案,最终一个比较厉害的程序员开发出了这样一个功能,当代码更新完后,自动替代人工的run操作。这样做的好处就是大大减小了人工的工作量。
2. 实现步骤
2.1 加入maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
2.2 测试
首先实现一个Controller
@GetMapping("/health")
public String health() {
return "health";
}
浏览器输入测试地址:http://localhost/backend/core/health
结果:

Controller的返回值改为health 2,代码为:
@GetMapping("/health")
public String health() {
return "health 2";
}
保存完后,可看到如下log日志
2020-12-25 15:35:55.302 INFO 26596 --- [ Thread-16] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.2.RELEASE)
2020-12-25 15:35:55.633 INFO 26596 --- [ restartedMain] com.shane.happy.HappyApplication : Starting HappyApplication on DESKTOP-JVC4NB6 with PID 26596 (C:\codeSpace\marry\romantic\target\classes started by Mr-Sui in C:\codeSpace\marry\romantic)
2020-12-25 15:35:55.633 INFO 26596 --- [ restartedMain] com.shane.happy.HappyApplication : No active profile set, falling back to default profiles: default
2020-12-25 15:35:56.120 INFO 26596 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2020-12-25 15:35:56.120 INFO 26596 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2020-12-25 15:35:56.135 INFO 26596 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 14ms. Found 0 Redis repository interfaces.
2020-12-25 15:35:56.318 INFO 26596 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 80 (http)
2020-12-25 15:35:56.319 INFO 26596 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-12-25 15:35:56.319 INFO 26596 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.37]
可以看到,程序又启动起来了。
浏览器继续输入地址,验证没有问题。
本文介绍了Spring Boot的热部署功能,旨在减少程序员手动重启应用的繁琐过程。通过加入Maven依赖并进行简单配置,可以实现在代码更改后自动编译并运行新版本,提高开发效率。
791

被折叠的 条评论
为什么被折叠?



