Implementing Configuration Versioning with Spring Cloud Config
Last Updated :
21 Jun, 2024
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 externalize configurations and supports the versioning through integration with version control systems like Git. This article will guide you through the process of implementing the configuration version using Spring Cloud Config of the Spring applications.
Prerequisites
- Basic understanding of the Spring Boot and Spring Cloud.
- The version control system is set like Git.
- Java Development Kit installed in your local system.
- Maven for the dependency management system.
Main Concept
Spring Cloud Config can provides the server and client-side supports for the externalized configuration in the distrubted system. With the Spring Cloud Config, we can manage the configurations in the central place and version it using the Git repository. This setup can allows the application to fetch their configuration from the server which can retrives it from the version-controlled repository.
Key Terminologies
- Configuration Repository: The central location where the configuration files are stored and managed of the application. In Spring Cloud Config, it can often the Git repository, the repository contains the configruation properties for the different applications and enviornments and it can enabling the version control and collaboration.
- Spring Cloud Config Server: The Spring Boot application that can provides the configuration properties to the client applications. It can acts as the middle between the configurations from the repository and serves them to the clients.
- Spring Cloud Config Client: The client applicatio that can be retrieves the configuration properties from Config Server. The Client application can be any Spring Boot application that needs the externalized configuration. It can fetches the configurations during the startup and can refresh them at runtime of the application.
- Git: The distributed system used to track the changes in the source code. In context of the Spring Cloud Config, Git can be used as the configuration repository to manage the configuration files, track changes and collaborate on the configuration management.
- Versioning: The process of the tracking and managing the changes to the configuration files. Versioning can allows the developers to maintain the history of configuration changes, roll back to the previous versions and understand the evolution of the configurations over time.
Implementing Configuration Versioning with Spring Cloud Config
Create the Git repository
We can create the git repository and add the application.properties file with the following content.
# application.properties
message=Hello from Config Server
Create 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: Configure the Application Properties
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
spring.cloud.config.server.git.uri=https://github.com/yourusername/config-repo
server.port=8888
Step 3: Main Class
Open the main class, add the @EnableConfigServer to activate the Spring cloud config functionality of the application.
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);
}
}
pom.xml
XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-server</name>
<description>config-server</description>
<properties>
<java.version>17</java.version>
<spring-cloud.version>2023.0.1</spring-cloud.version>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Step 4: Run the Application
Once the Spring project is completed and successfully runs as a Spring application, it will start at port 8888.

Create the Config-Client
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: Configure the Application properties
Open the application.properties file and add the configuring the server port and import git uri configuration
spring.application.name=config-client
spring.cloud.config.uri=http://localhost:8888
Step 3: Create the MesaageController class
Go to src > main > java > org.example.configclient > MessageController and put the below code.
Java
package org.example.configclient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MessageController {
@Value("${message}")
private String message;
@GetMapping("/message")
public String getMessage() {
return this.message;
}
}
Step 4: Main Class
Open the main class (No changes are required) and put the below code.
Java
package org.example.configclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
pom.xml
XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-client</name>
<description>config-client</description>
<properties>
<java.version>17</java.version>
<spring-cloud.version>2023.0.1</spring-cloud.version>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Step 5: Run the application
After the project completed, we will run this as a Spring Boot Application and it will start at port 8080.
.jpg)
Testing the Endpoints
GET http://localhost:8080/message

Update git application.properties file
Hello from Config Server - Version
commit and push the changes to Git repository
git add application.properties
git commit -m "Update configuration message to version 2"
git push
Again Endpoint Testing
GET http://localhost:8080/message

Conclusion
Implementing the configuration versioning with Spring Cloud Config allows you to manage the configuration centrally and leverage version control for the tracking changes of the application. This setup can enhances the maintainability and reliability of the microservices architecture. By the following the steps outlined above, we can easily set up and manage the configuration versioning in the Spring Cloud projects.
Similar Reads
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
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
Managing Configuration for Microservices with Spring Cloud Config
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 C
4 min read
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
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
Spring @Configuration Annotation with Example
The @Configuration annotation in Spring is one of the most important annotations. It indicates that a class contains @Bean definition methods, which the Spring container can process to generate Spring Beans for use in the application. This annotation is part of the Spring Core framework. Let's under
4 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
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
Implementing Secure API Communication in Spring Boot
In modern web applications, securing API communication is important to protect sensitive data and ensuring the integrity of interactions between clients and servers. In this article, we will learn how to implement secure API communication in a Spring Boot application. Securing API communication is c
5 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