上一篇[20、开发者工具(1)]
下一篇[23、SpringApplication(1)]
英文原文:https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/using-boot-devtools.html#using-boot-devtoolsGitHub:https://github.com/jijicai/Spring/tree/master/spring-boot
20.3、实时重新加载(LiveReload)
spring-boot-devtools 模块包含一个嵌入式 LiveReload 服务器,可以用于在资源更改时触发浏览器刷新。从网站 livereload.com 可以免费获得 Chrome、Firefox 和 Safari 的LiveReload 浏览器扩展。
如果运行应用时不希望启动 LiveReload 服务器,则可以将 spring.devtools.livereload.enabled 属性设置为 false。
注释:一次只能够运行一个 LiveReload 服务器。在启动应用之前,请确保没有其他的 LiveReload 服务器在运行。如果从 IDE 启动多个应用,那么只有第一个获得 LiveReload 支持。
20.4、全局设置
通过添加名为 .spring-boot-devtools.properties 的文件到 $HOME 目录,可以配置全局 devtools 设置(注意,文件名以“.”开始)。添加到此文件中的任何属性都被应用到你电脑上使用 devtools 的所有 Spring Boot 应用。例如,要将 restart 配置为始终使用触发器文件,可以添加以下属性(在文件 ~/.spring-boot-devtools.properties 中添加):
spring.devtools.reload.trigger-file=.reloadtrigger
注释:在 .spring-boot-devtools.properties 中激活的配置不会影响特定配置(profile-specific)的配置文件的加载。
20.5、远程应用程序
Spring Boot 开发者工具不限于本地开发。当远程地运行应用时,还能够使用一些功能。远程支持是可选的。要启用它,需要确保再打包的归档文件中包含 devtools,如下面清单所示:
org.springframework.boot spring-boot-maven-plugin false
然后,你需要设置 spring.devtools.remote.secret 属性,如下面示例所示:
spring.devtools.remote.secret=mysecret
警告:在远程应用上启用 spring-boot-devtools 是有安全风险的。你应当从不在生产部署上启用该支持。
远程 devtools 支持分为两部分:接受连接的服务器端端点,在 IDE 中运行的客户端应用程序。当设置 spring.devtools.remote.secret 属性时,服务器组件是自动启用的。客户端组件必须手动启动。
20.5.1、运行远程客户端应用程序
远程客户端应用程序设计为从 IDE 中运行。你需要使用与连接到的远程项目相同的类路径运行 org.springframework.boot.devtools.RemoteSpringApplication。应用程序的唯一必需参数是它连接到的远程 URL。
例如,如果你使用的是 Eclipse 或 STS, 并且有一个名为 my-app 的项目已部署到 Cloud Foundry,那么你将执行以下操作:
(1)从“运行”菜单选择“运行配置...”。(2)创建一个新的“Java 应用程序”“启动配置”。(3)浏览项目 my-app。(4)将 org.springframework.boot.devtools.RemoteSpringApplication 用作主类。(5)将 https://myapp.cfapps.io 添加到“程序参数”(或者你自己的远程 URL)。
正在运行的远程客户端可能类似于以下清单:
. ____ _ __ _ _/ / ___'_ __ _ _(_)_ __ __ _ ___ _ ( ( )___ | '_ | '_| | '_ / _` | | _ ___ _ __ ___| |_ ___ / ___)| |_)| | | | | || (_| []::::::[] / -_) ' / _ _/ -_) ) ) ) )' |____| .__|_| |_|_| |___, | |_|____|_|_|____/_____|/ / / /=========|_|==============|___/===================================/_/_/_/:: Spring Boot Remote :: 2.1.6.RELEASE2015-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 hierarchy2015-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 357292015-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)
注释:因为远程客户端使用与实际应用程序相同的类路径,所以它可以直接读取应用程序属性。这就是读取 spring.devtools.remote.secret 属性并将其传递给服务器进行身份验证的方式。
提示:建议始终使用 https:// 作为连接协议,以便对通信进行加密,并且不能截获密码。
提示:如果需要使用代理来访问远程应用,则请配置 spring.devtools.remote.proxy.host 和 spring.devtools.remote.proxy.port 属性。
20.5.2、远程更新
远程客户端以与本地重启相同的方式监视应用程序类路径的更改。任何已更新的资源都会被推送到远程应用并(如果需要)触发重启。如果你在一个使用非本地的云服务的功能上进行迭代,这是可能是有用的。通常,远程更新和重启要比完全重新构建和部署周期快得多。
注释:只有在远程客户端运行时才会监视文件。如果在启动远程客户端之前更改文件,则不会将其推送到远程服务器。
21、打包你的应用程序用于生产
可执行的 jar 可以用于生产部署。由于它们是自包含的,因此也非常适合于基于云的部署。
对于其它“生产就绪”的功能,例如:健康、审计和度量 REST 或 JMX 端点,请考虑添加 spring-boot-actuator。有关详情,请查看第五部分:Spring Boot Actuator:生产就绪功能。
22、接下来阅读什么
现在你应该了解如何使用 Spring Boot 以及应该遵循的一些最佳实践。现在,你可以继续深入了解特定的 Spring Boot 特性,也可以跳到后面,阅读 Spring Boot 的“生产就绪”方面的内容。(https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/production-ready.html)
上一篇[20、开发者工具(1)]
下一篇[23、SpringApplication(1)]