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 sbin directory of the RabbitMQ installation.
Open a command line and enable the management plugin:
rabbitmq-plugins enable rabbitmq_managementVisit http://localhost:15672/ and log in with username guest and 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:
<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>Create application-amqp.yml with RabbitMQ settings and expose the bus-refresh actuator endpoint.
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'Add Bus Support to config‑client
Add the same spring-cloud-starter-bus-amqp dependency to the client’s pom.xml.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>Create two bootstrap files ( bootstrap-amqp1.yml and bootstrap-amqp2.yml) for two client instances with different ports.
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'Demonstration
Start Eureka server, config‑server (using application-amqp.yml), and the two config‑clients.
In the RabbitMQ console, a springCloudBus exchange and three queues prefixed with springCloudBus.anonymous are created.
Modify config-dev.yml in the Git repository (change config.info value).
# before
config:
info: "config info for dev(dev)"
# after
config:
info: "update config info for dev(dev)"Trigger a refresh for all services via http://localhost:8904/actuator/bus-refresh . The updated value appears when calling http://localhost:9004/configInfo and http://localhost:9005/configInfo: update config info for dev(dev) 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
springcloud-learning
├── eureka-server // Eureka registration center
├── config-server // Configuration center service
└── config-client // Client services that fetch configurationProject Source Code
https://github.com/macrozheng/springcloud-learning
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
