问题描述
我正在尝试使用带有数据库后端的 Spring Cloud Config 设置一个 Spring Boot 项目.我的设置中有以下内容:
I'm trying to setup a spring boot project with Spring Cloud Config with database backend. I've following things in my setup:
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
,我试图从我的数据库中注入一个值.
And I've @EnableConfigServer
in application class. Also I've @RefreshScope
in a controller where I'm trying to inject a value from my database.
@Value("${message}")
private String message;
而且我在 db 中有一个条目.
And I've an entry in db.
APPLICATION |PROFILE |LABEL |KEY |VALUE
my-services-api |production |latest |message |Some New Hello
为了刷新,我在 /actuator/refresh
中做一个 POST 并返回一个成功的 200.但是 message
字段的值仍然是一个来来自属性文件且未更新.
To refresh I'm doing a POST in /actuator/refresh
with empty body which returns a success 200. But the value of message
field is still the one coming from properties file and is not updated.
当我执行 GET /my-services-api/production/latest
时,我在响应中看到了新值,但 message
字段仍未刷新.我缺少任何配置吗?谢谢.
When I go a GET /my-services-api/production/latest
, I see the new value in the response but message
field is still not refreshed. Am I missing any configuration? Thanks.
推荐答案
我不得不更改配置以获得单独的服务器和客户端:
I had to change the configuration to have a separate server and client:
在服务器端,我在 application.properties 中有这个:
On the server side, I have this in 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:
On the client side, I've a bootstrap.properties with:
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 一起:
And application.properties with:
management.endpoints.web.exposure.include=refresh
为了刷新,我在客户端服务上对/actuator/refresh 进行了 POST 调用.
To refresh, I did a POST call to /actuator/refresh on the client service.
这篇关于带有数据库后端的 Spring Cloud 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!