c# – Azure服务总线 – 循环到多个服务的主题

前端之家收集整理的这篇文章主要介绍了c# – Azure服务总线 – 循环到多个服务的主题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是场景:
  1. Publisher #1 ═══╗ ╔═══ Round Robin ═══╦═══ Subscriber #1 (Service 1)
  2. ╚═══ Subscriber #2 (Service 1)
  3. ╠═══ Topic ═══╣
  4. ╔═══ Subscriber #3 (Service 2)
  5. 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

您需要在竞争的使用者(服务实例)之间重新使用主题订阅来实现您的方案.

  1. Publisher #1 ═══╗ ╔═══ Subscription 1 ═══╦═══ Service 1-instance 1
  2. ╚═══ Service 1-instance 2
  3. ╠═══ Topic ═══╣
  4. ╔═══ Service 2-instance 1
  5. Publisher #2 ═══╝ ╚═══ Subscription 2 ═══╩═══ Service 2-instance 2

A.创建主题订阅

  1. string connectionString = "<Secret>"
  2. var namespaceManager =
  3. NamespaceManager.CreateFromConnectionString(connectionString);
  4.  
  5. if (!namespaceManager.SubscriptionExists("TestTopic","Inventory"))
  6. {
  7. namespaceManager.CreateSubscription("TestTopic","Inventory");
  8. }

B.收听现有订阅

  1. MessagingFactory factory = MessagingFactory.Create(uri,tokenProvider);
  2.  
  3. MessageReceiver receiver = factory.CreateMessageReceiver("TestTopic/subscriptions/Inventory");

2.队列

使用多个队列也可以适合您的特定场景.具有多个竞争消费者(实例)的每个队列将仅向请求和成功处理它的第一个客户端传递消息一次.

然后设计变成:

  1. Publisher #1 ═══╗ ╔═══ Service 1 Queue ═══╦═══ Subscriber #1 (Service 1)
  2. ╚═══ Subscriber #2 (Service 1)
  3. ╠═══ASB═══╣
  4. ╔═══ Subscriber #3 (Service 2)
  5. Publisher #2 ═══╝ ╚═══ Service 2 Queue ═══╩═══ Subscriber #4 (Service 2)

猜你在找的C#相关文章