天天看點

【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秒。

【Azure 服務總線】Azure.Messaging.ServiceBus 多次發送消息報逾時錯誤,是否可以配置重新發送?是否有内置重試機制?

如在 .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​​

繼續閱讀