Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

microservice doesn't fetch properties from spring-cloud-config-server microservice

I want to build a simple microservices app with spring cloud config server. I am using Github repository to store my configuration files. Currently, I have two very simple microservices. One of them is a cloud-config-server which retrieve properties from the remote repo on Github. In the main class of this microservice, I added @EnableConfigServer annotation and I provided a couple of properties in the application.properties file:

spring.application.name=cloud-config-server
server.port=8888

spring.cloud.config.server.git.uri=*Remote repo URI*
spring.cloud.config.server.git.username=*Username*
spring.cloud.config.server.git.password=*Password*
spring.cloud.config.server.git.clone-on-start=true

If I go to http://localhost:8888/cloud-config-server/default everything works well, my spring cloud config server retrieves properties successfully.

In my second microservice, I added bootstrap.properties with 2 properties to connect to the cloud-config-server:

spring.cloud.config.uri=http://localhost:8888
spring.cloud.config.name=cloud-config-server

And I also added this dependency

       <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
       </dependency>

Unfortunately, my microservice doesn't retrieve properties from cloud-config-server. When I try to use property via @Value annotation I get an error:

Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'test.text' in value "${test.text}

I am sure, I didn't make a typo in my property name "test.text".

like image 560
dulikkk Avatar asked Nov 17 '20 18:11

dulikkk


People also ask

How do I create a microservice in Spring Boot?

Create a new folder where you will hold your config file and do a git init on that folder. now create the property file that you will map to your config server. We will be naming our microservice : microservice-one File Name : miroservice-one.properties Now add and commit your changes. Else the spring boot app will not read it

How to implement config server in microservices?

Typically, there are two ways to implement Config Server in Microservices. In this approach, we use remote repository to accommodate our common config file such as Github, Gitlab, Bitbucket etc. This approach is used in production environment.

What is a Spring Cloud config server?

A Spring Cloud config server is one of the more popular centralized configuration servers used in a microservice-based application. This is especially true with the increasing trend of Java developers building their application using Spring Boot; providing little to no effort of work to integrate their application to Spring Cloud config server.

What is the default value of searchlocations in Spring Cloud config?

As per documentation of Spring Cloud Config: "The default value of the searchLocations is identical to a local Spring Boot application (...) This does not expose the application.properties from the server to all clients, because any property sources present in the server are removed before being sent to the client."


2 Answers

I've already solved my problem. Bootstrap.properties file is no longer enabled by default. You can enable it by adding new dependency:

       <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>

or alternatively, you can remove bootstrap.properties file and provide these properties in your application.properties file

spring.config.import=configserver:*URI to your cloud config server*
spring.cloud.config.name=*Your cloud config server name*
like image 124
dulikkk Avatar answered Oct 21 '22 11:10

dulikkk


In Client application.properties you must use not the server name, but client application name

spring.cloud.config.uri=http://localhost:8888
spring.cloud.config.name=applicationname
like image 1
RUSLAN SIMAKOV Avatar answered Oct 21 '22 12:10

RUSLAN SIMAKOV