In this tutorial, we are going to cover the communication between microservices using RabbitMQ and MassTransit with Asp.Net Core 5. In the previous tutorial, we have discussed Microservices Architecture with Ocelot API gateway in Asp.Net Core 5.
What will we build in this tutorial?
In this tutorial, we will build a customer order processing system where the user will order the product. So, we will build two microservices. One is Customer Microservice and the other one is Order microservice. Both services will communicate using the RabbitMQ. So, the customer will order the product using the POST request from the customer’s microservice, and the detail of this order will be sent to the RabbitMQ queue. And then the order microservice will consume it.
What will we learn in this tutorial?
- What is RabbitMQ?
- How RabbitMQ works?
- Advantages of RabbitMQ?
- How to set-up the environment?
- How to create Microservices Architecture?
- What is MassTransit?
- How to configure MassTransit in publisher Microservice (Customer Microservice)?
- How to configure MassTransit in the Consumer Microservice (Order Microservice)?
- How to test Communication between microservices with RabbitMQ using POSTMAN?
What is RabbitMQ?
RabbitMQ is one of the most popular open-source Message-Broker services. We can also say that RabbitMQ is a message-queueing software. It means it provides us the platform which helps us to send and receive messages. A message can include any kind of information like a simple text message, and etc.
How RabbitMQ works?
There are two endpoints, the one is Publisher and the second one is the Consumer. So, the publisher will publish the message to the message broker service. And the RabbitMQ server will store the message in a queue. Now, multiple consumers can subscribe to this queue. And then all Consumers will receive the message whenever they are available. So, using message-broker you will never lose your data.
What are the Benefits of RabbitMQ?
There are a lot of benefits of using a message broker service like RabbitMQ. Here are below some advantages listed.
Reliability: Your messages will never lose once published on the RabbitMQ server even if the consumer is not available due to offline or any technical issue. Once the consumer is back, it will consume the pending message and process it. So, your messages will always be safe even in hardware failure.
Multiple Protocols: It supports multiple protocols such as MQTT, STOMP, AMQP, and etc. Click here if you want to read more about RabbitMQ protocols.
Better User Experience: It has a better user experience due to asynchronous processing. You will be less likely to see any error even when there is a lot of traffic. You will see the system will perform well due to asynchronous processing.
Tracing: If our messaging system is not working fine, then the RabbitMQ offers us tracing support. Using tracing support we can find out what’s going on.
How to set-up the environment?
Now, in this section, we will see how to set up the environment using the below steps.
Install Erlang: So, first-of-all, we need to install Erlang according to our machine. Because Erlang is a programming language and the RabbitMQ server is built on it. So, click here to download the latest installer according to your machine.
Note: – make sure you have installed it with admin rights.
Install RabbitMQ: Now, in this step, we will install the RabbitMQ server. So, click here to download the latest installer according to your machine.
Note: – Make sure you have installed it with admin rights.
Enable RabbitMQ Management Plugin Portal in windows: Once the RabbitMQ server is installed, then we need to activate the Management portal. So, open the command prompt with admin rights and then go to the below location as you do see below in the screenshot.
C:\Program Files\RabbitMQ Server\rabbitmq_server-3.8.11\sbin
Now, run the below command as you do see below in the screenshot. this command will actually run the rabbitmq-plugins batch file which is present inside the sbin folder.
rabbitmq-plugins enable rabbitmq_management
Now, go to this URL (http://localhost:15672/), and then you will see the management portal is up and running.
Now, enter the below credentials, and then you will see the portal up and running.
Username: guest
Password: guest
Note: – If you want to restart the RabbitMQ service, then there are two ways.
1- Go to the services and then RabbitMQ service and then right-click on the RabbitMQ service and then restart it as you do see below in the screenshot.
2- And we can run the below two commands to restart the RabbitMQ service.
net stop RabbitMQ
net start RabbitMQ
How to create Microservices Architecture?
Now, in this step, we will see how to create a Microservices Architecture using Visual Studio 2019. So, go to VS2019 and then follow the below steps.
Step # 1: Create a blank solution
So, go to the VS2019 and then click on the “Create a new project” button and then search for a blank solution and then select “Blank Solution” and then click on the Next button as you do see below in the screenshot.
Now, enter the solution name (E.g. Microservices.WebApi) and then select a solution and then click on the Create button.
Step # 2: Create a Customer Microservice.
Now, right-click on the “Microservices.WebApi” solution name and then add a new folder with the name of Microservices. Now, right-click on the Microservices folder and then select Add and then select New Project… and then select “ASP.NET Core Web Application” and then click on the Next button as you do see below in the screenshot.
Now enter the name (E.g. Customer.Microservice) and then select the location and then click on the Create button as you do see below in the screenshot.
Now, select ASP.NET Core 5.0 and then select ASP.NET Core Web API template and then click on the Create button as you do see below in the screenshot.
After clicking on the Create button, a new customer microservice will be added to the Microservices folder.
Step # 3: Create an Order Microservice.
Now, add another Microservice with the name of Order Microservice as we have added Customer Microservice in the previous section.
Note:- click here to understand how to build microservices architecture using Ocelot API gateway.
Step # 4: Add a shared Model Library
Now, in this step, we will add a new .NET Core library project with the name of Shared.Models. In this library project, we will add a model with the name of Order.
So, right-click on the solution name and then click on the Add and then select New Project and then select Class Library(.NET Core) and then click on the Next button as you do see below in the screenshot.
Now, add a new folder with the name of Models within the Shared.Models library project. Then add a new class with the name of CustomerOrder.cs within the newly created Models folder. Now, add the properties in the CustomerOrder class as you do see below in the file.
What is MassTransit?
It is a lightweight service bus for building distributed .Net applications. The main goal of MassTransit is to provide a consistent, .NET friendly abstraction over the message broker services (whether it is RabbitMQ, Azure Service Bus, etc). It is very helpful for routing messages over RabbitMQ, MSMQ, TIBCO, and ActiveMQ service busses with native support for RabbitMQ and MSMQ. It provides us lots of features like multicast, versioning, encryption, sagas, transactions, distributed systems, retries, and many other features.
So, we will use MassTransit to publish and receive the messages from the RabbitMQ server.
Install MassTransit Packages.
So, now in this section we will install MassTransit in our both services (Customer Microservice, Order Microservice). So, go to Manage NuGet Packages For Solution… and then install these below packages as you see below in the screenshot.
MassTransit
MassTransit.RabbitMQ
MassTransit.AspNetCore
Note:- Now, install other packages (MassTransit.RabbitMQ and MassTransit.AspNetCore) as we have installed MassTransit.
How to configure MassTransit in publisher Microservice (Customer Microservice)?
Now, in this step, we will configure the MassTransit in Customer Microservice. So, go to Customer Microservice and then go to Startup.cs class and then add the services as you do see below in the file’s line # 27 to line # 39.
Now, go to the Controllers folder and then add a new API Controller with the name of CustomerOrder and then write some code as you do see below in the file.
Note:- don’t forget to add a shared model library’s reference.
Let’s understand the above code:
Line # 16 to 20: Here in this block of code, we are injecting the IBus service in the Customer Order Controller.
Line # 27: Here in this line, we are setting the URL and naming the queue as the orderQueue.
Line # 28: Here we are getting the send endpoint for our address.
Line # 29: Here in this line, we are pushing the message to the queue.
How to configure MassTransit in the Consumer Microservice (Order Microservice)?
Now, in this step, we will configure the MassTransit in Order Microservice. So, this microservice will be used for consuming the publisher’s request. So, go to the project folder structure and then create a new folder with the name of Consumers and then add a new class with the name of OrderConsumer.cs and then write some code as you do see below in the file.
Note:- don’t forget to add shared model library’s reference.
So, now go to Startup.cs class and then add the services as you do see below in the file.
Let’s understand the above code
Line # 31: here in this line, we are adding a new consumer with the name of OrderConsumer.
Line # 32 to 47: here in this block of code, we are setting the host credentials and receiving the endpoint.
How to test Communication between microservices with RabbitMQ using POSTMAN?
Now, in this section, we will test communication between microservices using postman. We will test it with two scenarios.
First Scenario: Publisher and Consumer both are online
Second Scenario: Publisher is online and the consumer is offline.
Let’s test first scenario:
So, go to the project folder structure and then right-click on the solution, and then click on the properties. Now, select the Multiple startup projects radio button then start both services and then click on the apply button as you do see below in the screenshot.
Now, run the project, and then you will see both services are up and running. Now, go to the postman and then enter the publisher service URL and then select the method as POST method and then set the message body as you do see below in the screenshot.
and then click on the Send button and then you will see a 200 OK response.
Now, go to the RabbitMQ dashboard, and there you will see a new queue is added as you do see below in the screenshot.
Now, you will see the consumer microservice will consume the request as you do see below in the screenshot.
Our both services are working fine.
Let’s test the second scenario:
So, go to the project folder structure and then right-click on the solution, and then click on the properties. Now, select the Multiple startup projects radio button then start the publisher service and then click on the apply button as you do see below in the screenshot.
Now, run the project, and then you will see publisher service is up and running. Now, go to the postman and then enter the publisher service URL and then select the method as POST method and then set the message body as you do see below in the screenshot.
and then click on the Send button and then you will see a 200 OK response.
Now, go to the RabbitMQ dashboard, and there you will see a new message is queued with zero (0) consumers available as you do see below in the screenshot.
Now, run the consumer application, and then you will see the breakpoint will hit again and the message will be delivered to the consumer as you do see below in the screenshot.
Now, go to the RabbitMQ dashboard, and there you will see the message is delivered and the count of consumers is changed from 0 to 1 as you do see below in the screenshot.
Yes, we have tested both scenarios successfully. Thank you for reading please keep visiting and sharing within your community.
People are also reading:
Hi ,
Great article about RabbitMQ in Asp.Net Core . I found very useful for start with rabitMQ. I just want to know that is there any way to get source code link for this article. If yes , please assist on this.
Thanks in advance.