Spring Cloud Config Server and Client to externalize configuration in a distributed system

Using Spring Cloud Config Server and Client to externalize configuration using GIT in a distributed system

Spring Cloud Config provides a way to externalize and centralize application configuration (application.yml|properties) in a central place. Also, It allows us to manage the configuration between environments as we moves through the deployment pipeline from dev to test and into production. The default implementation uses GIT to store configuration environments.

Spring Cloud Config Server Schema

In this example we will create a simple microservice that return a coefficient readed from application properties.

1 Git configuration

In order to externalize centralize data we need an external data source to save the configuration. Steps:

  1. Create a git repository
  2. Create a configuration file
  3. Commit and push the changes to the git
app.coefficient=40
app.coefficient=50

the name service1 is the name of your microservice that you will provide on bootstrap.properties

2 Spring cloud Config Server

To enable the spring cloud config server we use the annotation @EnableConfigServer plus setting the property git uri.

<!--Rest of the pom-->
	<properties>
		<spring-cloud.version>Greenwich.SR3</spring-cloud.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>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
<!--Rest of the pom-->
server.port=8888
spring.application.name=server-config
spring.cloud.config.server.git.uri=https://gitlab.com/yamicode/spring-cloud.git
package com.yamicode.cloud_config;

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

@SpringBootApplication
@EnableConfigServer
public class CloudConfigApplication {

	public static void main(String[] args) {
		SpringApplication.run(CloudConfigApplication.class, args);
	}

}

After running the app, go to http://host:port/service-name/profile (Example: http://localhost:8888/service1/default) to see the configuration of that microservice.

3 Spring cloud Config Client

In order to communicate to spring cloud config server we should add the spring cloud starter config dependency and provide some properties: application name and config server url and the type of profile.

spring.application.name=service1
spring.cloud.config.uri=http://localhost:8888
#spring.profiles.active=prod

server.port=8000
app.coefficient = 20
package com.yamicode.microservice1.rest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("calculator")
public class CalculatorRest {

    @Value("${app.coefficient}")
    private int coefficient;

    @GetMapping("coefficient")
    public Map sum(){
        Map result = new HashMap();
        result.put("coefficient", coefficient);
        return result;
    }
}

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

3 Result

Default profile

spring cloud config dev testing

Prod profile

spring cloud config prod testing