【发布时间】:2017-03-22 21:22:54
【问题描述】:
我有一个小的 Spring Boot Web 应用程序(可执行 jar),它在 application.properties 文件中有一些自定义属性。 我的目标是能够在运行时动态更改这些属性,而无需运行构建/部署或重新启动 java 进程。 spring-cloud-starter-config 项目似乎很适合这个(即使我没有使用配置服务器)但我遇到了以下问题:
在我的 pom.xml 中,我包含对 spring-boot-starter-actuator 和 spring-cloud-starter-config 的依赖项。 application.properties 配置以下属性
management.port=8081
management.context-path=/management
management.security.enabled=false
foo=bar
我可以使用
读取变量curl http://localhost:8081/management/env/foo
{"foo":"bar"}
下面的更新似乎也成功了
curl -X POST -d foo=foo http://localhost:8081/management/env
{"foo":"foo"}
当我查询整个环境时,我看到管理器部分中的更改值和 applicationConfig 中的原始值
curl http://localhost:8081/management/env
{
...
"manager": {
"foo": "foo"
},
...
"configServerClient": {
"spring.cloud.config.enabled": "false"
},
...
"applicationConfig: [classpath:/application.properties]": {
...
"foo": "bar",
...
}
}
现在当我再次查询变量时,我仍然得到旧值
curl http://localhost:8081/management/env/foo
{"foo":"bar"}
这与我在一些网络博客上看到的相反。据我了解,更改后的值应该会显示出来。 我究竟做错了什么?有没有更好的方法可以在不重新启动服务器的情况下动态更改 application.properties 中的值?
非常感谢您的帮助。
【问题讨论】:
-
你得到的输出是什么?
标签: spring spring-boot spring-boot-actuator spring-cloud-config