【问题标题】:How to route message to right queue when working with multiple regional Azure Queues使用多个区域 Azure 队列时如何将消息路由到正确的队列
【发布时间】:2021-04-14 07:23:06
【问题描述】:

我有一个应用程序分发到五个地区,美国、欧盟、中国等。

请求被路由到离客户最近的工作人员,但是请求的某些方面必须根据某些业务逻辑在特定区域中处理,例如,如果客户从 NYSE 请求信息但在欧洲,大部分工作可以在欧洲进行,但有些工作必须在美国地区进行。

当发生这种情况时,我使用正确的 QueueClient 来在正确的区域发送消息,在那里处理敏感工作。

我有一个路由客户端来确定我们何时需要将这些消息发送到正确的区域,这工作正常。但是我发现我需要管理多个队列客户端。

正在寻找一种能够满足这种需求的设计模式,因为今天我正在这样做:

public class MyService : IMyAppService
    {
        private readonly IMyAppQueueEventClient usQueueClient;
        private readonly IMyAppQueueEventClient euQueueClient;
        private readonly IMyAppQueueEventClient cnQueueClient;

        public MyAppService(IMyAppQueueEventClient USQueueClient, 
            IMyAppQueueEventClient EUqueueClient;
            IMyAppQueueEventClient CNqueueClient;
            )
        {
            
            this.usQueueClient = USQueueClient;
            this.euQueueClient = EUQueueClient;
            this.cnQueueClient = CNQueueClient;
            
        }

这感觉真的很笨重。当我添加新区域时,我最终不得不更新构造函数,这导致后续更改以更新测试等。

当然有一种方法可以从 appSettings 中读取数据并创建一个 QueueClients 数组并将它们传入,然后选择正确的。

【问题讨论】:

    标签: azure design-patterns message-queue


    【解决方案1】:

    我解决这个需求的方法是使用装饰器模式。

    我采用了基础 QueueClient 并将它和一个 string Region 属性类型添加到这样的实现中:

    public class RegionalQueueClient
    
       public string region;
       private QueueClient client;
    

    我在这个类中添加了一个public Receipt SendMessage() 方法,它调用私有的QueueClient 成员来传递消息。

    然后我创建了另一种类型,称为我的SuperQueue,其中包含一个List<RegionalQueueClient> 成员。

    我使用 DependencyInjection 将它们全部加载,它就像一个魅力,我能够根据需要使用 Linq 查询为正确的区域找到正确的客户端。

    记录从AppSettings 读取为数组,如seen in this answer.

    【讨论】:

      猜你喜欢
      • 2012-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-01
      • 2013-04-25
      • 2012-04-29
      • 2014-08-30
      相关资源
      最近更新 更多