.NET Core Microservice using RabbitMQ

I plan to use the Microservice architecture for the project. The selected .NET Core technology stack with Docker and RabbitMQ as a simple service bus, and this should be deployable on Linux .

Suppose I have a Payment service and an Order service, I want each of these services to open REST . Because of this, I was thinking about these two services being .NET Core Web APIs .

But the problem is inter-service communication using RabbitMQ . Whenever I receive a new Order , I want to publish an event using RabbitMQ , and then listen to this event in the Payment service to perform certain operations (database updates). But since these are Web APIs , I don’t think you can listen to events as I described. (I feel that I may have to use something like a console application to subscribe to events.)

I would like to find the most effective method to achieve this, using best practices, given the scalability and extensibility of the system.

+5
source share
1 answer

When you build your application with .NET Core, you load things into the Main method, and then register the services and middle classes in the Startup class. Therefore, before starting web hosting, you can also create and run 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:

An interface was introduced in ASP.NET Core 2.0 IHostedService to achieve the same goal, and they mention this script in their ad. Here is an example of how to implement it.

+5
source

Source: https://habr.com/ru/post/1266897/


All Articles