最近需要调试 SpringBoot 应用的三方接口,这些接口经常有 domain 相关的限制,需要在服务器上调试。
Remote debug + DetTools Remote Update 两个功能可以同时使用,可以让远程开发调试事半功倍。
Remote Debug
远程服务器运行的 SpringBoot 应用支持 JDWP,可以在本地上设置 Remote Debug,进行远程断点调试:
服务器应用启动命令
java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n \
-jar devdemo.jar
本地 Remote Debug 启动参数
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000
Remote Remote Update
本地修改代码后,Remote Update 会反映新的 class 到远程服务器上,避免不必要的部署。
添加 DetTools 依赖
在 build.gradle 文件中添加:
//...
apply plugin: 'org.springframework.boot'
// for SpringBoot2
bootJar {
excludeDevtools = false
}
//...or for SpringBoot1
springBoot {
excludeDevtools = false
}
dependencies {
//...
runtime('org.springframework.boot:spring-boot-devtools')
}
配置 remote secret
在 application.properties 中添加:
spring.devtools.remote.secret=mydemosharedsecret
remote.secret
配置用于与远程服务器的连接。
如果远程服务器的和本地的 remote.secret
配置不一致,则无法使用 Remote Update.
启动 DevTools Remote Update
- 为当前项目新建 Java Application launch configuration
- 指定 Main class:
org.springframework.boot.devtools.RemoteSpringApplication
- 指定 Program arguments 为应用的 HTTP(S) URL
触发 DevTools Remote Update
方法a 设置 IDE 的自动编译
以 InteliJ Idea 为例:
- 开启 Compiler -> Build project automatically
- 开启 Registry... 中的
compiler.automake.allow.when.app.running
选项
保存文件后将自动触发编译和 Remote Update
方法b 手动执行编译
./gradlew classes
References
https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html
https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/introclientissues005.html