Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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:

<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

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:

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

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

like image 712
Anzar Avatar asked Jul 22 '16 17:07

Anzar


2 Answers

There is some breaking changes with the new spring cloud module read more: here.

Bootstrap, provided by spring-cloud-commons, is no longer enabled by default. 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. These need to be set as an environment variable, java system property or a command line argument.

  • The other option is to include the new 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>
like image 183
Skillz Avatar answered Sep 22 '22 12:09

Skillz


I referred this post answered by 'spencergibb' and able to resolve the issue. I added "spring.config.name" in my bootstrap.yml file of client application and resolved the issue. Now, my bootstrap.yml look like as below:

spring:
  application:
    name: my-config-client
  cloud:
    services:
      registrationMethod: route
  config:
    name: configserver
    enabled: true
    uri: http://localhost:${config.port:8888}
like image 26
Anzar Avatar answered Sep 22 '22 12:09

Anzar