Backend Development 9 min read

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.

macrozheng
macrozheng
macrozheng
Master Spring Cloud Bus: Dynamic Config Refresh with RabbitMQ

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:

<code>rabbitmq-plugins enable rabbitmq_management</code>

Visit 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

:

<code>&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
    &lt;artifactId&gt;spring-cloud-starter-bus-amqp&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
&lt;/dependency&gt;</code>

Create

application-amqp.yml

with RabbitMQ settings and expose the

bus-refresh

actuator 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-amqp

dependency to the client’s

pom.xml

.

<code>&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
    &lt;artifactId&gt;spring-cloud-starter-bus-amqp&lt;/artifactId&gt;
&lt;/dependency&gt;</code>

Create two bootstrap files (

bootstrap-amqp1.yml

and

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

springCloudBus

exchange and three queues prefixed with

springCloudBus.anonymous

are created.

Modify

config-dev.yml

in the Git repository (change

config.info

value).

<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/configInfo

and

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

MicroservicesRabbitMQSpring Cloud ConfigDynamic Config RefreshSpring Cloud Bus
macrozheng
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.