DEV Community


Introduction to Spring Cloud Config

Java developer
・4 min read

In my previous article I have mentioned four products/applications for managing configuration in microservices architecture. In this article we will explain the first option (Spring Cloud Config)

What is spring cloud config

Spring cloud config is part of spring cloud tool set for developing cloud native application and microservices. It provides server and client support for externalize configurations in a distributed systems.

Spring cloud server

In much simpler words, spring cloud config is a normal spring project that reads configurations from a centralized configuration repository and provides these configuration to other application (microservices).

Setting up the config server.

Getting spring cloud config server is easy as much as creating any other spring project.

  • Go to
  • Set the project name (Artifact) to config-server and in the dependencies section add config server.
  • Download the zipped file (
  • Extract the project $ unzip
  • In the ConfigServerApplication class add @EnableConfigServer annotation.
package com.example.configserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class ConfigServerApplication {

    public static void main(String[] args) {, args);
Enter fullscreen mode Exit fullscreen mode
  • Execute the following commands to create a local git repository. This git repository will be used by the spring config server as the source of configuration for the other service.
$mkdir config-repo
$cd config-repo
$echo "" >
$echo "" >
$echo "" >
$git init
$git add .
$git commit -m"Adding configurations"
Enter fullscreen mode Exit fullscreen mode
  • Add the following properties to the file.${user.home}/config-repo
Enter fullscreen mode Exit fullscreen mode
  • Build the project $ cd config-server && mvn clean package.
  • Run the project $ java -jar target/config-server.VERSION.jar.

Now you have a full featured configuration server up and running.

Creating a spring boot client application.

Now we need to create another application that will act as a microservices that needs to load its configuration from our configuration server.
Obviously we will use spring boot and the other part of spring cloud config project (the client library).

There are two points we need to mention here before getting into the steps of creating the client.

  1. As the configuration of any project usually needs to be loaded at the startup process, we need to change the behavior of application bootstraping. This happens just by adding a new file to the resources directory.
  2. The link between the client application and the config server is the application name. In our case my-service.

Here are the steps needed to create a client application.

  • As usual, go to
  • set the project name (Artifact) to my-service.
  • add config client to the dependencies.
  • Change class to be as following
package com.example.myservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class MyserviceApplication {

    public static void main(String[] args) {, args);

Enter fullscreen mode Exit fullscreen mode
  • Add file to the resource directory and write the following configuration into it.
Enter fullscreen mode Exit fullscreen mode

The property is pointing to the URL of the config server.

  • For testing, we will create a REST controller ConfigController, inject a value from the properties files in the configuration server (, then display the value. For that purpose, create the following class.
package com.example.myservice;

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

public class ConfigController{

    @Value("${ nothing}")
    private String configProperty;

    public String getConfigProperty(){
        return this.configProperty;
Enter fullscreen mode Exit fullscreen mode
  • Package the client application $mvn clean package
  • Run the client application $java -jar target/myservice.VERSION.jar. Now if you open this URL (http://localhost:8081/getConfigs) in the browser, you should see the value of the displayed. Alt Text
  • If you want to get the configuration of another profile, you have to set the property in the file.
Enter fullscreen mode Exit fullscreen mode

Now if you package and rerun the application, you will get the value of the same property from the prod profile.
Alt Text

Final note.

At the time of writing this article, I faced an issue getting the client up and running using spring boot version 2.4.0. I had to use the previous stable version 2.3.6.

Discussion (0)