Lab 9: Set Up Event-Based Messaging Between Microservices Using Spring Cloud Bus (Spring Boot 3.4.1)
Learn how to use Spring Cloud Bus with Spring Boot 3.4.1 to propagate configuration changes (and other events) among microservices in real time. By integrating Spring Cloud Config Server, Kafka, and Spring Cloud Bus, you’ll enable multiple microservices to automatically refresh updates without manual restarts.
-
Generate a new Spring Boot project for
ConfigServer
.- Go to https://start.spring.io/.
- Configure:
- Spring Boot Version: 3.4.1
- Group Id:
com.microservices
- Artifact Id:
config-server
- Name:
ConfigServer
- Dependencies:
- Spring Cloud Config Server
- Spring Boot Actuator
- Spring Cloud Bus
- Spring Cloud Stream Kafka
- Extract into a folder named
ConfigServer
.
-
Import the project into your IDE.
-
Enable Spring Cloud Config Server.
- In
ConfigServerApplication.java
:package com.microservices.configserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
- In
-
Configure the Config Server in
application.properties
.- In
src/main/resources/application.properties
, add:server.port=8888 spring.application.name=config-server spring.cloud.config.server.git.uri=https://github.com/<your-username>/config-repo spring.cloud.bus.enabled=true spring.cloud.stream.kafka.binder.brokers=localhost:9092
- Replace
<your-username>
with your GitHub name if needed.
- In
-
Run the Config Server.
- From
ConfigServer
:./mvnw spring-boot:run
- The server starts on port 8888.
- From
-
Download and install Apache Kafka.
- Visit Apache Kafka Downloads to get the latest version.
- Extract it (e.g., to
/opt/kafka
).
-
Start Zookeeper.
- In one terminal:
cd /opt/kafka ./bin/zookeeper-server-start.sh config/zookeeper.properties
- In one terminal:
-
Start Kafka.
- In another terminal:
./bin/kafka-server-start.sh config/server.properties
- In another terminal:
-
Verify Kafka is running.
- Use:
./bin/kafka-topics.sh --list --bootstrap-server localhost:9092
- If no topics are listed, Kafka is running but with no custom topics yet.
- Use:
-
Generate a new Spring Boot project for
UserService
.- Configure:
- Artifact Id:
user-service
- Dependencies:
- Spring Web
- Spring Cloud Config Client
- Spring Cloud Bus
- Spring Cloud Stream Kafka
- Artifact Id:
- Extract into
UserService
.
- Configure:
-
Import the project into your IDE.
-
Configure the
UserService
to use the Config Server.- In
src/main/resources/application.properties
:server.port=8081 spring.application.name=user-service spring.cloud.config.uri=http://localhost:8888 spring.cloud.bus.enabled=true spring.cloud.stream.kafka.binder.brokers=localhost:9092
- The application name
user-service
helps the config server locate the right properties file.
- In
-
Create a REST controller to display configuration values.
- In
src/main/java/com/microservices/userservice/ConfigController.java
:package com.microservices.userservice; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ConfigController { @Value("${message:Default message}") private String message; @GetMapping("/message") public String getMessage() { return message; } }
- In
-
Run
UserService
.- From the
UserService
folder:./mvnw spring-boot:run
- From the
-
Verify configuration retrieval.
- In a browser:
http://localhost:8081/message
- It should show the
message
property from your Git config repo (e.g.,application.yml
oruser-service.yml
).
- In a browser:
-
Update the configuration file in the Git repo.
- For instance, if you have
message=Hello from Config Server!
inuser-service.properties
, change it to:message=Updated message from Config Server
- Commit and push:
git add . git commit -m "Updated message property" git push origin main
- For instance, if you have
-
Trigger a refresh event.
- Post a request to the Config Server’s
/actuator/bus-refresh
endpoint:curl -X POST http://localhost:8888/actuator/bus-refresh
- This publishes an event over Spring Cloud Bus via Kafka.
- Post a request to the Config Server’s
-
Verify the updated config in
UserService
.- Reload:
http://localhost:8081/message
- Confirm the new
"Updated message from Config Server"
is displayed without restartingUserService
.
- Reload:
-
Generate a new project for
ProductService
.- Similar to
UserService
:- Artifact Id:
product-service
- Server port:
8082
- Dependencies:
- Spring Web
- Spring Cloud Config Client
- Spring Cloud Bus
- Spring Cloud Stream Kafka
- Artifact Id:
- Similar to
-
Check config sync between services.
- After configuring
ProductService
similarly (application nameproduct-service
), trigger/actuator/bus-refresh
again. - Both services (
UserService
andProductService
) should reflect updated properties from your Git config repo.
- After configuring
-
Add a new property to the configuration.
- e.g.,
service.version=1.0
. Test that it appears in both services after a bus refresh.
- e.g.,
-
Manually test Kafka messaging.
- Use Kafka CLI to produce/consume on the topics that Spring Cloud Bus uses, verifying message flows.
-
Scale services.
- Run multiple instances of
UserService
. All instances automatically get updated configs after a single bus refresh event.
- Run multiple instances of
In this lab, you:
- Set up a Spring Cloud Config Server (with Bus + Kafka) on port 8888.
- Configured one or more microservices (
UserService
,ProductService
) to fetch config from the server and auto-refresh changes. - Leveraged Spring Cloud Bus (via Kafka) to broadcast configuration updates, ensuring real-time changes with minimal overhead.
This architecture significantly simplifies config management across distributed services, enabling near-instant synchronization of property changes without restarts.