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.
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.
1 |
npm init -y |
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.
1 |
npm install express amqplib |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
const express = require('express') const app = express() const amqp = require('amqplib') app.use(express.json()) var connection, channel const connetToRabbitMQ = async () => { try { const amqpServer = 'amqp://<public_ip>:<port_number>' connection = await amqp.connect(amqpServer) channel = await connection.createChannel() await channel.assertQueue('data-channel') console.log('Connected with RabbitMQ!') } catch(err) { console.log('Error in Connection!') } } connetToRabbitMQ() const addDataToRabbitMQ = async (data) => { await channel.sendToQueue('data-channel', Buffer.from(JSON.stringify(data))) } app.post('/', (req, res) => { addDataToRabbitMQ(req.body) console.log('Data Sent: ', req.body) res.json({ message: 'Data Sent' }) }) const PORT1 = 4000 app.listen(PORT1, () => console.log(`Server is Running at PORT: ${PORT1}`)) |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
const express = require('express') const app = express() const amqp = require('amqplib') const processData = async (data) => { setTimeout(() => { console.log('Data Processed: ', data) }, 10000) } const connectToRabbitMQ = async () => { try { const amqpServer = 'amqp://<public_ip>:<port_number>' const connection = await amqp.connect(amqpServer) const channel = await connection.createChannel() await channel.assertQueue('data-channel') console.log('Connected with RabbitMQ!') channel.consume('data-channel', (data) => { const userData = JSON.parse(Buffer.from(data.content)) processData(userData) channel.ack(data) }) } catch(err) { console.log('Error in Connection!', err) } } connectToRabbitMQ() const PORT2 = 5000 app.listen(PORT2, () => console.log(`Server is Running at PORT: ${PORT2}`)) |
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
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.
Click to Comment