I am planing to use Microservice architecture for a project. The selected technology stack is .NET Core
with Docker
and RabbitMQ
as a simple service bus and this should be able to deploy on Linux
.
Lets say I have a Payment
service and an Order
Service, I want each of these services to expose REST
endpoints. Because of that, I thought of making these two services as .NET Core Web APIs
.
But the problem is the inter-service communication using RabbitMQ
. Whenever I get a new order
, I want to publish an event using RabbitMQ
and then listen to that event in Payment
service to perform certain operations (database updates). But since these are Web APIs
, I don't think it's possible to listen to events as I described. (I feel like I might have to use something like a console application to subscribe to events.)
I would like to find the most viable method to achieve this using the best practices considering the scalability and extendability of the system.
RabbitMQ Setup with Docker Or we can add RabbitMQ image into Docker-Compose File for Multi-Container Docker Environment. — We set rabbitmq configuration as per environment variables. Finally, we can create RabbitMq image for our Basket and Ordering microservices.
The two commonly used protocols are HTTP request/response with resource APIs (when querying most of all), and lightweight asynchronous messaging when communicating updates across multiple microservices.
Microservices that communicate in an asynchronous manner can use a protocol such as AMQP to exchange messages via a message broker. The intended service receives the message in its own time. The sending service is not locked to the broker. It simply fires and forgets.
When you create your application with .NET Core you bootstrap things in Main
method and then register services and middle-ware in Startup
class. So before you start web host you can also create and start your messaging services:
public class MessageListener
{
public void Start()
{
// Listen to rabbit QM and all the things.
}
}
public static void Main(string[] args)
{
// Listen to messages.
var messaging = MessageListener();
messaging.Start();
// Configure web host and start it.
var host = new WebHostBuilder()
...
.Build();
host.Run();
}
UPDATE:
In ASP.NET Core 2.0 IHostedService
interface was introduced to achieve same goal and they mention this scenario in their announcement. Here is an example of how to implement one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With