问题
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
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;
And I've an entry in db.
APPLICATION |PROFILE |LABEL |KEY |VALUE
my-services-api |production |latest |message |Some New Hello
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.
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.
回答1:
I had to change the configuration to have a separate server and client:
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
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
And application.properties with:
management.endpoints.web.exposure.include=refresh
To refresh, I did a POST call to /actuator/refresh on the client service.
来源:https://stackoverflow.com/questions/64143361/spring-cloud-config-with-database-backend