Search Tutorials


Spring Cloud Tutorial - Publish Events Using Spring Cloud Bus | JavaInUse

Spring Cloud Tutorial - Publish Events Using Spring Cloud Bus

In a previous tutorial we had seen how with the Spring Cloud Config Server we have a central place to manage external properties for applications across all environments. We had stored the properties in GIT and used the same in our modules using Spring Cloud Config.
Spring Cloud Bus Tutorial

Need for Spring Cloud Bus

Now consider the scenario that we have multiple applications reading the properties using the Spring Cloud Config and the Spring Cloud Config in turn reads these properties from GIT.
Consider the below example where multiple employee producer modules are getting the property for Eureka Registration from Employee Config Module.
Spring Cloud Bus Example
What will happen if suppose the eureka registration property in GIT changes to point to another Eureka server. In such a scenario we will have to restart the services to get the updated properties. There is another way of using Actuator Endpoint /refresh. But we will have to individually call this url for each of the modules. For example if Employee Producer1 is deployed on port 8080 then call http://localhost:8080/refresh. Similarly for Employee Producer2 http://localhost:8081/refresh and so on. This is again cumbersome. This is where Spring Cloud Bus comes into picture.
Spring Cloud Bus Use
The Spring Cloud Bus provides feature to refresh configurations across multiple instances. So in above example if we refresh for Employee Producer1, then it will automatically refresh for all other required modules. This is particularly useful if we are having multiple microservice up and running. This is achieved by connecting all microservices to a single message broker. Whenever an instance is refreshed, this event is subscribed to all the microservices listening to this broker and they also get refreshed. The refresh to any single instance can be made by using the endpoint /bus/refresh

Spring Cloud - Table Of Contents

Microservice Registration and Discovery with Spring cloud using Netflix Eureka- Part 1. Microservice Registration and Discovery with Spring cloud using Netflix Eureka - Part 2. Microservice Registration and Discovery with Spring cloud using Netflix Eureka - Part 3. Microservice Registration and Discovery with Spring cloud using Netflix Eureka - Part 4. Spring Cloud- Netflix Eureka + Ribbon Simple Example Spring Cloud- Netflix Eureka + Ribbon + Hystrix Fallback Simple Example Spring Cloud- Netflix Hystrix Circuit Breaker Simple Example Spring Cloud- Netflix Feign REST Client Simple Example Spring Cloud- Netflix Zuul +Eureka Simple Example Spring Cloud Config Server using Native Mode Simple Example Spring Cloud Config Server Using Git Simple Example Spring Boot Admin Simple Example Spring Cloud Stream Tutorial - Publish Message to RabbitMQ Simple Example Spring Cloud Stream Tutorial - Consume Message from RabbitMQ Simple Example Spring Cloud Tutorial - Publish Events Using Spring Cloud Bus Spring Cloud Tutorial - Stream Processing Using Spring Cloud Data Flow Spring Cloud Tutorial - Distributed Log Tracing using Sleuth and Zipkin Example Spring Cloud Tutorial - Spring Cloud Gateway Hello World Example Spring Cloud Tutorial - Spring Cloud Gateway Filters Example Spring Cloud Tutorial - Spring Cloud Gateway + Netflix Eureka Example Spring Cloud Tutorial - Spring Cloud Gateway + Netflix Eureka + Netflix Hystrix Example Spring Cloud Tutorial - Secure Secrets using Spring Cloud Config + Vault Example

Video

This tutorial is explained in the below Youtube Video.

Lets Begin-

We will modifying the code we developed in Spring Cloud Config using GIT example.

Changes for employee-producer module -




Once you have imported the employee-producer module in eclipse, copy and create a similar employee producer module which will run on port 8081. So we will have employee-producer1 and employee-producer2 modules running on ports 8080 and 8081 respectively. Next for both these modules make the following changes.
  • Add Actuator support
  • Add spring-cloud-starter-bus-amqp dependencies
So in these modules add the dependencies as follows -
        <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-bus-amqp</artifactId>
		</dependency>
Also in each module add the following property to disable spring actuator security -
management.security.enabled=false 

Changes for employee-config module -

For this module make the following changes
  • Add Actuator support
  • Add spring-cloud-starter-bus-amqp dependencies
So in these modules add the dependencies as follows -
        <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-bus-amqp</artifactId>
		</dependency>
Also in each module add the following property to disable spring actuator security -
management.security.enabled=false 

Changes for employee-eureka-server module -

Once you have imported the employee-eureka-server module in eclipse, copy and create a similar employee-eureka-server which will run on port 8091. So we will have employee-eureka-server1 and employee-eureka-server2 modules running on ports 8090 and 8091 respectively.
We are done with the changes. Start RabbitMQ as specified in this Getting Started with RabbitMQ Tutorial. Next start the modules in the following sequence -
  • employee-config
  • employee-server1
  • employee-server2
  • employee-producer1
  • employee-producer2
So we will be having the modules as follows. Since the global property for eureka service registration specifies eureka server running on port 8090 so this will be where both employee producer modules will be registered.
Spring Cloud Bus Eureka Example
We can see employee producer modules are registered at eureka server on port 8090.
Spring Cloud Bus Eureka Tutorial
Now modify this global property to point to eureka server running on 8091. and call a bus/refresh call on any one module among employee producer1,employee producer2 and employee config.
Spring Cloud Bus Config
We will see that all modules get refreshed and both employee producer1 and employee producer2 are now registered on eureka server running on port 8091
Spring Cloud Bus Producer
We can see employee producer modules are registered at eureka server on port 8091.
Spring Cloud Bus

Download Source Code

Download it -
Spring Cloud Config
Employee Producer2
Employee Producer1
Employee Eureka Server1
Employee Eureka Server2

See Also

Spring Boot Hello World Application- Create simple controller and jsp view using Maven Spring Boot Tutorial-Spring Data JPA Spring Boot + Simple Security Configuration Pagination using Spring Boot Simple Example Spring Boot + ActiveMQ Hello world Example Spring Boot + Swagger Example Hello World Example Spring Boot + Swagger- Understanding the various Swagger Annotations Spring Boot Main Menu Spring Boot Interview Questions