20.5 Remote Applications

Spring Boot开发工具并不局限于本地开发.在远程运行应用程序时,你还是可以使用几个特性.远程支持是可选的.要启用它,您需要确保将devtools包含在打包的归档中,如下面的清单所示:

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

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

  1. 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地址).

一个运行的远程客户端可能类似于下面:

  1. . ____ _ __ _ _
  2. /\\ / ___'_ __ _ _(_)_ __ __ _ ___ _ \ \ \ \
  3. ( ( )\___ | '_ | '_| | '_ \/ _` | | _ \___ _ __ ___| |_ ___ \ \ \ \
  4. \\/ ___)| |_)| | | | | || (_| []::::::[] / -_) ' \/ _ \ _/ -_) ) ) ) )
  5. ' |____| .__|_| |_|_| |_\__, | |_|_\___|_|_|_\___/\__\___|/ / / /
  6. =========|_|==============|___/===================================/_/_/_/
  7. :: Spring Boot Remote :: 2.0.2.RELEASE
  8. 2015-06-10 18:25:06.632 INFO 14938 --- [ main] o.s.b.devtools.RemoteSpringApplication :
  9. Starting RemoteSpringApplication on pwmbp with PID 14938 (/Users/pwebb/projects/spring-boot/code/
  10. spring-boot-devtools/target/classes started by pwebb in /Users/pwebb/projects/spring-boot/code/spring-
  11. boot-samples/spring-boot-sample-devtools)
  12. 2015-06-10 18:25:06.671 INFO 14938 --- [ main] s.c.a.AnnotationConfigApplicationContext :
  13. Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2a17b7b6: startup
  14. date [Wed Jun 10 18:25:06 PDT 2015]; root of context hierarchy
  15. 2015-06-10 18:25:07.043 WARN 14938 --- [ main] o.s.b.d.r.c.RemoteClientConfiguration : The
  16. connection to http://localhost:8080 is insecure. You should use a URL starting with 'https://'.
  17. 2015-06-10 18:25:07.074 INFO 14938 --- [ main] o.s.b.d.a.OptionalLiveReloadServer :
  18. LiveReload server is running on port 35729
  19. 2015-06-10 18:25:07.130 INFO 14938 --- [ main] o.s.b.devtools.RemoteSpringApplication :
  20. Started RemoteSpringApplication in 0.74 seconds (JVM running for 1.105)

Note

因为远程客户端使用相同的类路径作为真正的应用程序,它可直接读取应用程序属性.这就是为何spring.devtools.remote.secret属性被读取并传递到服务器进行身份验证.

Tip

总是明智的使用https://作为连接的协议,因此流量将会被加密并且密码将不能被拦截.

Tip

如果你需要使用一个代理来访问远程应用程序,请配置spring.devtools.remote.proxy.hostspring.devtools.remote.proxy.port属性。

Remote Update

与本地重启机制一致,远程客户端也监控您的应用类路径文件更改.任何被部署到远程应用程序资源更新时(如果需要的话)触发重新启动. 如果您在一个云服务的应用上进行迭代更新某个特性,那么这将是非常有用的.一般来说,远程更新和重新启动比完整的重建和部署周期要快得多.

Note

只在远程客户端运行时监控文件.如果你在开启远程客户端之前更改了一个文件,那么它将不会被推送到远程服务器.