spring-boot - 带有数据库后端的 Spring Cloud 配置
问题描述
我正在尝试使用带有数据库后端的 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
字段仍未刷新。我是否缺少任何配置?谢谢。
解决方案
我不得不更改配置以拥有单独的服务器和客户端:
在服务器端,我在 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=jdbc
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
logging.level.org.springframework.jdbc.core=TRACE
在客户端,我有一个 bootstrap.properties:
spring.application.name=my-services-api
spring.cloud.config.uri=http://localhost:8080
spring.cloud.config.label=latest
spring.cloud.config.profile=jdbc
和 application.properties:
management.endpoints.web.exposure.include=refresh
为了刷新,我在客户端服务上对 /actuator/refresh 进行了 POST 调用。
推荐阅读
- android - 以编程方式访问不同设备的屏幕截图目录路径
- python - 如何仅在嵌套模式中解析属性值?
- file - Could you tell me the difference between double slash paths and single slash paths in SAP?
- for-loop - 优化 Google Apps 脚本嵌套循环
- c++ - fftw:为什么我的 2D DFT 输出与每行的 1D DFT 输出不一样?
- google-apps-script - How to get a XML Download from API automatically in Google Sheets (for Data Studio later on)
- kubernetes - Helm-charts:在同级图表的部署/配置图中共享 env var
- android - Set screenOrientation to "portrait" in release build only
- sql - SQL - Get only data from 2 days ago
- react-native - React Native:如何在登录前隐藏底部栏并在用户登录后显示?