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}

应用程序.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;
    }
}

我的配置服务器运行良好。当我在浏览器上使用此url时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"
         }
      }
   ]
}

我试过所有面临这个问题的帖子。但是,这对我不起作用。也许,我遗漏了一些要点。如果有人能提供帮助,那就太好了。

感谢

新的spring-cloud模块有一些突破性的变化。

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

  • 要按属性重新启用,请设置spring.cloud.bootstrap.enabled=true或spring.config.使用legacyprocessing=true。这些需要设置为环境变量、java系统属性或命令行论点

  • 另一种选择是包括新的春季云启动器引导

它通过添加以下依赖项对我有效:

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

我引用了这篇由"spencergib"回答的帖子,并解决了这个问题。我在客户端应用程序的bootstrap.yml文件中添加了"spring.config.name"并解决了问题。现在,我的bootstrap.yml如下所示:

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

相关内容

  • 没有找到相关文章

最新更新