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.
1. Install the following NuGet package:
2. Add configuration to
Note: I find pretty easy to use CloudAMQP. Alternatively, you can run a Docker container for RabbiMQ on a local machine.
subscriber apps, create a new class called
4. In the
subscriber app, create a new class called
5. In the
publisher app, modify the method
6. In the
subscriber app, create an extension called
7. In the
subscriber app, modify
Configure methods in
8. Publish the
ItemCreatedIntegrationEvent event in the
publisher app by using the following code, for example in a controller.