使用 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