Spring Cloud - Configuration Server with Git Integration

Spring Cloud is a Spring module which provides the features to make the development of distributed cloud-based applications simple and quick. Spring cloud configuration server is also one of the features which help us in the configuration management of distributed applications.
In this article, we will discuss how spring cloud configuration can be used to externalize and manage the configurations of different distributed applications for different environments from a single place.
We will be using a git repository as a configuration store which is considered an ideal choice.

1. Setting up Configuration Server - We will create a simple spring boot application which with the help of some dependencies will work as our configuration server serving configuration to our client application using a git versioned configuration store.

1.1 Create a spring boot application with below-required dependencies and import it to your IDE.
  


<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
</dependency>
1.2 Annotate the main class with @EnableConfigServer to make it work as a configuration server and add some required properties in the application.properties file. Notice that we have added spring security starter dependency which means that our application will automatically use the Basic Authentication, but to avoid autogenerated password we will add username and password also in our property file.

package com.cloud.cloudconfig;

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

@SpringBootApplication
@EnableConfigServer
public class CloudConfigApplication {

	public static void main(String[] args) {
		SpringApplication.run(CloudConfigApplication.class, args);
	}
}
application.properties -

server.port=8888
spring.cloud.config.server.git.uri=${HOME}/HELLO-SPRING-CLOUD/config-repo
spring.cloud.config.server.git.clone-on-start=true
spring.security.user.name=root
spring.security.user.password=rootp
1.3 Setting up configuration GIT repository - Notice that in the application.properties file we have used a folder path for property spring.cloud.config.server.git.uri, we will create this repository at the specified location which is inside the home directory of the current user, and initialize it as a git repo. After initialization, we will add a properties file with some properties in it which can be read by the client applications.
After creating the directory run the below commands to git initialize the directory and add a new file to it config-client-development.properties

~/HELLO-SPRING-CLOUD/config-repo(master)$ git init
~/HELLO-SPRING-CLOUD/config-repo(master)$ vim config-client-development.properties
 
After writing the above commands vim command-line editor will open and you can add the properties given below using it and save it. (Or you can simply go to the directory after git init, create a new file config-client-development.properties and add the content using some text editor also and save it.)
app.env=development
Once the file is saved commit it

~/HELLO-SPRING-CLOUD/config-repo(master)$ git add .
~/HELLO-SPRING-CLOUD/config-repo(master)$ git commit -m "initialized config properties"

Note: The name of the property follows a naming pattern - the name of the client application followed by a dash and then the active profile. So above created file will be used by a client application with name config-client and active profile development.
Now once we are done adding the property file we are all set to start out configuration server application to start serving. To start the application you can simply run the CloudConfigApplication class which has the main method from your IDE and it will get deployed on inbuilt tomcat server on specified port 8888. Once the application is started the configurations can be queried on the URLs given below which are served by the APIs provided by our configuration server application.

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

Now if we hit the url - http://localhost:8888/config-client-development.properties
we will get the following output
user.role: Developer
Now we are done with our config server application set up and its time to create a client application that will be using these configurations.

1. Setting up Client application - We will create a simple spring boot application with a simple REST GET method. Use the same Spring Initializr to generate the client application with required dependencies given below

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

After generating the application import it to the IDE and create a resource file bootstrap.properties which will be loaded at the startup of the application.

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

The above configuration file has the connection details to our configuration server application along with the client application name and active profile.
Now let's write a simple controller to server a GET request which will use the configuration value from the configuration server.

package com.cloudclient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class CloudClientApplication {
	
	@Value("${app.env}")
    private String envName;
	
	public static void main(String[] args) {
		SpringApplication.run(CloudClientApplication.class, args);
	}
	
	@GetMapping(
		      value = "/which-env",  
		      produces = MediaType.TEXT_PLAIN_VALUE)
		    public String whoami() {
		        return String.format("Hello! You are on %s enviromnment.", envName);
		    }
}


Though It works very well with spring applications like we have used with @Value annotation but it works well on any other environment also.
Now we are done with our client application setup also, let's run our client application also and check if the configuration is working properly.
After running the client application (simply run the main class CloudClientApplication using your IDE) lets hit its REST URL -
http://localhost:8080/which-env
And if the response is as given below our client is fetching the configuration form configuration server properly.
Hello! You are on development enviromnment.
Now If any point of time you want to change the configuration just change and commit the configuration and refresh the configuration server application. There is no need to restart or redeploy our client application.
I hope this article has provided you a basic understanding of the spring cloud configuration server to get you started. You can check out the source code from here - Spring Cloud Config Server - GIT

Comments

Popular posts from this blog

Spring-Boot externalize logback configuration file (logback-spring.xml)

Create Height Balanced BST from Sorted Linkedlist

Reverse LinkedList in K nodes group - Data Structure Linked List

Finding Height Of A Binary Tree