首页 > 解决方案 > 带有数据库后端的 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字段仍未刷新。我是否缺少任何配置?谢谢。

标签: spring-bootspring-cloud-configspring-cloud-config-server

解决方案


我不得不更改配置以拥有单独的服务器和客户端:

在服务器端,我在 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 调用。


推荐阅读