Warm tip: This article is reproduced from serverfault.com, please click

MassTransit: Configure queue for multiple API instances

发布于 2020-12-02 15:21:21

I'm introducing myself into Event-driven architecture using MassTransit with RabbitMQ on localhost and AWS on live environments, using .NET 5 Web APIs.

I have an API (#API-1) which produces events, such as EntityCreated, EntityUpdated, EntityDeleted, etc.

Then I have another API (#API-2) which stores an in-memory copy of entities. This #API-2 must subscribe to those events in order to update its local cache. The #API-2 will have multiple running instances, let's say 3.

  • How should I configure #API-2 so that all of the instances receive the same message?

Producer Configuration (#API-1):

services.AddMassTransit(x =>
{
    x.UsingRabbitMq();
});
services.AddMassTransitHostedService();

Consumer Configuration (#API-2):

services.AddMassTransit(x =>
{
    x.AddConsumer<EntityCreatedConsumer>();
    x.AddConsumer<EntityUpdatedConsumer>();
    x.AddConsumer<EntityDeletedConsumer>();

    x.UsingRabbitMq((context, cfg) =>
    {
        cfg.ConfigureEndpoints(context);
    });
});
services.AddMassTransitHostedService();
Questioner
GomalId
Viewed
0
Alexey Zimarev 2020-12-02 23:52:47

You need to use different endpoint names. Each endpoint gets its own queue and binding, so if you use the same endpoint name, you get one queue and all instances started to compete for messages.

It is a common mistake, that's why it's even mentioned in the documentation.