Spring Cloud Config Server, Part 1
In the first part of this two-part tutorial, take a look at how to set up a Spring Cloud config server using Git and some git resources.
Join the DZone community and get the full member experience.Join For Free
Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems and it enables engineers to quickly develop cloud-native applications. One of the features is to maintain application configurations ouddtside the application. This will enable configuration change without restarting microservice application.
The following points will be covered in the article.
- Learning to create a Spring config server to store the configuration.
- Learning to create a client to use the config server to maintain properties outside the client application.
Create Spring Config Server
Let's start with creating the Spring config server first.
We will be using Git to store the configuration information. The first step is to create a git repository on the local machine. I am using a Mac.
You may also enjoy: How to Use the Spring Config Server
Create the folder “config-server-repo” on the local machine and create three files inside it.
One is the default file and the other two are environment-specific files. Please check the value after
- in the file name. Each file has one key “role.name.”
git commit -m property files for client-config
This will create a Git repository on the local machine and commit files. Reference files are also available here.
Now we have a Git repository and files for each environment. We can now create the Spring config server.
Creating the Config Server
Open the file "ConfigserverApplication.java" and add the
You will also have to setup server specific properties in application.properties or bootstrap.properties in the "src/main/resources" folder
spring.cloud.config.server.git.uri property is the location of the git repo with the property files.
The config server application is complete and now you can build and run the application with the build command
mvn clean install and the run command
java -jar configserver-0.0.1-SNAPSHOT.jar
Test the config server:
This should display the respective environment property details.
We can save the property file for more clients in git. For example, the client1-config-development.properties can access properties with the URL: http://localhost:8888/client1-config/development. The config server can maintain multiple client-specific properties.
The client application will be covered in the next article.
Microservices and Spring Cloud Config Server
Opinions expressed by DZone contributors are their own.