Manage Application Configuration Using Spring Cloud Config
By: Arun Mannuru
Tags: Git, Spring Boot, Spring Cloud Config, Configuration Management, Github

centralize service configuration with Spring Cloud Config Server without restarts

Overview:

Crafting Microservices now become an industry standard for any new API development, and almost all the organizations are promoting it. Spring cloud provides excellent tools to build these microservice on top of the Spring boot framework.

We will discuss a feature called Config Server. Config server is where all configurable parameters of all microservices are stored and maintained.It is more like externalizing properties/resource file out of project codebase to an external service altogether so that any changes to any given property does not necessitate the re-deployment of service which is using that property. All such property changes will be reflected without redeploying the microservice. Below given fig (a) describes how Spring Cloud Config Server works.

Fig(a) Centralized Configuration Management

Creating the Cloud Config Service:

Dependencies:

<dependencies>



<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-config-server</artifactId>

</dependency>



<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-config</artifactId>

</dependency>



<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>



</dependencies>

Adding @EnableConfigServer annotation:

Open the Spring Application Class that spring already has provided and add the @EnableConfigServer annotation before the class and build the project once again. With this annotation, this artifact will act like a spring config server.

After adding this annotation, the class will look like below :

SpringCloudConfigServerApplication.java

@SpringBootApplication

@EnableConfigServer

public class SpringCloudConfigServerApplication {



public static void main(String[] args) {

SpringApplication.run(SpringCloudConfigServerApplication.class, args);

}



}

Now we need to add the configuration like server port which our SpringBoot server listen into a configuration file and push that file into a git repo. This git repository acts as a configuration storage. We can keep adding the configuration on-fly and push the changes into repository. And we can see the changes reflected on server side.

application.yml

spring:

banner:

location: banner.txt

server.port: 8080

Once we have the configuration file pushed into a git repository. Place the git url inside the configuration file as shown below.

application.yml

spring:

cloud:

config:

server:

git:

uri: https://github:com/ARUN/springcloud-props/

Once we have this configuration file in-place under class path of our SpringBoot application. Just start the server, we can see the SpringBoot application starting in port 8080.

Conclusion:

Now we are able to create a configuration server to provide a set of configuration files from a Git repository to client applications. We can make use of this config server to provide an environment-dependent configuration.

References:

https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_server.html