Step-By-Step Guide for Establishing Communication Between Two Microservices Using RabbitMQ

RabbitMQ is an open-source message broker. It is widely used to build large-scale distributed systems. It provides a common platform for your application to send and receive messages. Your letters are also safe until they are received.

Before we move onto RabbitMQ, let’s first understand what a message queue is. A message queue is composed of two components. The first is the producer. This is responsible for pushing the messages into the queue. The consumer is responsible for retrieving those messages.
Let’s look at an example to help us understand. Let’s say we are creating a food delivery app like Swiggy/Zomato. Let’s say that we have two microservices in this application. The first is the order microservice. This will allow customers to place orders. The billing service will generate the invoice for these orders. The order service will receive a customer’s order and push it into the message queue. The billing service will then receive the same data to calculate total order amount.
The question he has is: Why are we doing this? If the billing service is down, it will not be possible to generate invoices and the data may be lost forever. We make every request persistent because of this queue. All these orders will be processed once the billing service is operational.
We will show you how to create node.js and RabbitMQ to send messages from one service to the other.
Make sure that node and npm have been installed on your machine.
First, create two directories with the names producer and consumer. Next, create two directories with the names producer and consumer. For this, you can use the following command.
npm install -y1npm install -yThere are two libraries you will need to install. The first is express, while the second is amqplib. To install these libraries, use the following command.
npm installation express amqplib1npm installer express amqplibAll this needs to be installed in both directories.
Express is used to set up a server at a port number. amqplib refers to RabbitMQ.
There are two options for setting up RabbitMQ servers. You can either create a virtual machine in the cloud and install RabbitMQ from there, or you can use docker to create containers of it. You can also install it on your own machine if you don’t want all that. This blog uses a container that runs on remote machines. We access the server via the public IP.
Below is the code you can copy and paste into the producer directory.
const express = require(‘express’)const app = express()const amqp = require(‘amqplib’)app.use(express.json())var connection, channelconst connetToRabbitMQ = async () => { try { const amqpServer = ‘amqp://:’ connection = await amqp.connect(amqpServer) channel = await connection.createChannel() await channel.assertQueue(‘data-channel’) console.log(‘Connected with RabbitMQ!’) catch(err) console.log(‘Connected with RabbitMQ!’) }}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 = 4000app.listen(PORT1, () => console.log(`Server is Running at PORT: $PORT1`))1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253const express = require(‘express’)const app = express()const amqp = require(‘amqplib’)app.use(express.json())var connection, channelconst connetToRabbitMQ = async () => { try { const

Author: Kody