Spring Cloud Config Server

December 27, 2022

Spring Cloud Config Server

Config Server idea comes from the “The Twelve-Factor App Manifesto” in order to follow best practices for implementing cloud-native applications. Config item in this manifesto suggests to take configuration files out of the application and centralize them as these configurations can vary during time and they can differ according to environment. So, it will be easier and more efficient to manage client configurations across micro services in distributed architecture.

Spring Cloud Config Server Architecture

Spring Cloud Config Server allows us to manage application configurations across different services and environments in a centralized way.

What is Spring Cloud Config Server?

You can manage different configuration of different services and even you can manage environment based configurations of a specific service via Spring Cloud Config Server. For instance, you can centralize your local, dev, test or production environment within Spring Cloud Config Server and you can serve these configurations according to environment selection while running your application.

Main features of Spring Cloud Config Server are as below described in Spring Documentation page;

  • HTTP, resource-based API for external configuration (name-value pairs, or equivalent YAML content)
  • Encrypt and decrypt property values (symmetric or asymmetric)
  • Embeddable easily in a Spring Boot application using @EnableConfigServer

Also the config client features (for Spring applications) are as below again from Spring Documentation page;

  • Bind to the Config Server and initialize Spring Environment with remote property sources
  • Encrypt and decrypt property values (symmetric or asymmetric)

Spring Cloud Config Server supports two ways of configuration data storage;

  • Local File Storage
  • Git or SVN Repository Storage

I will try to explain how to implement Spring Cloud Server with an example from my GitHub profile. As always, I am sharing my example repos as public in my GitHub profile.

You can reach my Spring Cloud Server example from my public repo, spring-cloud-config. I have transferred an existing application to work with Spring Cloud Config Server which is my-swagger-ui-example.

Implementing Spring Cloud Server

As I mentioned before this code base for Spring Cloud Config Server is published in my GitHub within spring-cloud-config repository. You can also check the code examples from that repo.

First of all, let’s create our Spring Boot project and we need to add Spring Cloud Config Server dependency to our pom.xml.

Please check the Gist that shows how to add @EnableConfigServer to SpringCloudConfigApplication class.

Now it is time to configure our Spring Cloud Config Server within application.properties file.

Please check the Gist that shows to to configure application.properties file for Spring Cloud Config Server.

So, our Spring Cloud Config Server serves from 8888 port in this example.

I have given also two ways to store your configuration file, one is from GitHub repository and the other is from local file system.

It is a little bit tricky to configure serving the config data from GitHub repository, so let me explain in more detail.

If your GitHub repository is public, which is not the desired one, so you only need to add “spring.cloud.config.server.git.uri” property.

If your GitHub repository is private, then the best way is to connect it with RSA keys. First, you need to create your private and public keys. Use the below ssh-keygen command in order to create them. Keep in mind that JGit requires RSA keys in PEM format, so below command will help you.

ssh-keygen -m PEM -t rsa -b 4096 -C “your email”

Then, you need to add your public key to your GitHub config data repository from Setting → Deploy keys → Add deploy key section.

Adding Public Key To GitHub Repo

Then, you need to add your private key into your application.properties files in your Spring Cloud Config Server application as in the above example. Please, check the format in my example otherwise it is possible to get a private key invalid error or a connection error to your config data repo.

How to Define Config Data

It does not matter whether you are using local storage or Git repository, you need to give the correct naming to your application property files in order to read my your client applications.

For instance, in my example my client application name is “my-swagger-ui-example” so there should be a config file having exactly the same name with our application. I create a public config data repo in my GitHub profile named “spring-cloud-config-date”, you can reach it from here. As you can see in my example config data repository, there is a property file named “my-swagger-ui-example.properties”. So, our client application looks for that config data file while bootstrapping.

Please check the Gist that shows how to configure config data.

Configuring Our Client Application

Now, it is time to configure our client application to run synchronously with our Spring Cloud Config Server.

We need to add a bootstrap.properties file to our application/service in order to connect Spring Cloud Server while bootstrapping. The important point here is the application name. It should be exactly the same with the config data as described in the previous section.

You can check the code base from my-swagger-ui-example repo in my GitHub profile.

Please check the Gist that shows how to configure bootstrap.properties for the client application.

@RefreshScope Annotation

@RefreshScope annotation is used to refresh any bean annotated by @RefreshScope annotation. Any component that are using these beans will get a new instance on the next method call, initialized and injected from scratch with all dependencies.

So, if you update any variable in your property file, these updates will be reflected with the next method call in that class. That is what “The Twelve-Factor App Manifesto” describes as the best practises in cloud-native applications.

In a Nutshell

And, that is all. Your Spring Cloud Config Server and your client application are ready. Just run your Spring Cloud Config Server and then your client application and experience the ease and advantageous ways of storing your application property files in a centralized way.

As always all example repos are shared as public in my GitHub profile.

All your questions & comments are always welcomed.

Share: