AWS, Azure, Cloud Computing

3 Mins Read

Step-By-Step Guide to Establish Communication between two Microservices using RabbitMQ

Voiced by Amazon Polly

How To Establish Communication Between Two Microservices Using Rabbitmq

RabbitMQ is a popular open-source message broker that is extensively used in building large-scale distributed systems. It gives your application a common platform to send and receive messages, and your letters are safe to live until received.

RabbitMQ

Before moving onto the RabbitMQ, we need to understand what exactly message queue means? A message queue is made up of two components, and the first one is the producer, which will be responsible for pushing the incoming messages into the queue. The second component is the consumer, which is used to retrieve those messages.

Let us understand with the help of an example. Suppose we are building a food delivery application like Swiggy/Zomato. Now, in this application, let us assume that we have two microservices. The first one is an order microservice, which will take orders from the customers. The second is the billing service, which will be responsible for generating the invoice for those orders. So, when a customer orders something, the order service will receive those orders and push the data into the message queue. After that, the billing service will obtain the same data to calculate the total amount of the order placed.

The question he is, why are we doing this? If the billing service goes down, it is impossible to generate invoices, and the data will be lost altogether. But because of this queue, we are making every request persistent. When the billing service is up, it will start processing all these orders.

We will see how to create two micro service applications, node.js, and transmit messages from one service to another using RabbitMQ.

For that, make sure node and npm are installed on your machine.

The first step is to create two directories with the name producer and consumer. Then, inside both these directories, you need to initialize npm. You can use the following command for that.

There are a couple of libraries that you need to install. The first one is express, and the second is amqplib. Use the below command to install those libraries.

You need to install all this in both the directories.

Express we are using just to set up a server on some port number, and amqplib is a module related to RabbitMQ.

There are a couple of options to set up the RabbitMQ server. Either you can create a virtual machine on the cloud and install RabbitMQ there or use docker to create a container of it. If you don’t want to do all this, you can also set it up on your machine. In this blog, we are using a container running on a remote machine. We are accessing that server with the public IP.

Below is the code that you can paste into the producer directory.

You can use Postman to send any JSON data through a POST request to this server. The next step is to create a consumer service that will grab the queue’s data.

The code is pretty much similar for both producer and consumer. In this code, we are first grabbing the data from the queue, and the acknowledgment is sent to the producer that the data is successfully received.

In this case, the services are up and running; now, you can make consumer service go down. After that, if you send any data using the producer service, it will be stored in that queue. Then, when you make consumer service up, it will automatically fetch the data and acknowledge it. So, that’s the beauty of using messaging queues. Your data will never get lost.

If you have any doubts, please let me know in the comment section, and I will get back to you quickly.

Get your new hires billable within 1-60 days. Experience our Capability Development Framework today.

  • Cloud Training
  • Customized Training
  • Experiential Learning
Read More

WRITTEN BY CloudThat

CloudThat is a leading provider of cloud training and consulting services, empowering individuals and organizations to leverage the full potential of cloud computing. With a commitment to delivering cutting-edge expertise, CloudThat equips professionals with the skills needed to thrive in the digital era.

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!