Skip to content

  • Home
  • Core Java
    • Write Your Own Immutable Class In Java
    • Write Your Own Singleton Class In java
    • Java Concurrent Package
    • Java Stream Revisited
    • Print odd and even numbers using thread
    • SOLID principles
    • Comparable Vs Comparator
    • Sort HashMap/TreeMap based on value
    • Deep and Shallow Copy in Java with examples
    • Find the frequency of each character in a String
    • How to avoid duplicate Employee objects in HashSet ?
  • Spring
    • Loose Coupling & Dependency Injection
    • Bean Scope
    • Spring Bean Lifecycle
    • IoC Container Vs Application Context Vs Bean Factory
    • @Component Vs @Service @Repository Vs @Controller
    • How to read properties file in Spring
    • Spring AOP (Aspect Oriented Programming)
    • @Component Vs @Bean in Spring
    • Exception Handling in SpringBoot
    • XML configuration Vs Annotations configuration in Spring
    • Spring Data JPA
    • Spring Data REST
  • Spring Security
    • Spring Security with Form, Basic and JWT
    • Security Configuration in Spring Boot Apps
    • Security Protocols & Frameworks
    • Okta OAuth 2.0 SSO with Springboot
    • Spring Boot 2.x Security using Username Password
    • Spring Boot 2.x Security using JWT
    • Spring Boot 3.x Security using Username Password
    • Spring Boot 3.x Security using JWT
  • Microservices
    • Spring Cloud Config (Server & Client)
    • Spring Boot Microservices Tutorial (Part 1 of 2)
    • Spring Boot Microservices Tutorial (Part 2 of 2)
    • Circuit Breaker – Resilience4j
    • The Twelve-Factor App Principles
  • Event Driven Microservices
    • What is Event Driven Microservice ?
    • What is Saga Design Pattern ?
    • What is CQRS Design Pattern ?
  • Spring AI
    • ChatGPT API + SpringBoot Integration
  • Hibernate & JPA
    • JPA vs JDBC
    • CRUD Example Using Spring Boot JPA + H2 + Gradle
    • MongoDB Atlas With Spring Boot Example
    • Transaction Management
    • Relationships in JPA & Hibernate
    • Hibernate First & Second Level Cache
    • Spring Boot Flyway Postgres
  • DevOps
    • What is Devops ?
    • Docker
    • Kubernetes (K8S)
    • Jenkins
    • Infrastructure As Code
  • Functional Programming
    • Functional Programming Vs Structured Programming
    • Java 8 Programs For Interview
    • Predicate, Function, Consumer and Supplier in Java 8
    • Sort a List having Employee objects using Java 8
    • Find Employee from List using Java 8
  • AWS
    • AWS S3
    • AWS EC2
    • EC2 Solutions Architecting
    • How to create an EC2 instance ?
    • How to connect to AWS EC2 instance ?
    • Deploy application to AWS
    • AWS Lambda Java Tutorial
    • Spring Cloud Functions
    • How to Start/Stop AWS ECS automatically using Java Spring?
    • Container Solution in AWS
    • AWS SQS, SNS, MQ and Kinesis
    • Databases in AWS
    • AWS VPC: Peering, Endpoint, VPN, Gateways- Internet, NAT, DX
    • Machine Learning in AWS
    • Storage Solutions in AWS
    • AWS ASG (Auto Scaling Group)
  • AWS Certifications
    • SAA-C03
      • Design Cost-Optimized Architectures
    • AWS Certified Solution Architect-Associate
      • Question 1
  • Kafka
    • Apache Kafka
    • Kafka Producer & Consumer Example Using Spring boot & Conduktor
  • Angular
    • Angular Tutorial – Part 1
    • Angular Tutorial – Part 2
  • Miscellaneous
    • How to add a project to Git/GitHub/GitLab
    • How to Clone a project from Git/GitLab using Git Bash
    • How to query Oracle tables based on some date ?
    • How to highlight text in WordPress ?
    • How to add Page Jumps in WordPress page ?
  • Interview Preparation
    • Core java interview questions
    • Java Threads Interview Questions
  • Contact Me
  • Toggle search form

Spring Cloud Config (Server & Client)

To demonstrate the Spring Cloud Config concept we need 3 applications:

  1. Git Repository.
  2. Spring Cloud Config Server.
  3. Spring Cloud Config Client (the microservice application).

In the below diagram the interest-rates-service is a microservice where we had added the Spring Cloud Config Client dependency. Also there would be other microservices in the vicinity who may be using the same config server :

1. Git Repository:

We will name this application as – interest-rates-repo
(You can use local git repo as well, but in real life environment you will be using a remote git repo).

In the repo you can add below files:

interest-rates.properties
interest-rates-dev.properties
interest-rates-uat.properties

The above 3 files would be for production, development, and UAT respectively.

You can find the GitHub URL for the same here : https://github.com/heapsteep/interest-rates-repo.git

2. Spring Cloud Config Server:

Now we will create a spring boot project from Spring Initializr. We will give it this name- config-server . Add the below dependency and create the config server application:

One can see the below dependency in the pom.xml:

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-config-server</artifactId>
</dependency>

Add below items in application.properties:

server.port=8888
spring.cloud.config.server.git.uri=https://github.com/heapsteep/interest-rates-repo.git
spring.cloud.config.server.git.default-label=master

The last line in the above properties file specifies the branch of the code you are considering. If you don’t specify that, you will get below exception:

org.springframework.cloud.config.server.environment.NoSuchLabelException: No such label: main
.
.
.
Caused by: org.eclipse.jgit.api.errors.RefNotFoundException: Ref main cannot be resolved

In the main class add below line:

@EnableConfigServer

Start the server. You can test the properties by calling below URL:

http://localhost:8888/interest-rates-dev.properties

http://localhost:8888/interest-rates.properties

If you change any of the value in the repo, the value in the config-server will be updated automatically, without any restart of the Config Server.

GitHub location for the above Config Server application is here: https://github.com/heapsteep/config-server.git

3. Spring Cloud Config Client:

Now lets create the Config Client, we will give it the name- interest-rates-service. In real life this would be the microservice application.
Create an spring boot application by importing below dependency:

One can see the below dependency in the pom.xml:

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

Create the bean class:

@Data
@AllArgsConstructor
public class InterestRates {
	int home;
	int motor;
}

And the controller:

@RestController
@RefreshScope
public class TestController {
	
	@Value("${interest.home}")
    private String home;
	
	@Value("${interest.motor}")
    private String motor;
		
	
	@GetMapping("/getRates") 
    @ResponseBody
    public InterestRates getRates() {
        return new InterestRates(Integer.parseInt(home),Integer.parseInt(motor));
    }
	
	@GetMapping("/getRate_home") 
    @ResponseBody
    public String getRate_home() {
        try {
            return this.home.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "Error: " + e.getMessage();
        }
    }

}

In application.properties:

spring.application.name=interest-rates
server.port=8080
spring.config.import=optional:configserver:http://localhost:8888
spring.profiles.active=dev
management.endpoints.web.exposure.include=*

Just make sure that the value of spring.application.name should match with the the name of .properties file in the repo, i.e., interest-rates.properties file here.

In the above properties file we have set the profile to dev. If you want to make it to prod, just keep it blank.

http://localhost:8080/getRates

GitHub URL of the same Config Client can be found here: https://github.com/heapsteep/interest-rates-service.git

But still there is a problem- If you change some values in the repo, although it will automatically get updated in the config-server application, but it will not get updated automatically in the config-client application. You need to restart the config-client. To avoid the restart, you have to add below 3 changes in the code and do some other activities as mentioned below:

1.In the RestController class add this line: @RefreshScope.
2.Add the actuator dependency to config-client application.
3.Add the below line in bootstrap.properties. This will enable all actuator urls.

management.endpoints.web.exposure.include=*

Now from a REST client like Postman do a POST call (no body) to the below URL:
For Spring Boot 1.x+ call this URL: http://localhost:8080/refresh
For Spring Boot 2.0.0+ call this URL: http://localhost:8080/actuator/refresh

Finally, you will see the updated changes in config-client application.

But in real world, there would be lot of microservices. You can’t give a refresh call to each and every microservices running on different port. For that you have to take help of – Spring Cloud Bus.
But, with Spring Cloud Bus you can refresh all instances of a single microservice, but not for all microservice.

Copyright © 2025 .

Powered by PressBook Blog WordPress theme