Understanding Spring Cloud Config Server with Example

View more Tutorials:

Follow us on our fanpages to receive notifications every time there are new articles. Facebook Twitter

1- The objective of the lesson

OK, this is the first step for you to build a distributed application using Spring Cloud. In this lesson, I am going to guide you for creating a service to manage configurations for other services.
The contents to be discussed in this lesson include:
  1. What is a Config Server? Why need a Config Server in distributed applications.
  2. Explain Centralized Configuration Management, and Versioned Configuration Management.
  3. Set up a repository to store configuration information.
  4. Build and Run Spring Cloud Config Server.

2- Config Server - Why?

OK, now we will discuss why you need to have a service that manages configurations for other services in a distributed application.
Below is the illustration of a distributed application - "Taxi Management", including three sub-applications, each of which is deployed on a server.
Each service (application) is a project developed by a team of developers. In a project, apart from code, it contains configurations, for example, information connected to database, information on location of data sources, etc. It will be a bad idea if You make hard code of this information in the project code. Therefore, this information is normally put in separate files, which are referred to as configuration files.
After being completed, the project will be packed and deployed onto the Server. Normally, configuration files will be packed together with the code and form a sole (file) product. Thus, if there are any changes in the configuration, you need to compile, and repack the project and re-deploy it onto the server. This is obviously a challenge in a distributed application environment.
Config Server?
The idea to solve the above problem is that a service (application) is required to manage configurations for other services. It runs independently on a server.
The above idea brings about the following benefits:
  • TODO
  • Spring Cloud Bus:
When you change a configuration file on the Config-Server, you will surely want to notify of such changes to Clients. The Spring Cloud Bus provides a mechanism for informing to Clients that "There is a change" and requires the Clients to update new information.

3- How does Config Server store data?

When you put all configuration files on the Config Server, you will ask how the Config Server stores those files.
There are 2 main ways for the Config Server to store configuration files:
  1. Store them on the hard drive of the server as system files.
  2. Use GIT or SVN (Subversion).
In this lesson, I am going to create a Config Server, which stores configuration files on GitHub. I have created a GitHub Repository:

4- Create Spring Boot project

On the Eclipse, create a Spring Boot ​​​​​​​project:
  • Name: SpringCloudConfigServer
  • Group: org.o7planning
  • Artifact: SpringCloudConfigServer
  • Description: Spring Cloud Config Server
  • Package: org.o7planning.scconfigserver
2.0.0.M..xx​​​​​​​ trial version Spring Boot is buggy with the Spring Cloud; therefore, we will use Spring Boot 1.5.9.
OK, the Project has been created:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"


    <description>Spring Cloud Config Server</description>

        <relativePath/> <!-- lookup parent from repository -->







package org.o7planning.scconfigserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer  // ==> Important!!
public class SpringCloudConfigServerApplication {

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

5- Configure Config Server

This service (application) will be deployed and run on port 8888 and store configuration files on the GitHub, so you need some configurations in the application.properties file.



# For File System:
# spring.profiles.active=native
# spring.cloud.config.server.native.searchLocations=C:/Users/tran/Desktop/config
If Git Server (or SVN Server) requires an username/password, you need to make additional configurations. You can refer to the appendix at the end of this post.

6- Run application

Right click on the Project, select:
  • Run As/Spring Boot App
Next lesson:

View more Tutorials: