Hello everyone,

Greetings today!

We'll look at the spring cloud config server today, which is used to externalise configuration in distributed environments.

Rather than storing properties individually at the server level of each instance or in application.properties, we can configure all properties in a version control system such as git/svn based on deployment environment such as local, dev, QA, and so on.

We'll need a spring cloud config server with access to git/svn for this, and any other applications that need to fetch properties from git/svn can be called spring cloud config clients.

In today's example, we'll be storing our properties in the GIT repository.

Now, go to Spring Initializr and create a basic project structure with the dependencies listed below, then import it into the IDE.


Next, we need to add git configuration in application properties.

spring.cloud.config.server.git.uri=https://github.com/TEST-USER-ABC/config-server

spring.application.name=config-server

server.port=8888

Following that, we must commit and push properties files in the git repository that have been configured in ConfigServer's application.properties for all applications.

In this example, we will create one application, book-management, and we are currently adding properties to book-management in a git repository for two profiles, dev and local.
Below are 2 files we are adding git.

book-management-dev.properties
book-management-local.properties

We have just added the following sample properties to the previously stated files and will now access book management. These properties have different values for both profiles.

book.mgt.property1=50

book.mgt.property2=150

We will add @EnableConfigServer and @SpringBootApplication to ConfigServerApplication.java.

Now run config server & after that you can access the properties at below urls.

http://localhost:8888/book-management/{profile} i.e 

http://localhost:8888/book-management/dev & http://localhost:8888/book-management/local

O/P

Now create a BookMgt project from Spring Initializr with the below dependency and import it into IDE.

Now we will add the following configuration server details to the application. properties to access git repository properties via config server

spring.application.name=book-management

spring.profiles.active=dev

server.port=8999

spring.config.import=optional:configserver:http://localhost:8888/

Note-File we have added in the git repository should have the below naming convention 

application-name-profile.properties  [application-name and profile of application which will be using these properties]

Now we will add a controller to verify we are able to access book.mgt.property1 from a git repository in BookMgt.

We will inject value using @Value("${book.mgt.property1}")

So now our both application is ready...Run config server first and then BookMgt and try to hit 

GET Endpoint localhost:8999/book/view and you will be able to see value in logs and responses.

Thanks

Enjoy your learning!


Another post you can refer to is

Caching In Spring Boot + Caching In Spring Rest API + Clear Cache In Spring Boot

Spring Microservices - Netflix Eureka Service Registration and Discovery 

Spring Cloud Bus-Refresh Config Changes With RabbitMQ

Spring Tutorial