Spring Boot 远程调试

远程应用(Remote Applications)

Spring Boot devtools 工具,不仅能在本地环境使用,某些特性也可以应用于远程运行中的程序。远程支持是可选择的特性。开启远程支持,你需要确认 devtools 依赖已经包含在应用的打包过程中,如下:

1
2
3
4
5
6
7
8
9
10
11
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludeDevtools>false</excludeDevtools>
</configuration>
</plugin>
</plugins>
</build>

然后,你需要设置 spring.devtools.remote.secret 属性,如下:

1
spring.devtools.remote.secret=mysecret

在远程应用中开启 spring-boot-devtools 是一个具有安全风险的事情,不应该在生产环境中启用。

远程 devtools 支持包含两方面:

  • 一个服务端 endpoint 接收链接
  • 一个运行在 IDE 中的客户端应用
    spring.devtools.remote.secret 属性设置之后,服务端组件会自动启用,本地组件需要手动启动

运行远程客户端应用(Running the Remote Client Application


远程客户端程序被设计成运行在你本地 IDE 中。你需要在和所连接的远程应用相同的 classpath 下 运行 org.springframework.boot.devtools.RemoteSpringApplication。应用需要的唯一参数就是需要连接的应用的远程 URL

例如,如果你使用的是 Eclipse 或者 STS,并且你有一个名为 my-app 的项目部署在 Cloud Foundry,那么你需要做如下操作:

  • Run 菜单下选择 Run Configurations…​.
  • 创建一个新的 Java Application launch configuration.
  • 打开 my-app 项目.
  • 使用 org.springframework.boot.devtools.RemoteSpringApplication 作为 main 类.
  • 添加远程 URL 到程序参数

一个运行中的远程客户端会输出如下信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
 .   ____          _                                              __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ ___ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | | _ \___ _ __ ___| |_ ___ \ \ \ \
\\/ ___)| |_)| | | | | || (_| []::::::[] / -_) ' \/ _ \ _/ -_) ) ) ) )
' |____| .__|_| |_|_| |_\__, | |_|_\___|_|_|_\___/\__\___|/ / / /
=========|_|==============|___/===================================/_/_/_/
:: Spring Boot Remote :: 2.2.1.BUILD-SNAPSHOT

2015-06-10 18:25:06.632 INFO 14938 --- [ main] o.s.b.devtools.RemoteSpringApplication : Starting RemoteSpringApplication on pwmbp with PID 14938 (/Users/pwebb/projects/spring-boot/code/spring-boot-devtools/target/classes started by pwebb in /Users/pwebb/projects/spring-boot/code/spring-boot-samples/spring-boot-sample-devtools)
2015-06-10 18:25:06.671 INFO 14938 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2a17b7b6: startup date [Wed Jun 10 18:25:06 PDT 2015]; root of context hierarchy
2015-06-10 18:25:07.043 WARN 14938 --- [ main] o.s.b.d.r.c.RemoteClientConfiguration : The connection to http://localhost:8080 is insecure. You should use a URL starting with 'https://'.
2015-06-10 18:25:07.074 INFO 14938 --- [ main] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2015-06-10 18:25:07.130 INFO 14938 --- [ main] o.s.b.devtools.RemoteSpringApplication : Started RemoteSpringApplication in 0.74 seconds (JVM running for 1.105)

  • 因为远程客户端运行在和真实应用相同的 classpath 目录下,它可以直接读取应用的 properties 属性。这就是为什么 spring.devtools.remote.secret 属性可以被读取并传输到服务端进行校验。
  • 建议使用 https:// 作为连接协议,这样所有传输的数据都被加密,密码不会被截获。
  • 如果需要使用代理连接远程应用,需要配置 spring.devtools.remote.proxy.host 以及 spring.devtools.remote.proxy.port 属性

远程更新(Remote Update)

远程客户端应用会以本地重启(资源更新后,应用会更新重启)同样的方式,监控你应用中 classpath 的变化,所有更新的资源都会被推送到远程应用,并且(如果需要)会触发应用重启。在你想迭代一个运行在云端的服务的特性的时候,这就非常有用了。总的来说,这种方式的远程的更新及重启,会比一个完成的重新构建和发布快得多。

当远程客户端在运行中的时候,文件才会被监控,如果你在远程客户端运行前修改了一个文件,那么这个文件将不会被推送到远程服务器

参考资料

  1. using-boot-devtools-remote