首页 > 解决方案 > 如何从 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}"

任何人都可以对此发表评论吗?

谢谢。

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

解决方案


此问题与最新的 Spring boot 版本一起出现,所有上述代码段步骤都可以,但默认情况下 Spring 已禁用 bootstrap。所以你必须通过添加来启用它们

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

不需要为旧版本的 Spring boot 项目添加。


推荐阅读