问题
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