Managing Configuration for Microservices with Spring Cloud Config
Last Updated :
20 May, 2024
Spring Cloud Config provides the centralized external configuration management system and it is designed to work well with modern microservices architectures. It is the part of larger Spring Config suite of the tools that aim to help the developers built the cloud-native applications.
Spring Cloud Config Server is the central place where all the configuration parameters of the applications are stored and maintained. It typically backend by the source control system like Git. It can serve the configuration across the multiple environments and applications.
Client microservices connect to the Config Server to fetch their configurations. This setup ensures all the configurations are centralized, version-controlled and maintained in the secure and efficient manner.
Key Terminologies:
- Config Server: A server in the Spring Cloud Config and its setup that can centralizes and manages all the configuration properties for the client applications.
- Config Client: The application that can fetches its the configuration from the Config Server using the Spring Cloud Config.
- @EnableConfigServer: This annotation can be used in Spring applications to designate them as the Config Server.
- Environment Repository: The storage system can be used by the Config Server to the hold and serve configuration properties and it is commonly backed by the Git repository.
- spring.config.import: The property in Spring Boot that can specifies the sources from which the application should import the additional configuration data of the Spring application.
Implementation of Managing Configuration for Microservices with Spring Cloud Config
Below are the implementations to manage configuration for Microservices with Spring Cloud Config.
Setup the Config Server
Step 1: Create the spring project using spring initializer and add the below required dependencies.
Dependencies:
- Spring Web
- Spring Dev Tools
- Lombok
- Spring Cloud Config
After creating the Spring project, the file structure will be like below image.

Step 2: Open the application.properties file and add the configuring the server port and git repository uri configuration of the application.
spring.application.name=config-server
server.port=8888
spring.cloud.config.server.git.uri=https://github.com/iammahesh123/spring-cloud-config-server.git
Git repository application.yml code:
user:
role: Admin
welcome:
message: Welcome to the Spring Cloud Config managed application!
Step 2: Open the main class, add the @EnableConfigServer to activate the Spring cloud config functionality of the application.
Go to src > main >java > org.example.configserver > ConfigServerApplication and put the below code.
Java
package org.example.configserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
Step 3: Run the application
Once the Spring project is completed and successfully runs as a Spring application, it will start at port 8888.

Setup the Client-application
Step 1: Create the spring project using spring initializer and add the below dependencies.
Dependencies:
- Spring Web
- Spring Dev Tools
- Lombok
- Spring Cloud Config Client
After creating the Spring project, the file structure will be like below:

Step 2: Open the application.properties file and add the configuring the server port and import git uri configuration
spring.application.name=client-application
spring.cloud.config.import=optional:configserver:http://localhost:8888
Step 3: Create the ConfigConsumer class.
Go to src > main >java > org.example.clientapplication > ConfigConsumer and put the below code.
Java
package org.example.clientapplication;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class ConfigConsumer {
@Value("${user.role}")
private String userRole;
@Value("${welcome.message}")
private String welcomeMessage;
// Getter methods
public String getUserRole() {
return userRole;
}
public String getWelcomeMessage() {
return welcomeMessage;
}
}
Step 4: Create the HomeController class.
Go to src > main >java > org.example.clientapplication > HomeController and put the below code.
Java
package org.example.clientapplication;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
@Value("${user.role}")
private String userRole;
@Value("${welcome.message}")
private String welcomeMessage;
@GetMapping("/config")
public String getConfig() {
return String.format("Role: %s, Message: %s", userRole, welcomeMessage);
}
}
Step 5: Open the main class (No changes are required) and put the below code.
Java
package org.example.clientapplication;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}
Step 6: Run the application
After the project completed, we will run this as a Spring Boot Application and it will start at port 8080.

Managing Configuration Testing API:
GET http://localhost:8080/config
Output:

This example demonstrates the implementation of the configuration for the microservices with Spring Cloud Config of the Spring Boot application.
Similar Reads
Microservices Communication with Apache Kafka in Spring Boot
Apache Kafka is a distributed streaming platform and can be widely used to create real-time data pipelines and streaming applications. It can publish and subscribe to records in progress, save these records in an error-free manner, and handle floating records as they arrive. Combined with Spring Boo
6 min read
Creating Profiles in Spring Cloud Config Server
Spring Cloud Config provides both server-side and client-side support for external systems in a distributed system. Using the Config Server, we can manage the configuration for multiple applications from the central place. Profiles in the Spring Cloud Config can allow you to provide different config
4 min read
Spring Boot Microservices Communication using FeignClient with Example
FeignClient also known as Spring Cloud OpenFeign is a Declarative REST Client in Spring Boot Web Application. But what do you mean by Declarative REST Client? It means we need to specify the client specification as an Interface and Spring Boot will take care of the implementation for us. Writing web
15 min read
Why Use Spring Cloud for Microservices Development?
Microservices are small, loosely coupled distributed services. Microservices architecture evolved as a solution to the scalability, independently deployable, and innovation challenges with Monolithic Architecture. It provides us to take a big application and break it into efficiently manageable smal
9 min read
Serverless Functions with Spring Cloud Function
Serverless computing in Spring Boot is popular because of its simplicity and low cost. Working serverless allows us to focus on writing code without worrying about managing infrastructure. Spring Cloud Function is a framework that can enable serverless services using the Spring ecosystem. It allows
4 min read
Monitoring and Logging in Spring Cloud Config Server
In Microservices, Spring Cloud Server can play a crucial role in Spring microservices by providing centralized configuration management. It helps externalize configuration properties across all environments for applications, making them easier to manage and update without redeploying or restarting t
9 min read
Introduction to Messaging Queues in Spring Boot Microservices
In Microservices architecture, communication between the services is essential for achieving loose coupling and scalability. Messaging queues provide a reliable and asynchronous communication mechanism that can enable the seamless interaction between the microservices. Spring boot is a popular frame
8 min read
Spring Boot - Cloud Configuration Server
In microservices, we have several different services responsible for different functionalities. These services act like smaller application modules, which together form the application. Each of these modules has its own responsibility, based on the business logic of the application being built. Thes
10 min read
API Composition and Aggregation with Spring Cloud Gateway in Java Microservices
API Composition and Aggregation is the critical pattern in the microservices architecture. It can enable combining the data from multiple microservices into a single response which is essential for reducing the number of client-side requests and improving the overall efficiency of the data retrieval
9 min read
Load Balancing in Spring Boot Microservices
Load balancing is an important concept in distributed systems, especially in microservice environments. As enterprises increasingly adopt cloud-native technologies, application models require complex load-balancing strategies to efficiently deliver requests to customers This ensures high availabilit
5 min read