Spring Cloud config server setup with Git

  • 4.0/5
  • 3916
  • Jul 20, 2024

In this article, we will see how to use the Spring Cloud Configuration server to place the configuration for microservices in a central configuration repository.

The Spring Cloud Configuration server supports a number of storage mechanisms to hold the configuration files: Git repositories, local filesystems, HashiCorp Vaults, JDBC databases, etc.

1) Create a GitHub repository

We will use a GitHub repository as a storage mechanism to hold our configuration files.

Let's create a public repository in GitHub.

Now add two configuration files, "order-service-development.yml" and "order-service-production.yml" to this repository and push the code.

order-service-development.yml

order-service-production.yml

2) Setup a config server

In this example, we will setup a Git-backed Spring Cloud Configuration server protected with username and password-based authentication.

2.1) Dependencies

A Spring Cloud Configuration server requires "spring-cloud-config-server", "spring-boot-starter-web" and "spring-boot-starter-security" dependencies added to the classpath

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <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>

2.2) Enable Config Server

The "@EnableConfigServer" annotation pulls in all the required setup to auto-configure the config server.

2.3) Properties

We need to configure a git repository URL that contains our version-controlled configuration content.

2.4) Run

Let's run the application, and the configuration should be available at:

% curl -u admin:admin123 http://localhost:8081/order-service/development/ | json_pp

{
   "label" : null,
   "name" : "order-service",
   "profiles" : [
      "development"
   ],
   "propertySources" : [
      {
         "name" : "https://github.com/nkchauhan003/test-config/order-service-development.yml",
         "source" : {
            "app.region.id" : 5000,
            "management.endpoints.health.show-details" : "always",
            "management.endpoints.web.exposure.include" : "*",
            "springdoc.swagger-ui.path" : "/swagger-ui.html"
         }
      }
   ],
   "state" : null,
   "version" : "7214339a264c67a6048cc6740714c0dce4bdb545"
}

3) Setup Client

Let's create a simple Spring Boot client application consisting of a REST controller with one GET method.

3.1) Dependencies

A simple spring-boot application with config-server's client capabilities should have "spring-cloud-starter-config", "spring-boot-starter-web" and "spring-boot-starter-security" dependencies added to the classpath.

        <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>

To use Swagger UI, we need to add an additional Maven dependency:

        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>${springdoc-openapi-ui.version}</version>
        </dependency>

3.2) Properties

We need to configure "spring.config.import", "spring.application.name", "spring.profiles.active", "spring.cloud.config.uri", "spring.cloud.config.username" and "spring.cloud.config.password".

3.3) Test

Now that everything is set, let's run the application and test our functionality with the help of: Swagger UI

Request

Response

This '"regionId": 5000' in the response is coming from "app.region.id" property defined in "order-service-development.yml" configuration file.

Source code: GitHub

Index
How to Implement PostgreSQL Full-Text Search with Spring Boot

15 min

Spring's transaction management with the @Transactional annotation

9 min

Spring Boot Rest APIs with PostgreSQL (Spring Boot + Rest APIs)

15 min

Caching in Spring Boot (@Cacheable, @CacheEvict & @CachePut)

21 min

Declarative REST Client in Spring Boot (Spring 6 HTTP Interface)

13 min

Profiling a Spring Boot application with Pyroscope

7 min

Service discovery in Spring Boot (Spring Cloud + Netflix Eureka)

9 min

Dockerize Spring Boot app and Push image to DockerHub (Spring Boot + DockerHub)

4 min

Creating a Jenkins Pipeline for Spring Boot application

2 min

Circuit Breaker Pattern in Microservices (Spring BOOT + Resilience4j)

4 min

Edge Server Pattern in Microservices (Spring Cloud Gateway)

7 min

Monitoring Microservices (Spring Boot + Micrometer + Prometheus + Grafana)

7 min

Spring Cloud config server setup with Git

8 min

Distributed Tracing in Microservices (Spring Cloud Sleuth + Zipkin)

9 min

Circuit Breaker Pattern with Resilience4J in a Spring Boot Application

24 min

Deploying Spring Boot microservices on Kubernetes Cluster

12 min

Reactive programming in Java with Project Reactor

50 min

Spring Reactive with PostgreSQL (Spring Boot WebFlux + PostgreSQL)

13 min

Spring Reactive, Thymeleaf Hello World (Spring Webflux + Thymeleaf + JS/CSS)

9 min

Problem JSON (application/problem+json) in Spring WebFlux

15 min

Spring Boot Login/Logout (Spring Security + MySql + Thymeleaf)

21 min

Securing Server-to-Server Communication with "Spring Boot" & "OAuth 2"

18 min

Sending Emails in Spring Boot via SMTP

7 min

How to create a basic Spring 6 project using Maven

5 min

Spring Boot, Thymeleaf Hello World (Spring Boot + Thymeleaf + JS/CSS)

9 min