您有两个应用程序使用相同的使用者组访问事件中心。要么重新设计您的体系结构以拥有一个客户端应用程序,要么为每个客户端使用一个新的消费者组。如果您试图并行处理,请从中心提取一批事件,然后并行处理它们,不要在同一消费者组上有多个读卡器。
或
事件中心在内部将分区从一台主机移动到幕后的另一台主机。在这种情况下,在代码中使用简单的重试,这应该可以工作。为此,我使用
Polly
. 实际上,在实践中,在重试中封装调用“云”的代码通常是一个好主意。
Polly Nuget package
):
namespace RetryPolicies
{
using Microsoft.Extensions.Logging;
using Polly;
using Polly.Retry;
using System;
public class ExponentialRetryManager
{
private static readonly int MAX_RETRIES = 6;
private RetryPolicy _retryPolicy;
/// <summary>
/// An exponential retry manager that will wait as follows between retries:
///
// 2 ^ 1 = 2 seconds first, then
// 2 ^ 2 = 4 seconds then
// 2 ^ 3 = 8 seconds then
// 2 ^ 4 = 16 seconds then
// 2 ^ 5 = 32 seconds
// 2 ^ 6 = 64 seconds
/// </summary>
public ExponentialRetryManager(ILogger logger, String exceptionName = null)
{
_retryPolicy = Policy
.Handle<Exception>()
.WaitAndRetry(MAX_RETRIES, retryAttempt =>
TimeSpan.FromSeconds(Math.Pow(1, retryAttempt)),
(ex, timeSpan, retryAttempt, context) =>
{
logger.LogWarning($"Warning! [RetryAttempt={retryAttempt.ToString()}],[Ex={ex.ToString()}]");
});
}
/// <summary>
/// Executes the passed in action using the retry policy in order to
/// keep attempting to process until the MAX_RETRIES are reached.
/// </summary>
/// <param name="action"></param>
public void Retry(Action action)
{
_retryPolicy.Execute(() => action());
}
}
}
你可以这样称呼它:
var retryManager = new ExponentialRetryManager(log);
retryManager.Retry(() => YourMethodToProcessEventHub());