Decouple Configuration from Application Code with Spring Cloud Config

Bruna Castelo Branco Santos
navalia
Published in
3 min readDec 1, 2021

The microservices architecture is composed of many small, autonomous services, each with its own configuration. So if you have multiple services, managing configurations of each service separately could be cumbersome and time-consuming.

Centralized configuration is a pattern where the configuration for all services is managed in a central repository. Decoupling configuration from services helps with easier organization and management of settings. This separation enables independent changes to configurations without the need to redeploy services.

Some of the most popular solutions are: etcd, Zookeeper, Consul, Spring Cloud Configuration

In this post, I’m going to show you how to use Spring Cloud Configuration that pulls configuration from GitHub. I’ll also show you how the configuration is consumed by a client service.

Implementation

1) Creating the Cloud Config Server

Create a Spring Boot application including the following dependencies.

implementation("org.springframework.cloud:spring-cloud-config-server")

In the main class include @EnableConfigServer annotation.

Configure application.properties/application.yml

2) Create Git Config Repository Naming Conventions

Check the link below to explore how folders and filenames are structured.

NOTE : the templates below should be followed for naming folders and files.

  • folder names:<application-name>
  • file names: <application-name>-<profile>.yml/properties
application-name
|--- application-name-dev.properties
|--- application-name-qa.properties

3) Creating the config server client project

Create a Spring Boot application including the following dependencies.

implementation("org.springframework.cloud:spring-cloud-starter-config:3.0.5")implementation("org.springframework.boot:spring-boot-starter-actuator")

Create rest endpoint to retrieve configuration from server

Now that the Config Service is up and running, it’s time to put it to work. We’ll create a simple Controller that will call the Config Service on startup to retrieve its properties. The Controller has one REST endpoints that print current properties value.

IMPORTANT: Spring @Bean that is marked as @RefreshScope will get special treatment when there is a configuration change.

Configure bootstrap.properties / bootstrap.yml

Further Testing

Start the configuration server

Start the spring boot application. Use the below endpoint to make sure the application is connected to external configuration repository (in this example it’s GitHub).

Start the configuration client

Start the spring boot application.

Update the configuration in github and config server will automatically refresh the content. but client application won’t refresh its configuration automatically without triggering the application to reload configurations.

Step 1 : Use the below endpoint to force the client application to reload configuration from server and updated attributes are shown in the response.

Step 2 : Updated values are being displayed upon refresh

--

--