A Beginners Guideline to RabbitMq and MassTransit(Part 1): RabbitMq and How To Install it

tong eric
Bina Nusantara IT Division
4 min readDec 29, 2021

A Complete Guideline from Installation to Implementation for RabbitMq as Message Broker

Picture by Luca Bravo on Unsplash

This article is a three-part series about complete implementation of RabbitMq as message broker . You can navigate to other part by link below :

Part 2 : Implement RabbitMQ in Code with MassTransit

Part 3 : User Access Management and Good Practice for Using RabbitMq

In distributed systems, communication between various applications plays an important role. Effectively passing messages between applications was always crucial in system design. One of the solutions to pass messages around your distributed system is a message broker. They bring in decoupling between the applications and provide an effective way to communicate between services. With message brokers, the services need no prior knowledge of their recipients to communicate.

In this tutorial, we will learn about message brokers, more specifically RabbitMQ, and learn about how to implement RabbitMQ as a message broker for our applications starting from setting up the broker itself to implementation in-code. So, stay tuned till the end.

First, let’s start with an explanation of what a message broker is.

Message Broker

In general, a broker is a person who facilitates trades between a buyer and a seller. An example could be a real estate agent or a stockbroker.

Similarly, if we want to trade messages between two distributed software components, we need a mediator. This mediator is known as the message broker. It receives incoming messages from a sender and sends them to a recipient. This way the sender and receiver can be totally isolated.

Another analogy for a message broker can be a Post Office . Let’s take a scenario where you are going to send a letter to your cousin living in another city. Then as per this analogy, you are a producer, your cousin is a consumer, and the post office is a message broker.

RabbitMQ as Message Broker

Now we know that the purpose of a message broker is to route messages from a producer to a consumer. Let’s examine one such message broker — RabbitMQ. It’s one of the most extensively used message brokers these days.

RabbitMQ is an open-source message-broker software that originally implemented the Advanced Message Queuing Protocol (AMQP) and has since been extended with a plug-in architecture to support Streaming Text Oriented Messaging Protocol (STOMP), MQ Telemetry Transport (MQTT), and other protocols.The way RabbitMQ routes messages depends on the messaging protocol it implements. RabbitMQ supports multiple messaging protocols like we mentioned above. But the one that we will use for this tutorial is AMQP.

AMQP or Advanced Message Queuing Protocol depends on 3 important entities: queue, binding and exchange. When a publisher pushes a message to RabbitMQ, it first arrives at an exchange. The exchange then distributes copies of these messages to variously connected queues. Finally, consumers receive these messages.

AMQP is a programmable protocol; programmers have the choice to use libraries to configure entities (exchange, binding, and queue) as per their own needs. A RabbitMQ admin has no role in setting up these entities. There are plenty of libraries available to work with RabbitMQ, you can choose from Nodejs, Python, .Net, Java, and many more. In this tutorial, we will use a library called MassTransit.

Installation of RabbitMQ

So, after we know what RabbitMQ is, we can start to implement it in our solution. Let’s start with the installation and setting up the RabbitMQ server.

Add the required dependencies

Add the RabbitMq repository in the repo configurations file. Open the config file:

sudo vi /etc/yum.repos.d/rabbitmq-server.repo

Add this to the lines below:

[rabbitmq-server]name=rabbitmq-serverbaseurl=https://packagecloud.io/rabbitmq/rabbitmq-server/el/7/$basearchrepo_gpgcheck=1gpgcheck=0enabled=1gpgkey=https://packagecloud.io/rabbitmq/rabbitmq-server/gpgkeysslverify=1sslcacert=/etc/pki/tls/certs/ca-bundle.crtmetadata_expire=300

Add Hostname to the hosts file

RabbitMq is built on top of Erlang. So it would make sense for us to install it beforehand. This part is very crucial as if you do not add the hostname to /etc/hosts then rabbitmqctl status will not work.

echo “127.0.0.1 $(hostname -s)” | sudo tee -a /etc/hosts

Setting Up RabbitMq Server

  1. Install RabbitMq Server

After completing the previous configuration setup steps, install RabbitMq by using this command:

sudo dnf install -y rabbitmq-server

After installation is completed, check if the installation is correct by running:

rpm -qi rabbitmq-server
  1. Open Ports

Centos has a pre-installed firewall called firewalld. It blocks all ports from connecting unless asked to. So we must whitelist the TCP ports that are going to be used by RabbitMq.

sudo firewall-cmd — zone=public — permanent — add-port={5672,15672}/tcpsudo firewall-cmd — reload

2. Start the RabbitMq service

The RabbitMq service doesn’t start on its own when you install it. You have to start it manually, then enable it to run on server startup. To start the RabbitMq service (the service might take a minute or two to start):

sudo systemctl start rabbitmq-server.service

To enable the service run:

sudo systemctl enable rabbitmq-server.service

To check if the service is still running:

sudo rabbitmqctl status

3. Enable RabbitMq Management

If you want to enable the RabbitMq admin dashboard, run:

sudo rabbitmq-plugins enable rabbitmq_management

Then try to open the dashboard from the browser:

sudo rabbitmq-plugins enable rabbitmq_management

Conclusion

To summarize, RabbitMq is an open-source message broker that can be implemented in your business process and only need simple setting and maintenance on it.

--

--