【Azure 服务总线】Azure.Messaging.ServiceBus 多次发送消息报超时错误,是否可以配置重新发送?是否有内置重试机制?

问题描述

使用 Azure Service Bus,提供应用程序之间松耦合的消息交换,但是有时候发送消息多次出现超时错误。

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

ErrorCode: TimedOut (ServiceCommunicationProblem)

 

为了预防这类偶发的Timedout异常对应用的影响,需要重新再次发送或接收消息,是否有内置的重试机制呢?

 

问题解答

有的,Service Bus的SDK有内置的重试机制,通过ServiceBusRetryOptions 配置。其中,默认的MaxRetries 次数为3次,每次重试的间隔时间默认为 60秒。

 

如在 .NET应用代码中使用示例:

复制代码
using Azure.Messaging.ServiceBus;

string connectionString = "<connection_string>";
string queueName = "<queue_name>";

// Because ServiceBusClient implements IAsyncDisposable, we'll create it
// with "await using" so that it is automatically disposed for us.
var options = new ServiceBusClientOptions();
options.RetryOptions = new ServiceBusRetryOptions
{
    Delay = TimeSpan.FromSeconds(10),
    MaxDelay = TimeSpan.FromSeconds(30),
    Mode = ServiceBusRetryMode.Exponential,
    MaxRetries = 3,
};
await using var client = new ServiceBusClient(connectionString, options);
复制代码

 

对于 Timeout 的异常,如果持续一小段(间断性)发送,可以通过 telnet 或 psping 来查看端口,网络稳定性。

# 测试端口,服务器是否能ping通
telnet <yournamespacename>.servicebus.chinacloudapi.cn 5671


#测试是否存在网络丢包问题
.\psping.exe -n 25 -i 1 -q <yournamespace>.servicebus.chinacloudapi.cn:5671 -nobanner     

 

参考资料

Service Bus的重试策略:https://learn.microsoft.com/zh-cn/azure/architecture/best-practices/retry-service-specific#retry-mechanism-6

Service Bus Timeout 异常 :https://docs.azure.cn/zh-cn/service-bus-messaging/service-bus-troubleshooting-guide#connectivity-certificate-or-timeout-issues

Service Bus Socket 异常 :https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messaging-exceptions#cause-2

 

posted @   路边两盏灯  阅读(171)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2021-11-14 【Azure 环境】用 PowerShell 调用 AAD Token, 以及调用Azure REST API(如资源组列表)
点击右上角即可分享
微信分享提示