在 MassTransit 上禁用循环消息消费

Disable round-robin message consumption on MassTransit

我已经创建了一个基本演示 pub/sub 应用程序,它可以在本地主机上使用 MassTransit。

我想要实现的是发布一条消息,所有订阅者都应该收到消息。

目前,在我的环境中,我启动了一个发布者应用程序和两个订阅者应用程序。但是当我发布消息时,订阅者轮流接收消息。

我的pub/sub代码:

发布:

var bus = Bus.Factory.CreateUsingRabbitMq(config =>
{
  config.Host(new Uri("rabbitmq://localhost/"), h => { });
  config.ExchangeType = ExchangeType.Fanout;
});
var busHandle = bus.Start();
bus.Publish<SomethingHappened>(message);

订阅者使用此代码:

var bus = Bus.Factory.CreateUsingRabbitMq(config =>
{
  var host = config.Host(new Uri("rabbitmq://localhost/"), h => { });
  config.ReceiveEndpoint(host, "MassTransitExample_Queue", e => e.Consumer<SomethingHappenedConsumer>());
});

var busHandle = bus.Start();
Console.ReadKey();
busHandle.Stop();

在阅读下面的文章时,我发现队列名称必须是唯一的

https://www.maldworth.com/2015/10/27/masstransit-send-vs-publish/

When building your bus and registering an endpoint like so: sbc.ReceiveEndpoint(...), one has to be sure that the queueName parameter is unique.

所以我的订阅者代码现在看起来像这样:

var bus = Bus.Factory.CreateUsingRabbitMq(config =>
{
  var host = config.Host(new Uri("rabbitmq://localhost/"), h => { });
  config.ReceiveEndpoint(host, "MTExQueue_" + Guid.NewGuid().ToString(), e => e.Consumer<SomethingHappenedConsumer>());
});

var busHandle = bus.Start();
Console.ReadKey();
busHandle.Stop();