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
Customizing the Configuration Location in Spring Cloud Config Server
Spring Cloud Config provides both server and client-side support for externalized configuration in distributed systems. The Config Server manages configuration properties for applications across various environments, allowing them to retrieve their configuration from a central location. By default,
5 min read
Using Native Configuration in Spring Cloud Config Server
Spring Cloud Config provides both server-side and client-side support for externalized configuration in distributed systems. The Config Server centralizes management of configuration properties for applications across various environments. By default, Spring Cloud Config uses Git for storage, but it
5 min read
Dynamic Configuration Updates with Spring Cloud Config
In a microservices architecture, managing configuration properties across multiple services can become challenging, especially when these configurations need to be updated frequently. Spring Cloud Config provides a centralized configuration management solution, allowing you to manage external proper
6 min read
Implementing Configuration Versioning with Spring Cloud Config
Configuration versioning is the critical aspect of maintaining and managing the configurations in the microservices architecture. It can allow the teams to track the changes, revert to previous versions and manage the configurations more efficiently, Spring Cloud Config can provide a powerful way to
5 min read
Encrypting Sensitive Configuration Data in Spring Cloud Config
Encrypting sensitive configuration data in Spring Cloud Config is essential for securing information like passwords, API keys, and other credentials. This extra layer of protection is crucial because it helps prevent unauthorized access and ensures that sensitive data remains safe, even if the confi
4 min read
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
Configuring Spring MVC with XML and Java Config
Spring MVC (Model-View-Controller) is a web framework within the Spring Framework that enables the development of web applications following the MVC design pattern. It separates application logic into three components: Model, View, and Controller, making it easier to maintain, test, and scale.Spring
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 â Managing Application Properties with Profiles
In Spring Boot, managing application properties is crucial for configuring the application based on different environments (e.g., development, testing, production). Spring Boot provides a feature called Profiles to help with this. Profiles allow you to define different sets of properties for various
6 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