Challenges with Manual Configuration Refresh
In a micorservice architecture using Spring Cloud Config, refreshing configuration changes on each client typically requires sending a POST request to the /actuator/refresh endpoint. While this works for a small number of clients, it becomes inefficient and cumbersome as the number of microservices grows. To address this, Spring Cloud Bus offers a centralized and automated solution.
Introduction to Spring Cloud Bus
Spring Cloud Bus connects distributed nodes using a lightweight message broker. It enables broadcasting state changes, such as configuration updates, across all connected services. Internally, it uses Spring Boot’s auto-configuration to extend applications and establish inter-service communication channels. Currently, it is most commonly implemented using AMQP brokers like RabbitMQ or Kafka.
Conceptually, Spring Cloud Bus acts as a communication backbone for distributed systems, leveraging the broadcast capabilities of messsage queues to propagate events. This is especially useful in scenarios like configuration management, where all instances need to be notified of a change simultaneously.
Setting Up RabbitMQ
Ensure RabbitMQ is running and accessible. This article assumes RabbitMQ is already installed and configured with default settings on localhost:5672 using the default credentials.
Configuring Spring Cloud Config Server
1. Add Required Dependenceis
Update your pom.xml to include the Spring Cloud Bus AMQP starter:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
2. Configure RabbitMQ in application.yml
spring:
cloud:
bus:
trace:
enabled: true
management:
endpoints:
web:
exposure:
include: "*"
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
Updating the Spring Cloud Config Client
1. Add Bus and Actuator Dependencies
Include the same bus and actuator dependencies in the client application:
<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>
2. Configure RabbitMQ in Client
Use the same RabbitMQ configuration as in the server:
spring:
cloud:
bus:
trace:
enabled: true
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
management:
endpoints:
web:
exposure:
include: "*"
Testing the Setup
- Start the Eureka discovery server.
- Launch the Spring Cloud Config Server and Client applications.
- Access the configuration endpoints to verify initial values, e.g.,
http://localhost:8001/neo-config/devandhttp://localhost:8003/hello. - Update the configuration file in the remote Git repository.
- Send a POST request to the
/actuator/bus-refreshendpoint on the Config Server to trigger a global refresh. - Verify that the updated configuration is reflected across all clients without manually refreshing each one.