Close

Spring Cloud - Spring Cloud Config with Git Backend

[Updated: Jan 29, 2019, Created: Jan 28, 2019]

In the last tutorial we saw basics of Spring cloud config and how to use cloud config with file system backend. Following tutorial will show how to use Cloud config server with Git backend.

By default Spring Cloud Config uses a Git backend, that means on the config server side, we don't have to activate any profile.

To specify git repository location we need to use following property in application.properties:

  • spring.cloud.config.server.git.uri=<gitLocation>

where gitLocation can be local (e.g. file:///D:/example-app-config) or remote (e.g. http://example-git-config-repo).

At the specified location we need to add property files and commit them to Git before the clients can access them.

Example

Creating Git repository

Let's first create a local git repository (we are using Git bash for windows):

Joe@jpc MINGW64 /d
$ mkdir example-app-config

Joe@jpc MINGW64 /d
$ cd example-app-config/

Joe@jpc MINGW64 /d/example-app-config
$ git init
Initialized empty Git repository in D:/example-app-config/.git/

Create following files in D:/example-app-config

test-dev.properties:

test.greeting=Hi developer!

test2-dev.properties:

test.msg=How is your coding going?

test-prod.properties:

test.greeting=Hi there!

test2-prod.properties:

test.msg=How are you doing?

Commit above files:

Joe@jpc MINGW64 /d/example-app-config (master)
$ ls -la
total 48
drwxr-xr-x 1 Joe 197610  0 Jan 29 00:18 ./
drwxr-xr-x 1 Joe 197610  0 Jan 29 00:09 ../
drwxr-xr-x 1 Joe 197610  0 Jan 29 00:17 .git/
-rw-r--r-- 1 Joe 197610 34 Oct 16 16:26 test2-dev.properties
-rw-r--r-- 1 Joe 197610 27 Oct 15 15:41 test2-prod.properties
-rw-r--r-- 1 Joe 197610 27 Oct 16 15:07 test-dev.properties
-rw-r--r-- 1 Joe 197610 23 Oct 15 15:41 test-prod.properties

Joe@jpc MINGW64 /d/example-app-config (master)
$ git add -A

Joe@jpc MINGW64 /d/example-app-config (master)
$ git commit -m "adding props"
[master (root-commit) 8755c6e] adding props
 4 files changed, 4 insertions(+)
 create mode 100644 test-dev.properties
 create mode 100644 test-prod.properties
 create mode 100644 test2-dev.properties
 create mode 100644 test2-prod.properties

Cloud Config Server

pom.xml

<project .....>
<modelVersion>4.0.0</modelVersion>
<groupId>com.logicbig.example</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/>
</parent>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

src/main/resources/application.properties

server.port=7777
spring.cloud.config.server.git.uri=file:///D:/example-app-config

Main class

spring-cloud-config-server/src/main/java/com/logicbig/example/ConfigServerApplication.java

package com.logicbig.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
  public static void main(String[] args) {
      SpringApplication.run(ConfigServerApplication.class, args);
  }
}

Running config server

To try examples, run spring-boot maven plugin (configured in pom.xml of example project below):

mvn spring-boot:run

Or run the main class from IDE.

Now we can access the backend via http://localhost:7777/{application}/{profile}

For example http://localhost:7777/test/dev

http://localhost:7777/test2/prod

Cloud Config Client

pom.xml

<project .....>
<modelVersion>4.0.0</modelVersion>
<groupId>com.logicbig.example</groupId>
<artifactId>spring-cloud-config-client</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>

src/main/resources/bootstrap.properties

spring.cloud.config.uri=http://localhost:7777
spring.application.name=test,test2
spring.profiles.active=dev

An Example Bean

spring-cloud-config-client/src/main/java/com/logicbig/example/ClientBean.java

package com.logicbig.example;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;

@Component
public class ClientBean {
  @Value("${test.greeting}")
  private String msg1;

  @Value("${test.msg}")
  private String msg2;

  @PostConstruct
  public void postConstruct() {
      System.out.println(msg1);
      System.out.println(msg2);
  }
}

Boot main class

spring-cloud-config-client/src/main/java/com/logicbig/example/ConfigClientApplication.java

package com.logicbig.example;

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);
  }
}

Output

Running boot main class:

Hi developer!
How is your coding going?

If we change the active profile property to spring.profiles.active=prod (in bootstrap.properties) and run the client again:

Hi there!
How are you doing?

Example Project

Dependencies and Technologies Used:

  • Spring Boot 2.1.2.RELEASE
    Corresponding Spring Version 5.1.4.RELEASE
  • Spring Cloud Finchley.SR2
  • spring-cloud-config-client 2.0.2.RELEASE: This project is a Spring configuration client.
  • spring-cloud-config-server 2.0.2.RELEASE: spring-cloud-config-server.
  • JDK 1.8
  • Maven 3.5.4

Spring Cloud Config with GIT Backend Select All Download
  • spring-cloud-config-with-git-backend
    • spring-cloud-config-client
      • pom.xml
      • spring-cloud-config-server

    See Also