Spring Cloud Config客户端未从配置服务器加载值

时间:2016-07-22 17:58:08

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

当我尝试运行Spring Cloud Config Client时,我面临以下问题:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'DATABASE_NAME' in string value "${DATABASE_NAME}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204

我在 POM.xml 中的依赖关系如下:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencyManagement>
    <dependencies>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config</artifactId>
            <version>1.1.1.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Brixton.SR3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

    </dependencies>
</dependencyManagement>

<dependencies>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

</dependencies>

bootstrap.yml

spring:
application:
name: my-config-client
cloud:
services:
  registrationMethod: route
config:
  enabled: true
  uri: http://localhost:${config.port:8888}

application.yml 如下:

# HTTP Server
server:
port: 2222

# Spring properties
spring:
  profiles: 
    active: dev

#Disable HTTP Basic Authentication
security:
  basic:
    enabled: false

我尝试访问该属性的类如下:

@RefreshScope
@Component
public class MyProperty {

    @Value("${DATABASE_NAME}")
    private String databaseName;


    public String getDatabaseName() {
        return databaseName;
    }
}

我的配置服务器运行正常。当我在浏览器http://localhost:8888/configserver/dev上使用此网址时,它会给出以下结果:

{  
   "name":"configserver",
   "profiles":[  
      "dev"
   ],
   "label":"master",
   "version":"c991526a93fb776e37e18e138c7485d894d6ea4f",
   "propertySources":[  
      {  
         "name":"https://onestash.abc.com/scm/kapmol/microservice-config-repo.git/configserver.properties",
         "source":{  
            "DATABASE_NAME":"ABC",
            "CONVERT_USERS":"Y",
            "LRDS_JNDI_NAME":"jdbc/tds_new"
         }
      }
   ]
}

我尝试了所有面临此问题的帖子。但是,它对我不起作用。可能是,我错过了一些观点。如果有人可以提供帮助,那就太好了。

由于

2 个答案:

答案 0 :(得分:9)

新的 Spring Cloud 模块有一些重大变化,请阅读更多内容:here.

<块引用>

由 spring-cloud-commons 提供的 Bootstrap 不再由 默认。如果您的项目需要它,可以通过以下方式重新启用它 属性或由新的启动器。

  • 要通过设置 spring.cloud.bootstrap.enabled=true 或 spring.config.use-legacy-processing=true 的属性重新启用。这些需要设置为 环境变量、java 系统属性或命令行 论证。

  • 另一个选项是包含新的 spring-cloud-starter-bootstrap

通过添加这些依赖项对我有用:

<parent>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-parent</artifactId>
  <version>2020.0.0</version>
  <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
   <dependencies>
     <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-dependencies</artifactId>
       <version>${spring-cloud.version}</version>
       <type>pom</type>
       <scope>import</scope>
       </dependency>
     </dependencies>
</dependencyManagement>
    <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-bootstrap</artifactId>
    </dependency>
       <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-config</artifactId>
     </dependency>

答案 1 :(得分:1)

我提到this post由#spencergibb&#39;回答。并能够解决问题。我在客户端应用程序的bootstrap.yml文件中添加了&#34; spring.config.name&#34; 并解决了问题。现在,我的 bootstrap.yml 如下所示:

spring:
  application:
    name: my-config-client
  cloud:
    services:
      registrationMethod: route
  config:
    name: configserver
    enabled: true
    uri: http://localhost:${config.port:8888}