Spring Boot使用devtools实现远程自动更新远程热发布

背景

在使用Spring Boot开发时可以使用spring-boot-devtools实现热加载功能,devtools还有一个更高级的功能:远程自动同步。即本地代码的更改能自动同步到远程实例中,不用本地打jar包复制过去或者jenkins构建那么麻烦。有时想临时在服务器上跑个程序调试一下将是非常方便的。

举个例子,当前在测试服务器192.168.10.5运行着myserver,在本地修改项目中的代码后,能够自动将修改的代码同步到测试服务器的myserver实例中,并且远程服务还是无重启的。

实战

1、配置

服务就是一个普通的Spring Boot Web项目,首先需要对pom.xml稍作修改,添加<excludeDevtools>false</excludeDevtools>打包时保留devtools

<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <excludeDevtools>false</excludeDevtools>
    </configuration>
</plugin>

application.properties中添加安全配置

spring.devtools.remote.secret=mysecret

mysecret相当于密码,client和remote server之间同步代码时就使用这个密码进行校验

2、启动远端服务

  • 定义一个普通的接口
@GetMapping("/index1")
@ResponseBody
public String index1(Model mode){
    return LocalDateTime.now().toString();
}
  • 远端服务模拟模拟在127.0.0.1:8081
  • 项目根目录mvn package打包,
  • java -jar target/xxx.jar启动
  • 启动日志中有一句不一样的日志输出
[ main] .s.b.d.a.RemoteDevToolsAutoConfiguration : Listening for remote restart updates on /.~~spring-boot!~/restart

2、启动本地服务

本地服务使用idea启动在127.0.0.1:8082端口

  • 指定Main Class为org.springframework.boot.devtools.RemoteSpringApplication
  • 指定Program arguments为http://127.0.0.1:8081 即远端服务的地址
    在这里插入图片描述
  • 启动项目
    可以看到这时控制台的输出比较少
  .   ____          _                                              __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _          ___               _      \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` |        | _ \___ _ __  ___| |_ ___ \ \ \ \
 \\/  ___)| |_)| | | | | || (_| []::::::[]   / -_) '  \/ _ \  _/ -_) ) ) ) )
  '  |____| .__|_| |_|_| |_\__, |        |_|_\___|_|_|_\___/\__\___|/ / / /
 =========|_|==============|___/===================================/_/_/_/
 :: Spring Boot Remote ::  (v2.1.2.RELEASE)

[ main] o.s.b.devtools.RemoteSpringApplication   : Starting RemoteSpringApplication v2.1.2.RELEASE on fangliangshengdeMacBook-Pro.local with PID 67016 (/Users/fangliangsheng/.m2/repository/org/springframework/boot/spring-boot-devtools/2.1.2.RELEASE/spring-boot-devtools-2.1.2.RELEASE.jar started by fangliangsheng in /Users/fangliangsheng/Documents/git/java-basics)
[ main] o.s.b.devtools.RemoteSpringApplication   : No active profile set, falling back to default profiles: default
[ main] o.s.b.d.r.c.RemoteClientConfiguration    : The connection to http://127.0.0.1:8081 is insecure. You should use a URL starting with 'https://'.
[ main] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
[ main] o.s.b.devtools.RemoteSpringApplication   : Started RemoteSpringApplication in 0.574 seconds (JVM running for 1.095)

验证

  1. 访问远端服务 http://127.0.0.1:8081/index1
    在这里插入图片描述
  2. 在本地代码中修改代码
@GetMapping("/index2")
@ResponseBody
public String index2(Model mode){
    return LocalDateTime.now().toString();
}
  1. 编译Build Project或者Recompile ‘xxxx.java’
    可以看到控制台输出,开始上传class resource,并触发LiveReload
[   File Watcher] o.s.b.d.r.c.ClassPathChangeUploader      : Uploaded 1 class resource
[pool-1-thread-1] o.s.b.d.r.c.DelayedLiveReloadTrigger     : Remote server has changed, triggering LiveReload
[   File Watcher] o.s.b.d.r.c.ClassPathChangeUploader      : Uploaded 2 class resources
[pool-1-thread-1] o.s.b.d.r.c.DelayedLiveReloadTrigger     : Remote server has changed, triggering LiveReload
  1. 刷新http://127.0.0.1:8081/index1
    因为index1改成了index2,所以无法访问了
    在这里插入图片描述
  2. 访问http://127.0.0.1:8081/index2
    正常输出,说明远程更新生效了在这里插入图片描述

注意事项

  1. 如果本地与远端的spring.devtools.remote.secret配置不一样,则本地在同步代码时会报错
    Exception in thread "File Watcher" java.lang.IllegalStateException: Unexpected 403 FORBIDDEN response uploading class files
  2. 此方式不可用在生产环境

猜你喜欢

转载自blog.csdn.net/f4761/article/details/86576013