gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:02:19 24 4
gpt4 key购买 nike

当我尝试运行 Spring Cloud Config 客户端时,我遇到以下问题:

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

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

谢谢

最佳答案

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

Bootstrap, provided by spring-cloud-commons, is no longer enabled bydefault. If your project requires it, it can be re-enabled byproperties or by a new starter.

  • To re-enable by properties set spring.cloud.bootstrap.enabled=true or spring.config.use-legacy-processing=true. These need to be set asan environment variable, java system property or a command lineargument.

  • The other option is to include the new 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>

关于maven - Spring Cloud Config 客户端未从配置服务器加载值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38532683/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com