DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • How to Develop Microservices With Spring Cloud and Netflix Discovery
  • Full-Duplex Scalable Client-Server Communication with WebSockets and Spring Boot (Part I)
  • A New Era Of Spring Cloud

Trending

  • MySQL to PostgreSQL Database Migration: A Practical Case Study
  • Understanding and Mitigating IP Spoofing Attacks
  • Enhancing Security With ZTNA in Hybrid and Multi-Cloud Deployments
  • Unlocking AI Coding Assistants Part 3: Generating Diagrams, Open API Specs, And Test Data
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. How to Setup the Spring Cloud Config Server With Git

How to Setup the Spring Cloud Config Server With Git

Spring cloud config server helps to manage all the application-specific properties from a single location without affecting the running microservices.

By 
Manish Gupta user avatar
Manish Gupta
·
Dec. 26, 20 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
33.3K Views

Join the DZone community and get the full member experience.

Join For Free

Companies are slowly adopting microservice over monolithic architecture to scale applications. Compared to monolithic, microservice architecture breaks applications into small, manageable services, i.e., microservices. That means one monolithic application can be converted to many microservices, and these services are developed and deployed independently. These services collaborate with each other to fulfill business objectives. Each microservice manages their own application-specific properties file, i.e., application.properties. In most situations, multiple microservices and their multiple instances run together to fulfill the business needs.  Any update in the properties file may require the redeployment and restart of services. Consider a situation where property updates need to be done on hundreds of services and their instance. This may require a considerable amount of downtime. 

To solve this problem, Spring Boot has provided a Spring configuration server. This will manage all service's properties files from a single point at runtime.

Advantage of Spring Cloud Config Server

  1. A centralized application that manages all the application related configuration properties and their versions.
  2. If there is any property change, only the centralized repository will be updated, and all the related services will receive the updated property changes without the microservice application redeploy or restart, i.e., application properties can be updated at runtime without redeploying or restarting microservices.
  3. Client-Server communication architecture, i.e., Spring Boot provides both client and server-specific components. Server manages all properties from the repository, while microservices act as a client to the server to consume properties.
  4. Secure access to the repository with encryption of the sensitive data.  
  5. Support to properties associated with multiple environments like development, staging, UAT, and production.

Spring Configuation Server services flowchart

This tutorial is helpful to develop a complete working example for a good understanding of concepts like: 

1. Repository (Store configuration).

2. Cloud config server (Read configuration from repository).

3. Cloud config client (Read configuration via Cloud Config Server).

4. Update configuration and trigger refresh event.

This tutorial will cover points 1 and 2. Another tutorial will follow to cover points 3 and 4. Let’s follow the tutorials with the basic assumption that Java8, Maven, and Eclipse are installed. 

Prerequisites

  • Java 1.8+  
  • Maven
  • Eclipse with Spring Tools Suite (STS)
  • Git

Configure Repository

1. Install Git from https://git-scm.com/downloads if not available on your machine. Git can be easily checked with the following command.Git checking command

2. Create a folder, e.g., config-properties, where configuration files are to be kept.  Add a property file, e.g., hello-service.properties with the following property:

Properties files
 




x


 
1
message=Hello world - this message is from config server



3. Instead of application.properties, name of the file kept as the client service name. Here the name of the client service name is hello-service. So file name is taken as hello-service.properties.

Java
 




xxxxxxxxxx
1


 
1
spring.application.name=hello-config-server
2
server.port=8888
3
spring.cloud.config.server.git.uri=file:///d:/config-properties



4. Now, initialize Git in the configuration folder with the help of the following commands. These commands will help to commit the properties files in the Git repository:

Java
 




x


 
1
$> cd config-properties
2
$> git init
3
$> git add .
4
$> git commit -m 'Initial Release'$> git init
5
$> git add .
6
$> git commit -m 'Initial Release'



5. Let's add a few more files considering the various other environments like development and UAT. Add and commit these files also to the repository (Refer to above step).





xxxxxxxxxx
1


 
1
message=Hello world - this message is from config server

hello-service-development.properties

Java
 




x


 
1
message=Hello world - this message is from development



hello-service-uat.properties 

Java
 




xxxxxxxxxx
1


 
1
message=Hello world - this message is from UAT



6. The local Git repository is created and used for development/testing purposes. For production deployment, the Repository can be more secure and kept at a remote location on Github, Bitbucket, Gitlab, AWS Code commit, etc.

Cloud Config Server Setup

1. Create a Spring Starter Project with a name likeHelloConfigServer. Select spring version 2.3.7 (Do not select 2.4.1 as it's having an issue in showing properties files) with dependencies like Config Server and DevTools (used for easy redeployment).new spring starter project dependencies

2. Once the project is created, go to the application.properties file available inside the resource folder and add the following in the file:

Java
 




xxxxxxxxxx
1


 
1
spring.application.name=hello-config-server
2
server.port=8888
3
spring.cloud.config.server.git.uri=file:///d:/config-properties



3. It's standard practice to run a configuration server at port 8888. For a Windows environment, kindly keep an additional "/".  For other environments, "//" will work. spring.cloud.config.server.git.uri=file://d:/config-properties


4. For remote repository, sample GitHub configuration will look like as below:

Java
 




x


 
1
spring.cloud.config.server.git.uri=https://github.com/abcd/springboot
2
spring.cloud.config.server.git.uri.username=test
3
spring.cloud.config.server.git.uri.password=test



5. Now open the main application file, i.e., HelloConfigServerApplication.java, and add @EnableConfigServer annotation to make it Config Server. Final java file will look as below

Java
 




xxxxxxxxxx
1
14


 
1
package com.hello.example;
2

          
3
import org.springframework.boot.SpringApplication;
4
import org.springframework.boot.autoconfigure.SpringBootApplication;
5
import org.springframework.cloud.config.server.EnableConfigServer;
6

          
7
@EnableConfigServer
8
@SpringBootApplication
9
public class HelloConfigServerApplication {
10

          
11
    public static void main(String[] args) {
12
        SpringApplication.run(HelloConfigServerApplication.class, args);
13
    }
14
}



6. All basic configurations are done now. Let's run the application as SpringBootApp. The Tomcat server will start at 8888 as configured. Check the following URL (This will pick the default configuration file from the repository). 
  • http://localhost:8888/hello-service/default

7. If the following response is received, then all configurations are OK, i.e., Config Server is fetching default property from Git repository.

JSON
 




x


 
1
{
2
  "name": "hello-service",
3
  "profiles": [
4
    "default"
5
  ],
6
  "label": null,
7
  "version": "3da88aa3c6103cc7f2b383a3ee5d5dad5e6e870c",
8
  "state": null,
9
  "propertySources": [
10
    {
11
      "name": "file:///d:/config-properties/hello-service.properties",
12
      "source": {
13
        "message": "Hello world - this message is from config server"
14
      }
15
    }
16
  ]
17
}



To access the development environment file, Kindly use the following URL:  http://localhost:8888/hello-service/development.

Conclusion

Now we are able to create a configuration server that can access the Git repository and provide access to configuration files.

 Source code related to this tutorial is available at Github.

Spring Framework Spring Cloud Git microservice Property (programming) application Repository (version control) Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • How to Develop Microservices With Spring Cloud and Netflix Discovery
  • Full-Duplex Scalable Client-Server Communication with WebSockets and Spring Boot (Part I)
  • A New Era Of Spring Cloud

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!