c# - 服务总线多命名空间故障转移循环

标签 c# azure azureservicebus

我想知道服务总线客户端或库中是否有内置机制支持循环方法来发送或发布消息?我的意思是,如果:

  • 我有两个命名空间 EUN 和 EUW(标准层)
  • 我不想向两者发送消息,而是尝试第一个命名空间 (EUN),如果失败,则尝试第二个命名空间 (EUW)

不幸的是,由于不使用高级层,因此无法选择配对命名空间。到目前为止,我处理这个问题的方法是尝试 EUN 将 try catch 包裹起来,然后尝试 EUW。它比那更优雅一点,但简而言之,这就是我正在做的事情。

最佳答案

There is no built-in mechanism in the Azure Service Bus client or library that supports a round-robin approach to send or publish messages.

您可以通过在第一个命名空间 (EUN) 周围包装一个 try-catch block 来实现自定义解决方案,并在第一次尝试失败时尝试将消息发送到第二个命名空间 (EUW)。

您还可以使用file share-based failover configuration “消息复制任务模式 - Azure 服务总线”<引用 1> 共享终结点信息。

您可以将主要端点的名称放入纯文本文件中,并从能够抵御中断且仍允许更新的基础设施中提供该文件。

示例代码

 static void Main(string[] args)
        {
            string primaryNamespace = "Test1Namespace1";
            string secondaryNamespace = "Test2Namespace2";

            try
            {
                SendMessage(primaryNamespace).Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Sending message to primary namespace failed. Exception: " + ex.Message);
                Console.WriteLine("Trying to send message to secondary namespace...");
                SendMessage(secondaryNamespace).Wait();
            }
        }

        private static async Task SendMessage(string namespaceName)
        {
            var connectionString = $"Endpoint=sb://namespace.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=";
            var queueClient = new QueueClient(connectionString, "testqueue");

            var message = new Message(Encoding.UTF8.GetBytes("Test message"));
            await queueClient.SendAsync(message);

            Console.WriteLine("Message sent successfully.");
        }

消息到队列

enter image description here

消息发送到 Azure 门户中的队列。

enter image description here

有关更多信息,请参阅 MSDoc.

关于c# - 服务总线多命名空间故障转移循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75754334/

相关文章:

c# - 以最小的质量损失调整大小

c# - Visual Studio 2022 中的 Microsoft Identity Platform 依赖项配置

c# - Azure 服务总线 - 多个主题?

azure - 在 Azure 门户中为事件类型 Microsoft.Storage.BlobCreated 创建主题筛选规则

azure - 确保服务总线安全

c# - 沙箱 AppDomain 跨程序集异常处理

c# - 支持 C# 和 F# 的最简单的 Visual Studio

c# - SQLSERVER : Can it be enabled differently? 中所有著名的 SA 登录

Azure DNS - 公共(public) Internet 未找到域

asp.net-mvc - Visual Studio Online 网站以 Debug模式部署到 Azure