RabbitMq – How to create Spring RabbitMq Publish/Subcribe pattern with SpringBoot
Tags: rabbitmq, springboot, publisher, subscriber
Published: 2021-05-06 12:09:16

https://grokonez.com/java-integration/distributed-system/rabbitmq-create-spring-rabbitmq-publishsubcribe-pattern-springboot

RabbitMq – How to create Spring RabbitMq Publish/Subcribe pattern with SpringBoot

In the tutorial, <a href="https://grokonez.com">JavaSampleApproach</a> will guide how to create <strong>Spring RabbitMq Publish/Subcribe</strong> pattern by using <strong>fanout</strong> exchanges of <strong>RabbitMq</strong> and <strong>SpringBoot</strong>.

Related articles:
- <a href="https://grokonez.com/spring-framework/spring-boot/create-spring-rabbitmq-producerconsumer-application-springboot">RabbitMQ – How to create Spring RabbitMQ Producer/Consumer applications with SpringBoot</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/how-to-start-spring-kafka-application-with-spring-boot">How to start Spring Kafka Application with Spring Boot</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/rabbitmq-queue-durability-persistent-messagedelivery-springboot">RabbitMq Queue Durability and Persistent MessageDelivery | SpringBoot</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/rabbitmq-sendreceive-java-object-messages-spring-rabbitmq-springboot">RabbitMQ – How to send/receive Java object messages with Spring RabbitMq | SpringBoot</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/springboot-rabbitmq-topic-exchange">SpringBoot RabbitMQ Topic Exchange</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/springboot-rabbitmq-headers-exchange">SpringBoot RabbitMq Headers Exchange</a>

<!--more-->
<iframe width="853" height="480" src="https://www.youtube.com/embed/P6UUAEzjrFs?rel=0" frameborder="0" allowfullscreen></iframe>
<h2>I. Spring RabbitMq Publish/Subcribe pattern</h2>
We create a <strong>Publisher</strong>, and 3 <strong>Subcriber</strong>s and using <strong>fanout</strong> exchanges of RabbitMQ for create a Publish/Subcribe Pattern System:

<img src="https://grokonez.com/wp-content/uploads/2017/07/Spring-RabbitMq-Publish–Subscribe-pattern-architechture.png" alt="Spring RabbitMq Publish–Subscribe pattern - architechture" width="608" height="251" class="alignnone size-full wp-image-6810" />

<strong>Flow messages</strong>:
- <strong>Publisher</strong> will send messages to the <strong>fanout exchange</strong>.
- The <strong>fanout exchange</strong> routes messages to all of the queues that are bound to it and the <strong>routing key</strong> is ignored.
- <strong>Subcriber</strong>s instances recieves messages from the queues.
<h2>II. Practice</h2>
<div class="jsa-quote">
<a href="https://grokonez.com/spring-framework/spring-boot/create-spring-rabbitmq-producerconsumer-application-springboot">RabbitMQ – How to create Spring RabbitMQ Producer/Consumer applications with SpringBoot</a>
</div>

<strong>Technologies</strong>
– Java 8
– Maven 3.6.1
– Spring Tool Suite: Version 3.8.4.RELEASE
– Spring Boot: 1.5.4.RELEASE
– RabbitMQ

We create 2 SpringBoot projects {<code>Publisher</code>, <code>Subcriber</code>}:

<img src="https://grokonez.com/wp-content/uploads/2017/07/Spring-RabbitMq-Publish–Subscribe-pattern-project-structures.png" alt="Spring RabbitMq Publish–Subscribe pattern - project structures" width="666" height="194" class="alignnone size-full wp-image-6811" />

<strong>Step to do</strong>:
– Create SpringBoot projects
– Create Publiser/Subcriber
– Setup RabbitMQ exchange, queues
– Run and check results
<h3>1. Create SpringBoot projects</h3>
Using <strong>Spring Tool Suite</strong>, create 2 <strong>Spring Starter Project</strong>s then add <strong>amqp</strong> dependency for both of them:

More at:

https://grokonez.com/java-integration/distributed-system/rabbitmq-create-spring-rabbitmq-publishsubcribe-pattern-springboot

RabbitMq – How to create Spring RabbitMq Publish/Subcribe pattern with SpringBoot