【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
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2021-11-14 【Azure 环境】用 PowerShell 调用 AAD Token, 以及调用Azure REST API(如资源组列表)