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 middle like a middle man that will handle and store that message. With the help of 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

 
 
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:
 
 

 

Overview

 
The figure above shows an overview of the RabbitMQ service. The overview tells the number of connections, channels, exchanges, and queues available in RabbitMQ service at a particular time. On the top right, the RabbitMQ dashboard shows the cluster name that is system name on which 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 being 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 messages within a RabbitMQ service bus. It contains details about the user and 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 mentioning username, password, and tags that is the category to which each user belongs.

Various command 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
  1. using RabbitMQ.Client;  
  2. using System;  
  3. using System.Text;  
  4.   
  5. namespace RabbitMQApplication  
  6. {  
  7.     public class Publisher  
  8.     {  
  9.         public static void Main(string[] args)  
  10.         {  
  11.             var factory = new ConnectionFactory() { HostName = "localhost" };  
  12.             using (var getconnection = factory.CreateConnection())  
  13.             using (var passage = getconnection.CreateModel())  
  14.             {  
  15.                 passage.QueueDeclare("Test",false,false,false,null);  
  16.                 string message = "creating a message using asp.net core rabbitmq";  
  17.                 var body = Encoding.UTF8.GetBytes(message);  
  18.                 passage.BasicPublish("","Test",null,body);  
  19.                 Console.WriteLine("sent message:{0}", message);  
  20.             }  
  21.               
  22.             Console.ReadLine();  
  23.         }  
  24.     }  

Output
 
 
 
Subscriber.cs
  1. using RabbitMQ.Client;  
  2. using RabbitMQ.Client.Events;  
  3. using System;  
  4. using System.Text;  
  5.   
  6. namespace RabbitMQconsumer  
  7. {  
  8.     public class Subscriber  
  9.     {  
  10.         public static void Main(string[] args)  
  11.         {  
  12.             var factory = new ConnectionFactory() { HostName = "localhost" };  
  13.             using (var connection = factory.CreateConnection())  
  14.             using (var channel = connection.CreateModel())  
  15.             {  
  16.                 channel.QueueDeclare("Test"falsefalsefalsenull);  
  17.                 var consumer = new EventingBasicConsumer(channel);  
  18.                 consumer.Received += (model, ea) =>  
  19.                 {  
  20.                     var messagebody = ea.Body;  
  21.                     var message = Encoding.UTF8.GetString(messagebody);  
  22.                     Console.WriteLine("Recieved message:{0}", message);  
  23.                 };  
  24.                   
  25.                 channel.BasicConsume("Test"true, consumer);  
  26.                   
  27.                 Console.ReadLine();  
  28.             }  
  29.              
  30.         }  
  31.     }  

Output
 
 
 
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, connection 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.