同一消息的多个订阅者 Rebus Azure 服务总线
Multiple Subscribers to same message Rebus Azure Service Bus
我有两个完全相同的消费者
消费者 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
其他一些有用的链接:
- How does Rebus work with Azure Service Bus topics?
- https://github.com/rebus-org/Rebus/wiki/Azure-Service-Bus-transport
我有两个完全相同的消费者
消费者 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
其他一些有用的链接:
- How does Rebus work with Azure Service Bus topics?
- https://github.com/rebus-org/Rebus/wiki/Azure-Service-Bus-transport