【发布时间】:2021-01-16 11:12:29
【问题描述】:
我正在尝试使用带有数据库后端的 Spring Cloud Config 设置一个 Spring Boot 项目。我的设置中有以下内容:
application.properties
spring.application.name=my-services-api
spring.datasource.url=jdbc:h2:~/test
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.platform=h2
spring.datasource.username=sa
spring.datasource.password=sa
spring.datasource.hikari.connection-timeout=5000
spring.datasource.hikari.maximum-pool-size=10
spring.profiles.active=jdbc
spring.cloud.config.uri=http://localhost:8080
spring.cloud.config.server.default-profile=production
spring.cloud.config.server.default-label=latest
spring.cloud.config.server.jdbc.sql=SELECT key, value FROM my_properties WHERE application=? AND profile=? AND label=?;
spring.cloud.config.server.jdbc.order=1
spring.h2.console.enabled=true
management.endpoints.web.exposure.include=refresh
message=default message
我在应用程序类中有@EnableConfigServer。此外,我在控制器中也有 @RefreshScope,我试图从我的数据库中注入一个值。
@Value("${message}")
private String message;
我在 db 中有一个条目。
APPLICATION |PROFILE |LABEL |KEY |VALUE
my-services-api |production |latest |message |Some New Hello
为了刷新,我在 /actuator/refresh 中使用空正文进行 POST,返回成功 200。但 message 字段的值仍然是来自属性文件的值,并且没有更新。
当我执行 GET /my-services-api/production/latest 时,我在响应中看到了新值,但 message 字段仍未刷新。我是否缺少任何配置?谢谢。
【问题讨论】:
-
你能分享
private String message类的代码吗? -
@RestController @RefreshScope public class MessageController { @Value("${message}") private String message; @RequestMapping("/message") String message() { return message; } } -
您是否尝试过此线程中的解决方案。 stackoverflow.com/questions/49311068/…
-
你也可以添加你的 pom.xml
标签: spring-boot spring-cloud-config spring-cloud-config-server