RabbitMQ Service Bus Explained

Introduction

RabbitMQ is an implementation of an event bus that implements event-based communication between different microservices developed using ASP.NET Core. In this article, we will discuss the architecture of the RabbitMQ service bus.

RabbitMQ Service Bus

RabbitMQ is an open-source message broker widely used in all kinds of enterprises. Here, message broker means, if someone is going to publish a message, then there is someone in the middle like a middle man that will handle and store that message. With the help of the RabbitMQ service bus, a microservice can publish events when an event triggers, as well as other microservices, can subscribe to those events. A microservice can update entities, upon receiving an event, which can lead to more events being published.

RabbitMQ service bus architecture

RabbitMQ

The RabbitMQ service bus acts as a link between multiple microservices through which microservices can publish messages under different numbers of queues available inside the RabbitMQ service bus. Other microservices can later subscribe to these messages available inside RabbitMQ service bus queues. A microservice can update entities, upon receiving an event, which can lead to more events being published.

RabbitMQ service bus API

Following is the description of each and every component inside the RabbitMQ service bus API.

Service bus

Overview

The figure above shows an overview of the RabbitMQ service. The overview tells the number of connections, channels, exchanges, and queues available in the RabbitMQ service at a particular time. On the top right, the RabbitMQ dashboard shows the cluster name which is the system name on which the RabbitMQ service is running. The cluster name identifies the system name, from where the message has been routed through.

The nodes section displays the name of the computer on which the RabbitMQ is running. It also displays how much memory has been consumed and the total disk space available.

It displays statistics of channel and connection operations. There are Export definitions section from where we can export broker definitions to some other server. A RabbitMQ server contains various queues and we can port these queues to some other server, having the same queue definition. Then we can download the broker definitions and import it to some other server.

Connections

The connections column tells about the connections taking place when messages are published within and consumed from the RabbitMQ service bus.

Channels

The channels section shows information about the channel being used to publish and subscribe to messages within a RabbitMQ service bus. It contains details about the user and the transfer rate at which the messages are being transferred.

Exchanges

Exchange helps in routing messages to different queues. There are different kinds of exchanges such as direct, fan out, topics, and headers exchange.

Direct exchange uses a key to go to the valid queue. Fanout sends messages to different instances of the queue.

Queues

RabbitMQ contains different instances of queues, where messages are published. We can define different queues for different microservices as well for events that can be subscribed by other receiver microservices.

Admin

The Admin section mentions the name of the person logged in as a user to the RabbitMQ server. The type of user is admin. We can also set new users by mentioning username, password, and tags that is the category to which each user belongs.

Various commands to handle the RabbitMQ server
 

RabbitMQ stop command

rabbitmqctl stop_app

This command stops the RabbitMQ application on a particular machine.

RabbitMQ start command

rabbitmqctl start_app

This command starts a RabbitMQ application on a particular machine.

Resetting the RabbitMQ command

rabbitmqctl reset

This command resets the entire RabbitMQ application running on a particular machine. We have to stop the application first every time we try to reset it.

Command to add a new user

rabbitmqctl add_user abc 12345

We can create a new user using this command by mentioning its username and password.

Command to assign a particular role to a newly created user

We can also set a tag for any newly created user using the below command.

rabbitmqctl set_user_tags abc administrator

Command to grant permission to a newly created user

We can also set all kinds of permissions to a newly created user such as read/write permission and configure permission using the below command.

rabbitmqctl set_permissions -p/abc “.*” “.*” “.*”

Permissions can also be changed on the RabbitMQ user interface. We can also create add, update, and delete users from RabbitMQ UI.

Below a simple application has been created that will publish a message to the queue and then there is a subscriber or a message receiver who will pick up the message from the queue.

Publisher. cs

using RabbitMQ.Client;
using System;
using System.Text;

namespace RabbitMQApplication
{
    public class Publisher
    {
        public static void Main(string[] args)
        {
            var factory = new ConnectionFactory() { HostName = "localhost" };
            using (var connection = factory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                channel.QueueDeclare("Test", false, false, false, null);
                string message = "creating a message using asp.net core rabbitmq";
                var body = Encoding.UTF8.GetBytes(message);
                channel.BasicPublish("", "Test", null, body);
                Console.WriteLine("sent message:{0}", message);
            }

            Console.ReadLine();
        }
    }
}

Output

Windows

Subscriber. cs

using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;

namespace RabbitMQconsumer
{
    public class Subscriber
    {
        public static void Main(string[] args)
        {
            var factory = new ConnectionFactory() { HostName = "localhost" };
            using (var connection = factory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                channel.QueueDeclare("Test", false, false, false, null);
                var consumer = new EventingBasicConsumer(channel);
                consumer.Received += (model, ea) =>
                {
                    var messagebody = ea.Body;
                    var message = Encoding.UTF8.GetString(messagebody);
                    Console.WriteLine("Recieved message:{0}", message);
                };

                channel.BasicConsume("Test", true, consumer);

                Console.ReadLine();
            }
             
        }
    }
}

Output

ASP.Net

Overview

Questions

Connections

As we can see from the above images when Publisher. cs published messages to the RabbitMQ service bus, there were 4 messages inside the queue. when Subscriber. cs consumed these messages, connections, and channels were created.

Summary

In this article, we have discussed the architecture of the RabbitMQ service bus. We have discussed each and every component belonging to the RabbitMQ service bus API. We have also created a simple application that will publish a message to the queue and then there is a subscriber or a message receiver who will pick up the message from the queue.


Similar Articles