这是场景:
- Publisher #1 ═══╗ ╔═══ Round Robin ═══╦═══ Subscriber #1 (Service 1)
- ║ ║ ╚═══ Subscriber #2 (Service 1)
- ╠═══ Topic ═══╣
- ║ ║ ╔═══ Subscriber #3 (Service 2)
- Publisher #2 ═══╝ ╚═══ Round Robin ═══╩═══ Subscriber #4 (Service 2)
我有一条消息需要由多个订阅者处理,但每个服务只有一个(每个服务都会运行多个实例).
消息#1需要由订户#1和#3处理.消息#2需要由订户#2和#4处理.消息#3,订户#1和#3再次.基本上,每条消息应循环到订阅每条消息的每个负载均衡服务,按连接的每个服务进行分组.这可能不创建多个主题吗?
即使它不是循环法,我也会尽最大努力在多个服务之间实现负载均衡.这可能吗?
解决方法
1.主题
主题是一种发布/分发机制,每个订阅(订阅者)将发送一次消息.
A topic subscription resembles a virtual queue that receives copies of the messages that are sent to the topic. Messages are received from a subscription identically to the way they are received from a queue…
Subscriptions support the same patterns described earlier in this section with regard to queues: competing consumer,temporal decoupling,load leveling,and load balancing.
资料来源:MSDN Article
您需要在竞争的使用者(服务实例)之间重新使用主题订阅来实现您的方案.
- Publisher #1 ═══╗ ╔═══ Subscription 1 ═══╦═══ Service 1-instance 1
- ║ ║ ╚═══ Service 1-instance 2
- ╠═══ Topic ═══╣
- ║ ║ ╔═══ Service 2-instance 1
- Publisher #2 ═══╝ ╚═══ Subscription 2 ═══╩═══ Service 2-instance 2
- string connectionString = "<Secret>"
- var namespaceManager =
- NamespaceManager.CreateFromConnectionString(connectionString);
- if (!namespaceManager.SubscriptionExists("TestTopic","Inventory"))
- {
- namespaceManager.CreateSubscription("TestTopic","Inventory");
- }
B.收听现有订阅
- MessagingFactory factory = MessagingFactory.Create(uri,tokenProvider);
- MessageReceiver receiver = factory.CreateMessageReceiver("TestTopic/subscriptions/Inventory");
2.队列
使用多个队列也可以适合您的特定场景.具有多个竞争消费者(实例)的每个队列将仅向请求和成功处理它的第一个客户端传递消息一次.
然后设计变成:
- Publisher #1 ═══╗ ╔═══ Service 1 Queue ═══╦═══ Subscriber #1 (Service 1)
- ║ ║ ╚═══ Subscriber #2 (Service 1)
- ╠═══ASB═══╣
- ║ ║ ╔═══ Subscriber #3 (Service 2)
- Publisher #2 ═══╝ ╚═══ Service 2 Queue ═══╩═══ Subscriber #4 (Service 2)