Open In App

Implementing Configuration Versioning with Spring Cloud Config

Last Updated : 21 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.

configserverfile

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.

configserverlogg-compressed

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:

configclientfile

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.

configclientlog-compressed-(1)

Testing the Endpoints

GET http://localhost:8080/message
cpost1

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
cpost2

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

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 Config Server is the central place where all the co
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 the applications. Monitoring and logging in the Spr
9 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 configurations for the various environments such as deve
4 min read
Securing Spring Cloud Config Server with Basic Authentication
Spring Cloud Config Server provides externalization for distributed systems. With the increasing importance of microservices, centrally managing configurations becomes crucial. Securing this configuration server is equally important to prevent unauthorized access. Basic authentication is a simple and effective way to secure a server, requiring user
4 min read
Spring Boot - Versioning a REST API
API Versioning is a defined process of making or managing changes to an API. These changes can be made transparently without interrupting the clients. When users have permission to decide whether they can conveniently upgrade to the latest version of API and clearly state the changes made is known as a Good API Versioning Strategy. When to Version
3 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. These services then perform the CRUD (Create, Retrieve
10 min read
Spring MVC context:annotation-config VS context:component-scan
In this article, we will learn about Spring MVC &lt;context:annotation-config&gt; vs &lt;context:component-scan&gt;. Activating all annotations existing in Java beans that have already been registered either by definition in an application context file or by component scanning is the primary function of annotation-config. The requirement for regist
3 min read
Setting Active Profile and Config Location from Command line in Spring Boot
In Spring Boot, Profiles and Configurations settings play an important role in managing the application behavior across the different environments. By default, Spring Boot looks for the application properties or YAML files in classpath to the configure the application. It is necessary to specify configuration settings or the active specific profile
4 min read
REST API Versioning in Java Microservices
REST API is widely used when it comes to communicating between two microservices. And thus maintaining it is again a major task. We also have to make sure that while developing new functionalities we don't interrupt the working functionalities because it may create a major problem in other services. Thus we have to do versioning of the API which ad
10 min read
Java Spring Boot Microservices - Develop API Gateway Using Spring Cloud Gateway
The API Gateway Pattern in some cases stands for “Backend for frontend”. It is basically the entry gate for taking entry into any application by an external source. The pattern is going on in a programmer’s mind while they are making the client’s application. It acts as a medium between the client applications and microservices. For example-Netflix
4 min read