Introduction to Spring Cloud Config

Ruminder Singh
Geek Culture
Published in
4 min readMay 20, 2021

--

The best practice of developing an application is to make it loosely coupled. We should be able to add new features, properties without affecting its other pre-existing functionalities. And as our application grows, into different micro-services, modules it becomes difficult to manage the properties of the application. Which property is for the development platform and which is for production builds. With changes in the properties, we have to rebuild, retest and deploy the application again.

Services connecting to config server for a single source of providing properties.

What if we can separate our all the configuration from the logic of different services and all the services can read the applications on the fly. There are different approaches to that like Spring Cloud Config, Consul, Zookeeper, and others. In this article, we will use Spring cloud-config to centralize our properties and configuration of the application. The detailed code for the repo can be found here and config servers and clients code can be found at this link.

Advantage

Consistency: It acts as a sole of truth for the configurations.
Time-Saving: You do not need to redeploy the application as the changes are made on the fly.
Profile Based Config: Can set up different configurations based on profiles.

Source of configurations:

We can put all the configurations into one microservice from which all services can fetch. But how can we put the config into it? If we add it to the source code, we will need to redeploy it whenever we make changes. We could use a database, which can be one of the possibilities. However, we use here git repo as a source of the configurations. Spring Cloud config fetches the configurations from the git repo and any service can get from it.

Git Repo

To create a repo and add properties follow the below commands

mkdir configuration_repo
cd configuration_repo
git init .
echo logging.level.root: INFO > config-client-production.properties
echo logging.level.root: DEBUG > config-client-development.properties
git add .
git commit -m "Initialization of properties"
git push

We have created two different configuration files based on profiles. In code, more properties are added which you can check here.

Spring Cloud Config Server

We have created the source from where our config server will receive the properties. Now we need to create a config server that will facilitate configurations to micro-services. Create a spring project with spring config server dependency added to it. If you are adding spring-cloud-config-server dependency after the creation of a project. You must add spring-cloud-dependencies in dependencyManagement. Add @EnableConfigServer annotation to Spring boot application class.

@SpringBootApplication
@EnableConfigServer
public class SpringCloudConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigServerApplication.class, args);
}
}

Now we need to specify the path/URL from where the config server will read the files in properties files. We can either use file, ssh, or HTTP to specify the git repo path.

spring.application.name=cloud-config-server
server.port=8888
spring.cloud.config.server.git.uri=https://github.com/ruminder-hub/spring_cloud_config

I am using here HTTP address, you can use the file by just changing the path. You can find other options in my code with comments. To verify its working use one of the below URLs.

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

application stands for the file name, the profile is for the profile you want to use, and the label is branch name.
curl http://localhost:8888/config-client/development/main

*** You must provide the branch name if you are using the URL with the label as if not provided it will take default as master( as it defaulted in GitHub), however, GitHub has changed it to main, so you need to provide main in the path. ***

Spring Cloud Config Client

No, we will create the micro-service which will be using configurations from the config server. To be able to do that, the service must declare itself a client of the spring config server. To do that added dependency spring-cloud-starter-config. Add the below values to the property file.

spring.application.name=config-client spring.profiles.active=development
spring.cloud.config.uri=http://localhost:8888

We are setting the name of the application and profile to know the exact file of the git repo for fetching configurations.

  • Now even if we set these properties, the service will not work. Reason: Github has changed the default branch to main but spring still considers the master branch as default. So you need to set one more property spring.cloud.config.label=main.
  • If you are using the latest version of spring boot use spring.config.import=configserver:http://localhost:8888 instead of config.URL.
  • To test create the Controller with API printing logs at a different level and printing database_name. Code can be found here.

Key Points

  • To decrease the latency of fetching configuration from remote, services make a local copy of the properties after the first fetch.
  • You can set different git repo for different applications.
  • You can control how often your service checks with the config server for any change in properties. Set the value of the refresh rate
  • There can be the possibility that your local copy of the config gets corrupted and you are unable to update it even after fetching the latest one. For that, you can use force-pull to forcefully update the configs.

From the above article, I believe you will get a better understanding of using the Spring Cloud Config in your project. If you have any questions, post them in the chat. Thanks.

--

--