【问题标题】:Spring Cloud Config Database Backend - Cannot resolve propertySpring Cloud Config 数据库后端 - 无法解析属性
【发布时间】:2021-05-07 08:27:42
【问题描述】:

我需要通过 PostgreSQL 数据库存储和使用配置。我已向数据库输入以下查询

CREATE TABLE xlabs.properties (
  application varchar(200) DEFAULT NULL,
  profile varchar(200) DEFAULT NULL,
  label varchar(200) DEFAULT NULL,
  prop_key  varchar(500) DEFAULT NULL,
  value varchar(500) DEFAULT NULL,
  id SERIAL PRIMARY KEY
);

INSERT INTO xlabs.properties (application, profile, label, prop_key, value) VALUES ('config-server','development','latest','ssn','([0-9]{9})');

所以,在运行配置服务器并点击http://localhost:8090/config-server/development/latest 后将返回此响应。

{"name":"config-server","profiles":["development"],"label":"latest","version":null,"state":null,"propertySources":[{"name":"config-server-development","source":{"ssn":"([0-9]{9})"}}]}

但问题是当我尝试通过云配置客户端使用以下配置使用上述 ssn 属性时,bootstrap.properties

spring.cloud.config.uri=http://localhost:8090
spring.profiles.active=development
management.endpoints.web.exposure.include=*
spring.application.name=config-server
spring.cloud.config.label=latest

@RestController
@RefreshScope
public class ConfigController {
    @Value("${ssn}")
    private String name;

    @GetMapping("/message")
    public String returnConfig(){
        return this.name;
    }
}

所以,当我运行项目时,它会给出运行时错误

Could not resolve placeholder 'ssn' in value "${ssn}"

请帮忙。

【问题讨论】:

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


    【解决方案1】:

    经过几个小时的阅读,我找到了答案。 此问题与最新版本的 Spring Boot 一起出现。因此,默认情况下它们的引导属性被禁用。所以你必须通过添加以下依赖来启用它们。

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

    对于旧版本,这可能没有用。 谢谢。

    【讨论】:

      猜你喜欢
      • 2017-05-01
      • 2021-09-21
      • 2019-01-12
      • 2020-10-05
      • 2017-08-17
      • 2019-12-25
      • 1970-01-01
      • 2017-01-21
      • 2019-01-07
      相关资源
      最近更新 更多