{{announcement.body}}
{{announcement.title}}

Spring Cloud Config Server, Part 1

DZone 's Guide to

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.

· Cloud Zone ·
Free Resource

Server

Learn how to set up a Spring config server

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

Using Git 

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.

Java




x
9


 
1
Default file :  client-config.properties
2
    role.name = default role 
3
 
          
4
Dev. environment file :client-config-development.properties
5
   role.name = development role
6
 
          
7
Production environment file :client-config-production.properties
8
   role.name = production role



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.” 

  • Run  git init 
  • Run  git add 
  • Run  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  

Go to Spring to create a new Spring project. Select the config server as a dependency. Please refer to the pom.xml file for a dependency used in the project. 

Open the file "ConfigserverApplication.java" and add the @EnableConfigServer annotation. 

XML




x
10


 
1
@EnableConfigServer
2
@SpringBootApplication
3
public class ConfigserverApplication {
4
 
          
5
    public static void main(String[] args) {
6
        SpringApplication.run(ConfigserverApplication.class, args);
7
    }
8
 
          
9
}
10
 
          



You will also have to setup server specific properties in application.properties or bootstrap.properties in the "src/main/resources" folder

The  spring.cloud.config.server.git.uri property is the location of the git repo with the property files.

Ruby




xxxxxxxxxx
1


 
1
#Server port
2
server.port = 8888
3
 
4
#Git repo location
5
spring.cloud.config.server.git.uri=/Users/dharmendra/Documents/springzone/configserver/config-server-repo
6
 
          


 

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.

Further Reading

Intro to Spring Cloud Config Server

Microservices and Spring Cloud Config Server

Topics:
spring cloud ,spring config server ,spring configurations ,java ,cloud ,git ,git repo

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}