Master Spring Cloud Bus: Dynamic Config Refresh with RabbitMQ
This guide explains how to install RabbitMQ, configure Spring Cloud Bus with Spring Cloud Config, and use it to dynamically refresh microservice configurations, including step‑by‑step setup, code snippets, and webhook integration for automatic updates.
Spring Cloud Bus Overview
Spring Cloud Bus uses a lightweight message broker to connect services in a microservice architecture, allowing broadcast of state changes such as configuration updates. It works with Spring Cloud Config to enable dynamic configuration refresh and supports RabbitMQ and Kafka; the example below uses RabbitMQ.
RabbitMQ Installation
Install Erlang (download from http://erlang.org/download/otp_win64_21.3.exe ).
Install RabbitMQ (download from https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.14/rabbitmq-server-3.7.14.exe ).
Navigate to the
sbindirectory of the RabbitMQ installation.
Open a command line and enable the management plugin:
<code>rabbitmq-plugins enable rabbitmq_management</code>Visit http://localhost:15672/ and log in with username
guestand password
guest.
Dynamic Configuration Refresh
Dynamic refresh requires Spring Cloud Config. The example uses the config‑server and config‑client modules from the previous lesson.
Add Bus Support to config‑server
Add dependencies to
pom.xml:
<code><dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency></code>Create
application-amqp.ymlwith RabbitMQ settings and expose the
bus-refreshactuator endpoint.
<code>server:
port: 8904
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/macrozheng/springcloud-config.git
username: macro
password: 123456
clone-on-start: true
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
management:
endpoints:
web:
exposure:
include: 'bus-refresh'</code>Add Bus Support to config‑client
Add the same
spring-cloud-starter-bus-amqpdependency to the client’s
pom.xml.
<code><dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency></code>Create two bootstrap files (
bootstrap-amqp1.ymland
bootstrap-amqp2.yml) for two client instances with different ports.
<code>server:
port: 9004
spring:
application:
name: config-client
cloud:
config:
profile: dev
label: dev
name: config
discovery:
enabled: true
service-id: config-server
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
management:
endpoints:
web:
exposure:
include: 'refresh'</code>Demonstration
Start Eureka server, config‑server (using
application-amqp.yml), and the two config‑clients.
In the RabbitMQ console, a
springCloudBusexchange and three queues prefixed with
springCloudBus.anonymousare created.
Modify
config-dev.ymlin the Git repository (change
config.infovalue).
<code># before
config:
info: "config info for dev(dev)"
# after
config:
info: "update config info for dev(dev)"</code>Trigger a refresh for all services via http://localhost:8904/actuator/bus-refresh . The updated value appears when calling
http://localhost:9004/configInfoand
http://localhost:9005/configInfo:
<code>update config info for dev(dev)</code>To refresh a specific instance, call
http://localhost:8904/actuator/bus-refresh/{destination}, e.g.,
http://localhost:8904/actuator/bus-refresh/config-client:9004.
Using WebHooks
Configure a Gitee webhook to invoke the bus‑refresh endpoint whenever code is pushed to the configuration repository, enabling automatic configuration updates.
Modules Used
<code>springcloud-learning
├── eureka-server // Eureka registration center
├── config-server // Configuration center service
└── config-client // Client services that fetch configuration</code>Project Source Code
https://github.com/macrozheng/springcloud-learning
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.