spring - 如何从 Spring Cloud 客户端服务器使用 Spring Cloud 配置服务器 Jdbc 后端配置?
问题描述
我浏览了很多关于此的教程,但无法完成。
这是我的表结构。
Application Profile Label prop_key value
masking dev latest test-property message
我有一个应该与 JDBC 后端集成的云配置服务器。这是我application.properties
的配置服务器
server.port=8082
spring.application.name=masking
management.endpoints.web.exposure.include=*
spring.datasource.url=jdbc:postgresql://localhost:8000/finos?currentSchema=xlabs
spring.datasource.username=mufgdev
spring.datasource.password=XXX
spring.profiles.active=XXX
spring.cloud.config.server.jdbc.sql=SELECT prop_key,value from xlabs.properties where application=? and profile=? and label=?
spring.cloud.config.server.jdbc.order=1
使用此配置,如果我输入http://localhost:8082/masking/dev/latest
响应将显示我想要的结果。
我想在客户端使用以下配置的属性bootstrap.properties
spring.application.name=masking
spring.cloud.config.uri=http://localhost:8082
spring.cloud.config.label=latest
spring.cloud.config.profile=dev
在我的客户端
@RestController
@RefreshScope
public class TestController {
@Value("${test-property}")
private String aConf;
@GetMapping("/message")
String message() {
String name =aConf ;
return name;
}
}
这给java.lang.IllegalArgumentException: Could not resolve placeholder 'test-property' in value "${test-property}"
任何人都可以对此发表评论吗?
谢谢。
解决方案
此问题与最新的 Spring boot 版本一起出现,所有上述代码段步骤都可以,但默认情况下 Spring 已禁用 bootstrap。所以你必须通过添加来启用它们
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
不需要为旧版本的 Spring boot 项目添加。
推荐阅读
- mysql - 如何更改 RDS MYSQL DB 上的“被视为字母的字符”列表?
- apache - 如何在使用 Apache Camel 和 Spring Boot 修改后将批量记录插入另一个表
- signalr - Visual Studio 负载测试中的 Signalr 连接问题
- c# - 如何在 MVC 中路由到 url.com/ControllerA/ControllerB/ControllerB 的操作
- angular - 与谷歌日历 API 同步
- activerecord - Yii2 over partition 到 activerecord
- java - Struts2 Iterator Sort List 倒序排列
- php - 通过数组中的路径更改json中的值
- javascript - 如何在javascript中使用paremt post消息关闭Iframe
- reactjs - 如何使用现有的登录 Chrome 用户配置文件正确设置 VS Code 以在 chrome 中调试?