如何从 Spring Cloud 客户端服务器使用 Spring Cloud 配置服务器 Jdbc 后端配置?

时间:2021-02-02 12:55:27

标签: spring spring-boot spring-cloud-config spring-cloud-config-server

我阅读了很多关于此的教程,但无法完成。

这是我的表结构。

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}"

有人可以对此发表评论吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

这个问题是最新的 Spring boot 发布的,上面所有的代码段步骤都没有问题,但是默认情况下 Spring 禁用了 bootstrap。所以你必须通过添加

来启用它们
<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

无需添加旧版本的 Spring Boot 项目。