Configure Spring Cloud Bus

Spring Cloud Bus links nodes of a distributed system with a lightweight message broker. This can then be used to broadcast state changes (e.g. configuration changes) or other management instructions.

In this example, we will be using Kafka as broker.

Prerequisites

Before we begin, it’s recommended to have already completed Spring Config Server and Spring Config Client.

Configure Config Server

In order to enable Cloud bus in Config Server, we need to provide below configuration in Config Server.

Maven Configuration

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

Adding Actuator

  1. Enable endpoints in actuator module.
  2. Add a unique bus id for each application under property spring.cloud.bus.id. This is very important to process the events from cloud bus.
spring:
  application:
    name: config-server
  profiles:
    active: native
  cloud:
    bus:
      id: ${spring.application.name}:${spring.profiles.active}:${random.uuid}
      enabled: true
management:
  endpoints:
    web:
      exposure:
        include:
        - bus-refresh 
        - bus-env

Kafka Installation is required in order to run this project. If broker is not available, disable the cloud bus. To disable cloud bus, update `spring.cloud.bus.enabled=false’

Configure Config Client

In order to enable Cloud bus in Config Client, we need to provide below configuration in Config Client.

Maven Dependencies

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

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

Enable Actuator and Bus

spring:
  application:
    name: rest
  cloud:
    bus:
      id: ${spring.application.name}:${spring.profiles.active}:${random.uuid} 
      enabled: true
      refresh:
        enabled: true
      env:
        enabled: true  
    config:
      uri: ${config.url:http://localhost:8001}
      name: rest
  profiles:
    active: dev
    
management:
  endpoint:
    info:
      enabled: true
  endpoints:
    enabled-by-default: true
    web:
      exposure:
        include:
        - bus-refresh
        - bus-env

Refresh Scope

In order to get the latest config changes from server, client must annotate the beans handling the configs with @RefreshScope annotation.

@Bean
@ConfigurationProperties(prefix = "rest")
@RefreshScope
public ApplicationProperties applicationProperties() {
	return new ApplicationProperties();
}

Test Application

Start both the config server and client application.
Config Server : http://localhost:8001
Client App : http://localhost:8080

Hit the below url and check response.
Url: http://localhost:8080/hello
Response: Hello User!!

Now, go to config folder and update the property rest.greeting in rest-dev.yml

rest.greeting = Hello New User!!

As we save the file, a refresh event will be sent to Kafka Broker and will be served to all the connected instance along with application name and profile. All applications who have the matching spring.cloud.bus.id will process that event and configs will be refreshed.

Hit the below url again and check response.
Url: http://localhost:8080/hello
Response: Hello New User!!