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

SpringBoot远程热部署实战
本文介绍如何使用SpringBoot的devtools模块实现代码的远程自动同步功能,通过配置pom.xml和application.properties,本地代码修改后能自动同步到远程实例,无需重新打包或构建,极大提升了开发效率。

背景

在使用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. 此方式不可用在生产环境
### Spring Boot DevTools 远程调试配置及使用方法 为了实现基于 `spring-boot-devtools` 的远程调试功能,需要完成以下几个方面的配置: #### 1. 启用远程调试支持 在项目的 `application.properties` 文件中添加如下配置以启用远程调试功能[^1]: ```properties spring.devtools.remote.secret=your_secret_key ``` 这里的 `your_secret_key` 是一个自定义的安全密钥,用于保护远程连接。 #### 2. 开启本地类路径监控 为了让应用程序能够实时响应代码变更并自动重载,需确保以下属性已正确设置[^2][^3]: ```properties spring.devtools.restart.enabled=true spring.devtools.livereload.enabled=true ``` 这些参数分别表示开启热部署以及启用 LiveReload 功能。当类路径中的资源发生变化时,Spring Boot 应用程序将会被重新加载。 #### 3. 设置 JVM 调试选项 要启动远程调试模式,在运行 Java 应用程序时需要指定特定的 JVM 参数。以下是常用的命令行参数示例: ```bash -javaagent:/path/to/spring-boot-devtools.jar \ -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 ``` 上述命令的作用是附加 `spring-boot-devtools` 并开放端口 `5005` 来监听来自 IDE 的调试请求。其中 `-javaagent` 表明引入了 devtools 工具包;而 JDWP (Java Debug Wire Protocol) 则负责处理实际的调试通信过程。 #### 4. 使用 IDE 连接至目标进程 最后一步是在集成开发环境中创建一个新的 Remote Debugging 配置项,并将其绑定到之前设定好的侦听地址上(即 localhost 和 port 5005)。一旦建立成功,开发者就可以像对待普通的断点一样操作远端服务实例上的线程状态和变量值了。 ```python import java.lang.management.ManagementFactory; public class Main { public static void main(String[] args){ System.out.println(ManagementFactory.getRuntimeMXBean().getInputArguments()); } } ``` 此段代码可以帮助验证当前 JVM 是否已经打开了调试接口。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值