Introduction
RabbitMQ is a centralized message broker based on the AMQP (Advanced Message Queuing Protocol) protocol, acting as a Middleware between Producers and Consumers of different systems.In a message system, Publishers sends a message to a message broker where messages are consumed some time later by one or more Subscribers.
By introducing a message brokeer between systems we are decoupling the sender application from the receiver. In this case the service that is responsible for sending or publishing the message does not need to know about any other service. All it needs to care about is the message and its format.
With a message system you send a message to a message broker first and when the consumers or listeners of it become online they can start consuming from the message queue. This means you can keep sending messages without even care if the other application is online or if they had any failures.
RabbitMQ Architecture
Exchange, queue and bindings are the main components in the RabbitMQ architecture. In RabbitMQ, the message is first sent to an Exchange. In order for it to know which queue or queues the message should be route to we need to bind an Exchange to one or more Queues and provide a routing key for it.In this way, Publishers just needs to know about the exchanges they are sending the messages to and the routing key that binds the exchange to a queue, while consumers just need to care about the queues itself.
Running RabbitMQ in Docker
If you want to be able to access the RabbitMQ Management Panel you need to run the following RabbitMQ docker image:docker run -d --hostname rabbit --name rabbit -p 5672:5672 -p 5671:5671 -p 15672:15672 -p 15671:15671 rabbitmq:3-management
By exposing the port 15672 we can get access to the Management panel of RabbitMQ. The default username and password are both: guest. To take advantage of Rabbit default configuration for Spring we are also exposing the port 5672. In this way we can use RabbitTemplate to send a message to our RabbitMQ message broker without providing any configuration so ever.
Creating the Exchange, Queue and Bindings
To create an exchange go to the Exchange top menu and click in the "Add a new exchange". Add a name for your new exchange and click in the Add exchange button to save your changes. Click in the top Queue menu to create a new queue. Click in the Add a new queue and give a name for it. Click in the Add queue to save your changes. To create the binding between the Exchange and the Queue, go back to the Exchange page and click in your Exchange name in the available table in the page. Click in the Bindings section to enter a Queue to bind to this Exchange and enter a Routing key. Click in the Bind button to save your changes. We will need the routing key to send a message to the exchange from Spring using the RabbitTemplate.Adding RabbitMQ to a Spring Boot project
To be able to use RabbitMQ and RabbitTemplate in Spring we need to add the following dependency:implementation 'org.springframework.boot:spring-boot-starter-amqp' testImplementation 'org.springframework.amqp:spring-rabbit-test'
Next we are going to create a Service class, called RabbitService. All you need to do in your new class is to inject the RabbitTemplate using Spring Dependency Injection by adding the @Autowired annotation in the property field as you can see in the following code.
The above convertAndSend method from the RabbitTemplate class will convert our Java Message object to an Amqp Message. As we said before, besides the message, we just need to pass the exchange name and the routing key to the method. That's all we need to do in order to publish a message to the RabbitMQ broker using Spring Boot.
If you start your Spring Boot Application you will note that Spring tries to connect to Rabbit at localhost:5672. That's why we exposed that port in our docker command in the beginning of this article.
Let's now add Swagger as an application dependency so that we can use it to send a message to our RabbitMQ.
Adding Swagger
To add Swagger, we just need to add the following dependency to our project:implementation 'org.springdoc:springdoc-openapi-ui:1.6.4'
The Swagger UI shows us all the resources available from our API, as we can see in the image below: Besides providing all the API documentation for other developers that need to interact with the API, we can also make use of the Swagger UI to test our endpoints by accessing the following url:
http://localhost:8080/swagger-ui/index.html
From the image we can see that we have a message resource that we can interact with by sending a POST request to it. If you open that resource in the Swagger UI and click in the Try it out button you will see that Swagger provides the json object required for the payload. We just need to fill with the desired values to test our application API from the browser using the Swagger UI editor. After executing the POST request using the Swagger UI we can see that the server responded with an HttpStatus code 201, indicating that our request completed successfully, since that is the status code that we are returning from the controller.
Getting the message from the Rabbit Management panel
To see that our message indeed was successfully sent to our message queue we can go back to the RabbitMQ Management panel and click in the Queues menu once again. The first thing you will notice is that the Queue graph now shows that we have one message ready to be consumed. We can now go to the Get messages section and enter the number 1 in the Messages input field in the form and click in the Get Message(s) button to view it.Conclusion
In the next article we are going to talk more about other configurations options. This time is important to interact with the Rabbit Management panel so that we can understand later what the configuration is doing for us. This understanding and manual creation of Exchanges, Queues and Bindings will be of great knowledge whenever you need to check why is a message not being sent to a certain queue.In the next article, we will also cover the consumer part of RabbitMQ with Spring Boot.
Github project
GitHub RepoReferences
AMQP ConceptsSWAGGER UI
SPRING GUIDE TO RABBITMQ
SPRING INTRO TO RABBITMQ
RABBITTEMPLATE DOC
Comments
Post a Comment