Using Git as a Backend for Spring Cloud Config Server
Last Updated :
21 Jun, 2024
Spring Cloud Config Server is a powerful tool and it provides server-side as well as client-side support for externalized configuration in a distributed system. It allows us to manage the configurations of multiple applications in a centralized way. By using Git as a backend, you can version the configuration files and manage them more effectively.
Prerequisites:
- Java 11 or higher version
- Maven dependency for project management
- Git installed in your local system
- Basic understanding of the Spring Boot and Spring Cloud
Spring Cloud Config Server can read the configuration properties from the Git repository and it provides them to the client applications. This setup ensures that the configuration changes are instantly available to all the client applications.
How Spring Cloud Config Works?
The Spring Cloud Config Server fetches the configuration properties from the variety of the sources, such as the Git repository, the file system, or even the database. These configuration properties are then the served to the client applications at the runtime. The clients refresh their configurations dynamically without requiring the restart.
Advantages of Using Git as a Backend for Config Server:
- Version Control: Git inherently supports the versioning, so every change in the configuration is tracked and can be reverted if necessary of the application.
- Collaboration: Multiple developers can work on the configuration files simultaneously, it can leveraging the Git's branching and merging the capabilities.
- Auditing: Git can provides the history of the changes, making it easy to see who made what change and when.
- Security: Git repositories can be secured using the SSH or HTTPS, it can ensuring that only authorized the users have access to the configurations.
Config Server Architecture
- Config Server: It acts as the central hub that can be provides the configuration properties to the client applications. It fetches the configuration from the Git repository.
- Git Repository: It stores the configuration files. Each application can have its own set of the properties and environments can have different configurations of the application.
- Config Client: It fetch their configuration properties from the Config Server. They can use the Spring Cloud Config dependency to the communicate with the Config Server.
Step-by-Step Flow:
- Setting Up the Git Repository: We can create the Git repository that contains the configuration files for the different applications and environments.
- Config Server Fetches Configurations: The Config Server can be configured to the point to Git repository. It fetches the configuration properties from this repository.
- Client Applications Retrieve Configurations: Client applications are configured to use the Config Server URL. On the startup, they can fetch their configuration properties from the Config Server.
- Dynamic Updates: If the configuration property changes in the Git repository, the Config Server can be notify the clients or the clients can periodically check for the updates and refresh their properties without the restarting.
Implementation of Using Git as a Backend for Spring Cloud Config Server
Below is the implementation for using Git as a Backend for Spring Cloud Config Server.
Create the Config-Server
Step 1: Create the Spring Project
Create the new Spring Boot project using Spring Initializr on creating the project add the following dependencies into the project.
Dependencies:
- Spring Web
- Spring Cloud Config Server
- Lombok
- Spring DevTools
Once create the project then the file structure looks like the below image.

Step 2: Application Properties
spring.application.name=config-server
spring.cloud.config.server.git.uri=https://github.com/yourusername/config-repo
server.port=8888
Step 3: Main Class
No changes are required in the main class.
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
The application will start at port 8888.

Create the Config-Client
Step 1: Create the Spring Project
Create the new Spring Boot project using Spring Initializr on creating the project add the following dependencies into the project.
Dependencies:
- Spring Web
- Spring Cloud Config Server
- Lombok
- Spring DevTools
Once create the project then the file structure looks like the below image.

Step 2: Application Properties
spring.application.name=config-client
spring.cloud.config.uri=http://localhost:8888
Step 3: Create the MessageController
We will now create the simple REST endpoint of the application.
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
No changes are required in the main class.
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
The application will start at port 8080.

Step 6: Testing the Endpoint
GET http://localhost:8080/message
Output:

Once update the git configuration file then it will updated the endpoint results also.
This Example project demonstrates using the Git as Backend for the Spring Cloud Config server of the Spring Boot project.
Similar Reads
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
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 an
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
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
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
Microsoft Azure - Using Azure Spring Cloud
Pre-requisite:- Azure VM In this article, we will learn how to get started with Microsoft Azure - Using Azure Spring Cloud. Azure Spring Cloud is a fully managed service that allows developers to build, deploy, and run Spring Boot applications on the Azure platform. It is designed to simplify the pr
3 min read
Spring Cloud - Server Side Load Balancer
Spring Cloud is a collection of projects like load balancing, service discovery, circuit breakers, routing, micro-proxy, etc will be given by Spring Cloud. So spring Cloud basically provides some of the common tools and techniques and projects to quickly develop some common patterns of the microserv
4 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
Spring Boot - Continuous Integration Using GitHub Actions
Let's say we have two or more developers who are merging code into a single repository. There can be issues in merging code from different developers. So the Continuous Integration tools help in solving not just that but many other things like: Run an automated build after successful merging (like b
3 min read
Configuring Multiple Spring Batch Jobs in a Spring Boot Application
Spring Batch serves as a robust framework within the Spring ecosystem, specifically tailored for managing batch processing tasks efficiently. It's designed to tackle big data jobs efficiently and comes with handy tools to make batch application development a breeze. In the context of a Spring Boot a
10 min read