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

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


<!--Rest of the pom-->
package com.yamicode.cloud_config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.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.


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;

public class CalculatorRest {

    private int coefficient;

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


3 Result

Default profile

spring cloud config dev testing

Prod profile

spring cloud config prod testing