Set up Spring Cloud Config Server
Tags: spring, java, springboot, springcloud
Published: 2020-03-02 11:43:54

__Why we need Spring cloud Config Server?__
Before the microservices era, we used to create properties files where we maintained our configurable parameters so that if we changed the parameter values or added or deleted a parameter, we generally needed to restart the application container. 
Configuring a monolithic application is usually quite straightforward. You either set environment variables in the server where the application will be deployed or you embed one or more properties files along with the application code in the deployment.
Making configuration part of your deployment, however, presents some challenges:
1. Configuration may change more frequently
2. Configuration may need to be distinct across various environments
3. Configuration may contain information that application developers are restricted from seeing (e.g. passwords)

Moreover, in a microservice architecture, embedded configuration results in duplication of configuration details across all microservices. This makes it difficult to manage and change configuration, as it would require rebuilding and redeploying all microservices.

To eliminate this problem, Configuration details in a microservice architecture should be centralised and not carried in the deployment of each individual microservice. The advantage of Centralisation is that it can be shared, evolved, and versioned independently from the applications that rely on it. 
![spring cloud cofig](https://o7planning.org/en/11723/cache/images/i/15359236.png)
Let's Setup 

Create a new project via https://start.spring.io/ , import it on your IDE and add dependencies in pom.xml mentioned below.
{% gist https://gist.github.com/ajeetbal/394b275c146a209263dc35e3d18c0c78 %}
After adding all dependencies , go to project directory in terminal and run following command : __mvn clean install__ 

Add __@EnableConfigServer__ in main file. Spring Cloud Config Server provides an HTTP resource-based API for external configuration. The server is embeddable in a Spring Boot application, by using the @EnableConfigServer annotation.
{% gist https://gist.github.com/ajeetbal/7047afe311d2b9c62ad11c896c048af4 %}

Lets have one microservice: gateway. Now We will create a git repository where we will centralise all the configuration of our microservices.
we will have a configuration folder under this repository for demonstration purpose:
Now under configuration we can create multiple yaml config files for different environment like gateway-dev.yml , gateway-qa.yml etc.
{% gist https://gist.github.com/ajeetbal/db155af4566d23c11f9f021e572c239f %}

Now We will create two configuration file ,one for development environment and one for qa environment in src/main/resource folder.
{% gist https://gist.github.com/ajeetbal/1ca15787f924a85c2b087d9bd9693faf 
%}

We are using bootstrap prefix configuration file because it is loaded before other yml or properties file.
That's it , we have set up spring cloud config server.
Go to the browser. To verify config server is working fine , hit url (http://localhost:8888/{service-name}/{profile} )

http://localhost:8888/gateway/dev 
{
"name":"gateway",
"profiles":["dev"],
"label":null,
"version":"ea7c7a8b36be3f483c25df24342e0db1ee1486e9",
"state":null,
"propertySources":[]
}

http://localhost:8888/gateway/qa
{
"name":"gateway",
"profiles":["qa"],
"label":null,
"version":"ea7c7a8b36be3f483c25df24342e0db1ee1486e9",
"state":null,
"propertySources":[]
}

Now microservices should have to include spring-cloud-starter-config dependency in the pom.xml and we should have to point to the spring-cloud-server service in the microservice config file. 
{% gist https://gist.github.com/ajeetbal/dbfaecfe614bdca212340840125993f4
%}

You can change environment of your microservice anytime and cloud-config-server will provide you all the configuration respect to that environment.

Thank You
If you find this article helpful, please do let me know or you can ask your doubts regarding this article. 🙂