问题描述
我已经创建了一个基本的演示pub/sub应用程序,该应用程序可通过MassTransit在localhost上运行.
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.
我的发布/订阅代码:
发布:
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();
推荐答案
阅读下面的文章时,我发现队列名称必须是唯一的
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/
所以我的订户代码现在看起来像这样:
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();
这篇关于在MassTransit上禁用循环消息消费的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!