Adding RabbitMq and MassTransit
This is the fourth post in the microservices architecture series.
The goal of this post is to use the MassTransit library to publish a message to RabbitMq and have that message consumed by an Api.
Sample Application
Download the sample app from here and walkthrough the changes in the next section.
Source Code Changes
SampleApi1
-
SampleApi1.csproj - Added
MassTransit
,MassTransit.RabbitMq
andAspNetCore.HealthChecks.RabbitMq
. -
SubmitOrderConsumer.cs - The class that consumes the message once it is published on the queue.
-
Program.cs -
AddMassTransit
is configured. The endpointorder-service
is configured and bound to the exchangecustomer-order
.
SampleApi2
-
SampleApi2.csproj - Added
MassTransit
,MassTransit.RabbitMq
andAspNetCore.HealthChecks.RabbitMq
. -
Program.cs -
AddMassTransit
is configured. A request client is bound to the exchangecustomer-order
. -
SubmitOrderController.cs - It has a post method that publishes a
SubmitOrder
request.
Verification
-
F5
should start debugging the docker compose file. -
Test the
SubmitOrderController
. -
It is better to add a breakpoint to the
Post
method ofSubmitOrderController
and theConsume
method ofSubmitOrderConsumer
to see the flow.