我有两个完全相同的消费者
消费者1
using (var adapter = new BuiltinHandlerActivator())
{
adapter.Handle<string>(async (bus, message) =>
{
Console.WriteLine("Got message > " + message);
await bus.Reply("Received in consumer 1");
});
Configure.With(adapter)
.Transport(t => t.UseAzureServiceBus(connectionString, "server"))
.Start();
adapter.Bus.Subscribe<string>().Wait();
Console.WriteLine("Press ENTER to quit");
Console.ReadLine();
}
消费者2
using (var adapter = new BuiltinHandlerActivator())
{
adapter.Handle<string>(async (bus, message) =>
{
Console.WriteLine("Got message > " + message);
await bus.Reply("Received in Consumer 2");
});
Configure.With(adapter)
.Transport(t => t.UseAzureServiceBus(connectionString, "server"))
.Start();
adapter.Bus.Subscribe<string>().Wait();
Console.WriteLine("Press ENTER to quit");
Console.ReadLine();
}
生产者
using (var adapter = new BuiltinHandlerActivator())
{
adapter.Handle<string>(async message =>
{
Console.WriteLine("Returned > " + message);
});
var bus = Configure
.With(adapter)
.Transport(t => t.UseAzureServiceBus(connectionString, "client"))
.Routing(r => r.TypeBased().Map<string>("server"))
.Start();
Console.WriteLine("Press Q to quit or any other key to produce a job");
while (true)
{
Console.Write("Write something > ");
var text = Console.ReadLine();
if (string.IsNullOrWhiteSpace(text)) break;
bus.Publish(text).Wait();
}
}
我所期望的是,每当我从生产者那里发送消息时,我的两个消费者都会显示消息。现在它只在其中一个身上起作用。当我关闭那个并发送另一条消息时,剩下的一条会收到
基本上,您只需要为消费者提供不同的名称。Rebus为每个生产者创建一个主题(基于程序集、命名空间和类型(,并为这些主题中的每个消费者创建订阅。如果两个消费者使用相同的名称,他们就会争夺消息。
.Transport(t => t.UseAzureServiceBus(connectionString, "consumer1"))
.Transport(t => t.UseAzureServiceBus(connectionString, "consumer2"))
完整示例:https://github.com/rebus-org/RebusSamples/tree/master/PubSubNative
其他一些有用的链接:
- Rebus如何处理Azure服务总线主题
- https://github.com/rebus-org/Rebus/wiki/Azure-Service-Bus-transport