处理消息以及长期重试策略

Handling messages along with long-term retry policy

机器翻译专家您好。

在我的应用程序中,我有默认的重试策略,即每 3 分钟发送一次消息,总共 30 分钟。如果有许多失败的消息受此策略影响(超过 16 个),则不会处理其他消息(即使是成功的消息)。这是一个大问题,因为如果有 16 条消息被破坏,那么整个队列将被阻塞 30 分钟。

我确定有解决方案,但我还没有找到。

我认为您正在寻找断路器模式,这可以通过以下方式应用于公共交通:

cfg.ReceiveEndpoint(host, "customer_update_queue", e =>
{
    e.UseCircuitBreaker(cb =>
    {
        cb.TrackingPeriod = TimeSpan.FromMinutes(1);
        cb.TripThreshold = 15;
        cb.ActiveThreshold = 10;
        cb.ResetInterval = TimeSpan.FromMinutes(5);
    });
    // other configuration
});

可以在文档中找到更多信息: http://masstransit-project.com/MassTransit/advanced/middleware/circuit-breaker.html

解决方案是重新投递,也就是二级重试。

这里是 documentation.

有两种使用方式:

来自消费者的显式重新交付,调用异常:

public async Task Consume(ConsumeContext<ScheduleNotification> context)
{
    try
    {
        // try to update the database
    }
    catch (CustomerNotFoundException exception)
    {
        // schedule redelivery in one minute
        context.Redeliver(TimeSpan.FromMinutes(1));
    }
}

或使用配置和策略(端点配置委托的一部分):

ep.Consumer<CSomeConsumer>(c => c.Message<SomeMessage>(
    x => x.UseDelayedRedelivery(
        p =>
        {
            p.Handle<SqlException>(e => e.Message.Contains("Timeout"));
            p.Exponential(40, TimeSpan.FromSeconds(10), TimeSpan.FromHours(1),
                TimeSpan.FromSeconds(4));
        })));

请记住,您必须具有 scheduling configured to use this feature. It can be done using Quartz or RabbitMQ/AzureSB 集成的日程安排功能。