20.5 Remote Applications
Spring Boot开发工具并不局限于本地开发.在远程运行应用程序时,你还是可以使用几个特性.远程支持是可选的.要启用它,您需要确保将devtools
包含在打包的归档中,如下面的清单所示:
<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属性,如下例所示:
spring.devtools.remote.secret=mysecret
Warning
在远程应用程序上启用
spring-boot-devtools
将是安全风险.你不应该在生产部署环境中使能该支持.
远程devtools支持分以下两部分:一个服务器端端点,它接受IDE中运行的客户端应用程序和连接.当配置了spring.devtools.remote.secret属性时,服务器组件自动启用支持.而客户端组件必须手动启动.
Running the Remote Client Application
远程客户端应用程序设计为从您的IDE中运行.您需要从你连接的远程项目相同的类路径下运行org.springframework.boot.devtools.RemoteSpringApplication
.应用程序的唯一参数为远程连接URL地址.
例如,如果您正在使用Eclipse或STS并且你有一个名为my-app
项目已部署到Cloud Foundry上,你需要做以下几点:
- 在
Run
菜单中选择Run Configurations...
. - 创建一个新的
Java Application
并加载配置. - 浏览
my-app
的项目. - 使用
org.springframework.boot.devtools.RemoteSpringApplication
作为主要类. - 添加
https://myapp.cfapps.io
到程序参数(或你的远程URL地址).
一个运行的远程客户端可能类似于下面:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ ___ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | | _ \___ _ __ ___| |_ ___ \ \ \ \
\\/ ___)| |_)| | | | | || (_| []::::::[] / -_) ' \/ _ \ _/ -_) ) ) ) )
' |____| .__|_| |_|_| |_\__, | |_|_\___|_|_|_\___/\__\___|/ / / /
=========|_|==============|___/===================================/_/_/_/
:: Spring Boot Remote :: 2.0.2.RELEASE
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)
Note
因为远程客户端使用相同的类路径作为真正的应用程序,它可直接读取应用程序属性.这就是为何
spring.devtools.remote.secret
属性被读取并传递到服务器进行身份验证.Tip
总是明智的使用
https://
作为连接的协议,因此流量将会被加密并且密码将不能被拦截.Tip
如果你需要使用一个代理来访问远程应用程序,请配置
spring.devtools.remote.proxy.host
和spring.devtools.remote.proxy.port
属性。
Remote Update
与本地重启机制一致,远程客户端也监控您的应用类路径文件更改.任何被部署到远程应用程序资源更新时(如果需要的话)触发重新启动. 如果您在一个云服务的应用上进行迭代更新某个特性,那么这将是非常有用的.一般来说,远程更新和重新启动比完整的重建和部署周期要快得多.
Note
只在远程客户端运行时监控文件.如果你在开启远程客户端之前更改了一个文件,那么它将不会被推送到远程服务器.