Spring Cloud Bus — Refresh properties in Configuration server clients.

Prashant Gangwar
3 min readOct 2, 2021

--

In this tutorial, we’re going to take a look on another way of refreshing properties for clients from configuration server.

Spring Cloud Bus links the nodes of a distributed system with a lightweight message broker. This broker can then be used to broadcast state changes (such as configuration changes) or other management instructions. A key idea is that the bus is like a distributed actuator for a Spring Boot application that is scaled out. However, it can also be used as a communication channel between apps. This project provides starters for either an AMQP broker or Kafka as the transport.

We can use RabbitMQ or KAFKA for this purpose but just to demonstrate whole architecture I’m going to use Kafka for this article as message broker. For this article we need to install Kafka locally on the system. You check Kafka Guide to install and running it locally.

Spring Cloud Bus
Spring Cloud Bus Architecture with 3 Client applications

How to refresh latest changes for properties to the client?

I already documented first way to refresh properties for client, you can follow this link, if you didn’t go through that already.

In real world we will be using Spring Cloud Bus instead of manual refresh for each client, there can be many instances running of your application So its really hard and error prone to do a refresh on each client manually.

Let us See how to configure it for Spring Boot-

We need the following dependencies to be added in pom.xml in all client and server application.

config server

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-config-server</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-bus-kafka</artifactId>

</dependency>

To be able to expose /monitor endpoint on server you need to add the following dependency also in pom.xml

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-config-monitor</artifactId>

</dependency>

Following property should be enabled in all client and server applications.

spring.cloud.bus.enabled=true

if you’re running Kafka locally, everything will be configured automatically for you. Otherwise you need some configuration to be added in properties file.

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-config</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-bus-kafka</artifactId>

</dependency>

Application logs from client application registered with Spring Cloud Bus —

2021–10–02 19:01:10.864 INFO [bookmark-service,,] 40512 — — [pool-7-thread-1] o.a.kafka.common.utils.AppInfoParser : Kafka version: 2.7.1
2021–10–02 19:01:10.864 INFO [bookmark-service,,] 40512 — — [pool-7-thread-1] o.a.kafka.common.utils.AppInfoParser : Kafka commitId: 61dbce85d0d41457
2021–10–02 19:01:10.865 INFO [bookmark-service,,] 40512 — — [pool-7-thread-1] o.a.kafka.common.utils.AppInfoParser : Kafka startTimeMs: 1633181470864
2021–10–02 19:01:10.879 INFO [bookmark-service,,] 40512 — — [pool-7-thread-1] org.apache.kafka.clients.Metadata : [Consumer clientId=consumer-anonymous.633a2bfa-b0a0–4adb-9f62–14fc7633e5d1–3, groupId=anonymous.633a2bfa-b0a0–4adb-9f62–14fc7633e5d1] Cluster ID: OIyjRwaySxKwSg37D5vO1w
2021–10–02 19:01:10.903 INFO [bookmark-service,,] 40512 — — [pool-7-thread-1] o.a.k.c.c.internals.AbstractCoordinator : [Consumer clientId=consumer-anonymous.633a2bfa-b0a0–4adb-9f62–14fc7633e5d1–3, groupId=anonymous.633a2bfa-b0a0–4adb-9f62–14fc7633e5d1] Discovered group coordinator IND-5CG6191Y3G.ctl.intranet:9092 (id: 2147483647 rack: null)

Now if you make any changes to your config files on GitHub, all changes would be reflected to your clients automatically for all the beans associated with @RefreshScope annotation and @ConfigurationalProperties.

Endpoints to Refresh Properties —

1- POST request on /actuator/bus-refresh on any client registered with Spring Cloud Bus
2- By calling /monitor endpoint exposed on config server integrated with Spring Cloud Bus

--

--

Prashant Gangwar

Technical Lead focused on backend services, micro services, enthusiastic for new tech skills