Spring Cloud Config — Part 2

Ritesh Panigrahi
4 min readJul 24, 2021

--

In the previous article, we have seen a basic understanding of the spring cloud config server and client.

Here, we will cover all that knowledge and create a git-backed config server and a client which uses the config server to get the properties from the git repository.

Config Server Implementation

To make a spring boot app act as a spring cloud config server we just need 2 dependencies in our pom spring-cloud-config-server and spring-boot-starter-web

Dependencies for config-server

Now that we have added the dependencies, the next step is to inform the config-server from where it should get the properties for any microservice, so let's configure that in the application. properties of the config server.

application.properties for config-server

So, according to the application. properties the config-server will run at port 8888.

As we have discussed in the previous article that the config server will be git-backed. So I have created another git repository that will store all the properties files for each service.

This repository information is given to the config server at line number 3.

In line 4, we have mentioned the branch from where to get the properties (By default spring cloud config server uses master branch)

Now the third step is to use @EnableConfigServer annotation in the main class which makes your Spring Boot application act as a Configuration Server.

Now we can start the config-service application.

Before going further let us look at the git repository.

all-services-config repository

As we can see it contains 2 properties file — message-service. properties and message-service-dev. properties.

message-service is the name of the client application which will ask the config-server about the properties(we will create this further), then the config-server will look into this repository and return all the properties inside the message-service. properties file when the profile is default and it will return properties from message-service-dev. properties when the profile in the client application is dev.

How the config-server will get the properties from git?

After starting the config service, just hit

http://localhost:8888/message-service/default

Template

http://{hostname}:{port of config server}/{client-application-name}/{env}

To get properties of dev environment, in place of default use dev.

Now that we have created a config-server that is retrieving all the properties from the git repository. Let us create a client application now.

Client Implementation

For the client application, we will create a simple spring-boot app that will return a greeting message whenever we hit the “/message” endpoint, and this message we will get from the properties file.

So, we need 2 dependencies spring-cloud-starter-config (required to get properties using config-server) and the spring-boot-starter-web

application. properties of message-service(client application)

First, let us check how this application works without a config-service by making cloud-config check false at line 3.

Also, we need to create a controller with a REST endpoint /message to get the message from the “greeting. message” property using @Value annotation.

Now when we start the message-service application and access the /message endpoint, we will see the message Good Morning which comes from the application. properties of message-service

Getting the value from the message-service application.properties

Now we will enable the config service by updating the application. properties of message-service (client) giving the hostname and port of the config server

message-service. properties from the git repository which the config-service will return to message-service

Now, if we access the /message endpoint.

The value coming from message-service. properties with help of config-server

As no active profile was set in message-service, we got the value from the default property file(message-service. properties)

If we set the profile of to dev by adding the below line in the application. properties of message-service

spring.profiles.active=dev

We will get the value of the “greeting. message” from the message-service-dev. properties file through the config server

The value coming from message-service-dev. properties with help of config-server

So in this way we have properties file for multiple services and also for multiple environments for any service.

If we want to update the properties of a client then there is no need to make any changes in the client application instead, we can make the changes in the specific properties file in the git repository which the config server refers.

--

--