【问题标题】:Spring cloud config with database backend带有数据库后端的 Spring Cloud 配置
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

  • 嗨,我试过这种方式,但我的客户说无法解析属性消息。你能帮帮我吗?
猜你喜欢
  • 2015-01-30
  • 2021-09-21
  • 2018-05-12
  • 2019-07-22
  • 2019-09-24
  • 1970-01-01
  • 1970-01-01
  • 2018-07-18
  • 2019-06-30
相关资源
最近更新 更多