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

mklgxw1f  于 2022-11-29  发布在  Spring
关注(0)|答案(2)|浏览(181)

我在尝试运行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>

引导程序.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上使用此URL时,它会给出以下结果:

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

我试过了所有的帖子,但都不适合我。可能是我遗漏了一些要点。如果有人能提供帮助,那就太好了。
谢谢

jk9hmnmh

jk9hmnmh1#

新的Spring Cloud模块有一些突破性的变化阅读更多:给你。
由spring-cloud-commons提供的Bootstrap在默认情况下不再启用。如果您的项目需要它,可以通过属性或新手重新启用它。

  • 要通过属性重新启用,请设置spring.cloud.bootstrap.enabled=true或spring.config.use-legacy-processing=true。需要将这些设置为环境变量、java系统属性或命令行参数。
  • 另一个选项是包含新的spring-cloud-starter引导程序

它通过添加以下依赖项来帮助我:
第一个

dwthyt8l

dwthyt8l2#

我引用了由“spencergibb”回答的this post,并能够解决这个问题。我spring.config.name在客户端应用程序的bootstrap.yml文件中添加了**”www.example.com“,并解决了这个问题。现在,我的bootstrap.yml**看起来如下所示:

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

相关问题