Centralized Configuration with Spring Cloud Config
What is Configuration as a Service
In micro-service world, managing configurations of each service separately is a tedious and time-consuming task. In other words, if there are many number of modules, and managing properties for each module with the traditional approach is very difficult.
Central configuration server provides configurations (properties) to each micro service connected. As mentioned in the above diagram, Spring Cloud Config Server can be used as a central cloud config server by integrating to several environments.
Environment Repository — Spring uses environment repositories to store the configuration data. it supports various of authentication mechanisms to protect the configuration data when retrieving.
Spring Cloud Config is Spring’s client/server approach for storing and serving distributed configurations across multiple applications and environments.
This configuration store is ideally versioned under Git version control and can be modified at application runtime. While it fits very well in Spring applications using all the supported configuration file formats together with constructs like Environment, PropertySource or @Value, it can be used in any environment running any programming language.
In this write-up, we’ll focus on an example of how to setup a Git-backed config server, use it in a simple REST application server and setup a secure environment including encrypted property values.
Project Setup and Dependencies
I’m depending Spring Initializr for this as it is much easier. And we have to create two spring boot projects and started with maven project.
- config-server
- config-client
To get ready for writing some code, we create two new Maven projects first. The server project is relying on the spring-cloud-config-server
module, as well as the spring-boot-starter-security
and spring-boot-starter-web
starter bundles.
|
|
However for the client project we’re going to only need the spring-cloud-starter-config
, spring-cloud-starter-bootstrap
and the spring-boot-starter-web
modules.
|
|
Config Server Implementation
The main part of the application is a config class – more specifically a @SpringBootApplication – which pulls in all the required setup through the auto-configure annotation @EnableConfigServer
.
|
|
We also need to set a username and a password for the Basic-Authentication in our application.properties
to avoid an auto-generated password on every application restart.
|
|
Git Repository as Configuration Storage
To complete our server, we have to initialize a Git repository under the configured url, create some new properties files and popularize them with some values.
We will use a configuration yaml file lika a normal Spring application.yml
, but instead of the word ‘application’ a configured name, e.g. the value of the property ‘spring.application.name’ of the client is used, followed by a dash and the active profile. We will create 3 yaml files of configuration.
- config-client-local.yml
- config-client-development.yml
- config-client-production.yml
config-client-local.yml
|
|
config-client-development.yml
|
|
config-client-production.yml
|
|
Querying the Configuration
Now we’re able to start our server. The Git-backed configuration API provided by our server can be queried using the following paths.
|
|
In which the {label} placeholder refers to a Git branch, {application} to the client’s application name and the {profile} to the client’s current active application profile.
So we can retrieve the configuration for our planned config client running under local profile in branch master
via.
|
|
Then we got a response like below.
|
|
Client Implementation
Next, let’s take care of the client. This will be a very simple client application, consisting of a REST controller with one GET method.
Now, We will create a controller in com.maverick.configclient.controller.ConfigClientController
like below.
|
|
The configuration, to fetch our server, must be placed in a resource file named bootstrap.application
, because this file (like the name implies) will be loaded very early while the application starts.
|
|
To test, if the configuration is properly received from our server and the role value gets injected in our controller method, we simply curl
it after booting the client.
|
|
If the response is as follows, our Spring Cloud Config Server and its client are working fine for now.
|
|
Clone or Download
You can clone or download this project
|
|
Thankyou
Medium - Microservices Centralized Configuration with Spring Cloud
Baeldung - Quick Intro to Spring Cloud Configuration
Github - Servidor Configuraciones en Spring Cloud