How to Create Spring Cloud Config Server
This is one of the first steps to build Spring based microservices with Spring Cloud by using a Config Server .
This section emphasizes on building centralized configurations. We are going to create a Spring Cloud Config Server and centralize configurations needed for microservices.
In a traditional application, our configurations reside inside a file in the same project. We package the files and deploy them on a server.
Now, if we need to change any configurations we need to repackage the code and redeploy.
This becomes a tedious task in the microservices world as there could be multiple deployment and repackaging needed in a real-world scenario.
So the solution to above problem is Create Spring Cloud Config Server.
The Config server gets the configuration from Github or File System and all microservices uses the configuration from the config server as shown below. Let’s see how can we achieve this?
Technologies Used:-
- Java 8
- Spring Boot
- Maven 3
- Git
Project Setup for Spring Cloud Config Server :-
Now create a simple Spring Boot app from Spring initializer as below and click on generate a project. We are going to choose Config Server dependency.
@Enableconfigserver:-
We need to use this annotation while we Create Spring Cloud Config Server which says its a cloud config server and serve the configuration. Using this annotation, our app will be treated like a spring config server.
package com.frugalis.config.ConfigServer;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
How Spring Cloud Config Server Stores Configuration:-
The Spring cloud config server stores configuration in two ways
- Git or SVN
- Using local File System
We are going to use a Github repository and store configuration in a properties file. We are going to create a properties file for each microservice. Let’s say I am going to build a service known as user-service
. So we are creating a user-service.properties
for our service user-service
and push it in our GitHub repository.
Let’s create a GitHub repository for Storing of Data by our Spring Cloud Config Server. You can fork my repository as well here.
Let’s create a file known as bootstrap.properties
in resources folder.
#Server port
server.port = 8888
#spring.cloud.config.server.svn.uri=//path to SVN
#spring.cloud.config.server.svn.order=1 // Specify order if you want to use both git and svn
#spring.cloud.config.server.git.order=2
#Git repo location.
spring.cloud.config.server.git.uri=https://github.com/Sanjay007/spring-cloud-config-server.git
management.security.enabled=false
Now let’s start Our Spring Cloud Config Server.
$ cd ConfigServer
$ mvnw spring-boot:run
$ curl localhost:8888/user-service.properties
In the next post, we will see how we can create a Spring Cloud Config Client.