Intro
When developing microservices, the question arises about providing information from one service to other services as events happen. It is essential to have a system without coupling the senders to the receivers. in this case, the Publisher-Subscriber pattern is used.
There are many messaging products and services on the market that support a publish-subscribe model such as Azure Service Bus, RabbitMQ or Apache Kafka. It is strongly recommended to use existing techs rather than building your own.
Below, we are going to see how easy set up event-based communication by using RabbitMQ.
How-To
1. Install the following NuGet package:
Install-Package EventBus.RabbitMQ.Standard
2. Add configuration to appsettings.json
.
Note: I find pretty easy to use CloudAMQP. Alternatively, you can run a Docker container for RabbiMQ on a local machine.
3. In publisher
and subscriber
apps, create a new class called ItemCreatedIntegrationEvent
.
4. In the subscriber
app, create a new class called ItemCreatedIntegrationEventHandler
.
5. In the publisher
app, modify the method ConfigureServices
in Startup.cs
.
6. In the subscriber
app, create an extension called EventBusExtension
.
7. In the subscriber
app, modify ConfigureServices
and Configure
methods in Startup.cs
.
8. Publish the ItemCreatedIntegrationEvent
event in the publisher
app by using the following code, for example in a controller.
Latest comments (0)