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:
- Configuration may change more frequently
- Configuration may need to be distinct across various environments
- 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.
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.
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.
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.
Now We will create two configuration file ,one for development environment and one for qa environment in src/main/resource folder.
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.
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. π
Top comments (0)