The Publish-Subscribe (Pub/Sub) pattern is a messaging pattern that allows different components or services of a system to communicate with each other without being directly aware of each other's existence. In this pattern, a message broker acts as an intermediary between publishers (senders) and subscribers (receivers) of messages.
The Pub/Sub pattern is commonly used in distributed systems, event-driven architectures, and microservices-based architectures. Some popular message brokers that implement the Pub/Sub pattern include Apache Kafka, RabbitMQ, and Google Cloud Pub/Sub.
Implement the concept of Publish/Subscribe pattern. It should:-
- Take an input of data
- Transform that data in some way
- Transport that data to a set of subscribers
- The subscribers should be able to display the transformed data
To implement the Pub/Sub pattern based on our agenda, we will use RabbitMQ as a message broker. The .Net Core Web API will act as the publisher, and we will have at least two console applications serving as subscribers. As an addition to our implementation of the Pub/Sub pattern using RabbitMQ as the message broker, let me provide a part of real scenario from the current application I'm working on. For this scenario, we have three applications:
- ResourceEngagement(publisher) - manages resource engagement information and publishes it to the subscribers
- IdentityGovernance(subscriber) - consumes the published message and creates a resource profile in the organization's active directory
- UserTimewrite(subscriber) - consumes the message to store engagement details and verify the resources' input hours.
Please find below the process diagram illustrating how pubsub is working:
- ASP.NET Core 7
- Entity Framework Core 7
- xUnit, Moq
- RabbitMQ
- Docker
- Postman
The publisher project is configured to use an in-memory database by default. This ensures that all users will be able to run the solution without needing to set up SQL Server.
"UseInMemoryDatabase": true
- We have database configuration in
appsettings.json
- We have RabbitMQ configuration in
appsettings.json
like host, user, password etc.
Since we have already implemented the publisher and subscribers applications based on our agenda and scenarios in this repository, you just need to clone it using git clone
in your local machine, let's now look at how we can run them on our local machine.
-
Running RabbitMQ in Docker - Assuming that we have already set up Docker on our machine if not then find instructions from here
-
To spin up RabbitMQ server open command-line and use the
docker run
command as belowdocker run -d --hostname pubsub-rabbitmq-server --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
-
We are using the
rabbitmq:3-management
image from DockerHub which will provide us a UI available on port 15672 to view our queues/messages. -
In order to access the management UI, we need to open a browser and navigate to
localhost:15672
, using the default login of guest/guest.
-
-
Running Application - Ensuring we have both applications set to run on startup befor running applications.
- Simply run by clicking on Start in visula studio, it will open a console two windows (Subscribers) and a browser with the Swagger UI (Publisher).
- I'm using Postman here as publisher instead of using browser swagger.
Step-01 - RabbitMQ management login UI Step-02 - RabbitMQ management dashboaed UI Step-03 - Publisher swagger UI Step-04 - Subscribers and Publisher (Postman) Step-05 - Create a engagement and published Step-06 - Create another engagement and published Step-07 - RabbitMQ queue dashboard Step-08 - RabbitMQ dashboard summary