[英]Spring Cloud Config client not loading the value from config server
I am facing below issue while I try to run my Spring Cloud Config Client:我在尝试运行 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
My dependencies in POM.xml are as below:我在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>
The bootstrap.yml bootstrap.yml
spring:
application:
name: my-config-client
cloud:
services:
registrationMethod: route
config:
enabled: true
uri: http://localhost:${config.port:8888}
The application.yml is as below: application.yml如下:
# HTTP Server
server:
port: 2222
# Spring properties
spring:
profiles:
active: dev
#Disable HTTP Basic Authentication
security:
basic:
enabled: false
The class where I am trying to access the property is as below:我试图访问该属性的类如下:
@RefreshScope
@Component
public class MyProperty {
@Value("${DATABASE_NAME}")
private String databaseName;
public String getDatabaseName() {
return databaseName;
}
}
My config server is running fine.我的配置服务器运行良好。 When I use this url on browser http://localhost:8888/configserver/dev , It gives the below result:当我在浏览器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"
}
}
]
}
I tried with all the posts who were facing this issue.我尝试了所有面临这个问题的帖子。 But, it is not working for me.但是,它对我不起作用。 May be, I am missing some points.可能是,我错过了一些要点。 If anybody can provide help, it would be great.如果有人可以提供帮助,那就太好了。
Thanks谢谢
There is some breaking changes with the new spring cloud module read more: here.新的 spring 云模块有一些重大变化阅读更多: 这里。
Bootstrap, provided by spring-cloud-commons, is no longer enabled by default.由 spring-cloud-commons 提供的 Bootstrap 默认不再启用。 If your project requires it, it can be re-enabled by properties or by a new starter.如果您的项目需要它,可以通过属性或新启动器重新启用它。
To re-enable by properties set spring.cloud.bootstrap.enabled=true or spring.config.use-legacy-processing=true.要通过设置 spring.cloud.bootstrap.enabled=true 或 spring.config.use-legacy-processing=true 的属性重新启用。 These need to be set as an environment variable, java system property or a command line argument.这些需要设置为环境变量、java 系统属性或命令行参数。
The other option is to include the new spring-cloud-starter-bootstrap另一种选择是包含新的 spring-cloud-starter-bootstrap
It worked for me by adding these dependencies:通过添加这些依赖项,它对我有用:
<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>
I referred this post answered by 'spencergibb' and able to resolve the issue.我提到了'spencergibb'回答的这篇帖子,并能够解决这个问题。 I added "spring.config.name" in my bootstrap.yml file of client application and resolved the issue.我在客户端应用程序的 bootstrap.yml 文件中添加了“spring.config.name”并解决了这个问题。 Now, my bootstrap.yml look like as below:现在,我的bootstrap.yml如下所示:
spring:
application:
name: my-config-client
cloud:
services:
registrationMethod: route
config:
name: configserver
enabled: true
uri: http://localhost:${config.port:8888}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.