Microservices
- Service discovery in Spring Boot (Spring Cloud + Netflix Eureka)
- Circuit Breaker Pattern in Microservices (Spring BOOT + Resilience4j)
- Docker commands every developer should know
- Edge Server Pattern in Microservices (Spring Cloud Gateway)
- Monitoring Microservices (Spring Boot + Micrometer + Prometheus + Grafana)
- Spring Cloud config server setup with Git
- Distributed Tracing in Microservices (Spring Cloud Sleuth + Zipkin)
- Deploying Spring Boot microservices on Kubernetes Cluster
Mar 31, 2024
In this article, we will see how to use the Spring Cloud Configuration server to place the configuration for microservices in a central configuration repository.
The Spring Cloud Configuration server supports a number of storage mechanisms to hold the configuration files: Git repositories, local filesystems, HashiCorp Vaults, JDBC databases, etc.
1) Create a GitHub repository
We will use a GitHub repository as a storage mechanism to hold our configuration files.
Let's create a public repository in GitHub.
Now add two configuration files, "order-service-development.yml" and "order-service-production.yml" to this repository and push the code.
order-service-development.yml
order-service-production.yml
2) Setup a config server
In this example, we will setup a Git-backed Spring Cloud Configuration server protected with username and password-based authentication.
2.1) Dependencies
A Spring Cloud Configuration server requires "spring-cloud-config-server", "spring-boot-starter-web" and "spring-boot-starter-security" dependencies added to the classpath
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
2.2) Enable Config Server
The "@EnableConfigServer" annotation pulls in all the required setup to auto-configure the config server.
2.3) Properties
We need to configure a git repository URL that contains our version-controlled configuration content.
2.4) Run
Let's run the application, and the configuration should be available at:
% curl -u admin:admin123 http://localhost:8081/order-service/development/ | json_pp { "label" : null, "name" : "order-service", "profiles" : [ "development" ], "propertySources" : [ { "name" : "https://github.com/nkchauhan003/test-config/order-service-development.yml", "source" : { "app.region.id" : 5000, "management.endpoints.health.show-details" : "always", "management.endpoints.web.exposure.include" : "*", "springdoc.swagger-ui.path" : "/swagger-ui.html" } } ], "state" : null, "version" : "7214339a264c67a6048cc6740714c0dce4bdb545" }
3) Setup Client
Let's create a simple Spring Boot client application consisting of a REST controller with one GET method.
3.1) Dependencies
A simple spring-boot application with config-server's client capabilities should have "spring-cloud-starter-config", "spring-boot-starter-web" and "spring-boot-starter-security" dependencies added to the classpath.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
To use Swagger UI, we need to add an additional Maven dependency:
<dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-ui</artifactId> <version>${springdoc-openapi-ui.version}</version> </dependency>
3.2) Properties
We need to configure "spring.config.import", "spring.application.name", "spring.profiles.active", "spring.cloud.config.uri", "spring.cloud.config.username" and "spring.cloud.config.password".
3.3) Test
Now that everything is set, let's run the application and test our functionality with the help of: Swagger UI
Request
Response
This '"regionId": 5000' in the response is coming from "app.region.id" property defined in "order-service-development.yml" configuration file.
Source code: GitHub
NK Chauhan
NK Chauhan is a Principal Software Engineer with one of the biggest E Commerce company in the World.
Chauhan has around 12 Yrs of experience with a focus on JVM based technologies and Big Data.
His hobbies include playing Cricket, Video Games and hanging with friends.