Disable round-robin pattern and use fanout on MassTransit

时光毁灭记忆、已成空白 提交于 2019-12-01 04:09:51

问题


I have created a basic demo pub/sub application which works on localhost with MassTransit.

What I want to achieve is to publish a message and all the subscribers should receive the message.

At the moment, in my environment I start one publisher app and two subscriber apps. But when I publish a message the subscribers receive the message in turns.

I thought the fanout exchange type was default? But it applies the round-robin pattern.

I added

config.ExchangeType = ExchangeType.Fanout;

explicitly, but the consumers receive the message still one by one...

My pub/sub code:

Publish:

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);

Subscribers use this code:

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();

回答1:


When reading the article below I found that the queue name must be unique

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.

So my subscribers code looks like this now:

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();


来源:https://stackoverflow.com/questions/39573721/disable-round-robin-pattern-and-use-fanout-on-masstransit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!