简体   繁体   English

Azure函数中的Service Bus死信队列处理

[英]Service Bus Dead Letter Queue Processing In Azure Functions

My environment.我的环境。

  • do.net 6做.net 6
  • Azure Functions ( do.net-isolated) Azure 功能(与 do.net 隔离)
  • Azure Service Bus Azure 服务总线

Now I have two functions with ServiceBusTrigger.现在我有两个 ServiceBusTrigger 函数。 One handle main queue message and another for handling dead letter queue message.一个处理主队列消息,另一个处理死信队列消息。

Scenario设想

  • When main queue message process get failed ( for 2 times) then message moves to dead letter queue.当主队列消息进程失败(2 次)时,消息将移至死信队列。
  • Now it trigger the dead letter queue message and start retrying.现在它触发死信队列消息并开始重试。 Here it seems to be infinite loop.这里好像是无限循环。 How to move message to another queue if it is failing at DLQ level.如果消息在 DLQ 级别失败,如何将消息移动到另一个队列。 Is there any way to resubmit message to queue again.有什么办法可以重新提交消息以再次排队。

Update-1更新-1

  • Scenario is that there is something wrong with consumer that processing that message.场景是处理该消息的消费者出现问题。
  • Now when message first land in Main queue and then it process by consumer and something went wrong due to some dependency issue with consumer.现在,当消息首先进入主队列然后由消费者处理时,由于消费者的某些依赖性问题而出现问题。 So after some retry message will land in DLQ.因此,经过一些重试消息将登陆 DLQ。
  • Now DLQ use same business code to process message but it is from different function. As there is something wrong with dependency so again message will get fail.现在 DLQ 使用相同的业务代码来处理消息,但它来自不同的 function。由于依赖关系有问题,所以消息再次失败。 Now in DLQ it processing infinite.现在在 DLQ 中它处理无限。 I want to move this message to some other queue and from there some manual intervention required to process that message further.我想将此消息移至其他队列,并从那里需要一些手动干预来进一步处理该消息。
  • In high traffic application it is possible that before above decision has made there are many message in queue/DLQ.在高流量应用程序中,在做出上述决定之前,队列/DLQ 中可能有很多消息。

The process which picks up the messages from the DLQ should not be something that has chances of failing so often.从 DLQ 中获取消息的过程不应该经常失败。 The fact that messages in the DLQ are going into loop itself suggests that something is really wrong here. DLQ 中的消息进入循环本身的事实表明这里确实有问题。

Can you elaborate what your process is doing and why is it failing?您能否详细说明您的流程在做什么以及为什么会失败? Are you triggering the same functionality from the main queue as you are from the DLQ?您是否从主队列触发了与 DLQ 相同的功能? That would not be wise.那是不明智的。 If you can look at the root cause of the failures, you will have less messages landing up in the DLQ to handle.如果您可以查看失败的根本原因,那么进入 DLQ 需要处理的消息就会减少。

Think of it this way.这样想。 Would you need the additional queue to retry and then push it to another queue?您是否需要额外的队列来重试然后将其推送到另一个队列? How many more queues would you need before you finally move it to a dead letter queue?在最终将其移动到死信队列之前,您还需要多少个队列? What would you do with the dead letter queue then?那么你会如何处理死信队列呢?

Anyhow, Service bus will not automatically move messages from the DLQ to another queue.无论如何,服务总线不会自动将消息从 DLQ 移动到另一个队列。 But you can trigger your own process to write it into another queue if you really need to.但是,如果确实需要,您可以触发自己的进程将其写入另一个队列。 Service bus triggered Azure functions is perfect to do such work.服务总线触发 Azure 功能非常适合做这样的工作。

To break that infinite loop from the DLQ, add a check for the delivery count value from the Brokered properties.要从 DLQ 中打破无限循环,请从 Brokered 属性中添加对传递计数值的检查。 If this value is greater than a certain threshold, then write the message into another queue.如果这个值大于某个阈值,则将消息写入另一个队列。 The delivery count is incremented each time the message is picked up.每次消息被拾取时,传递计数都会增加。

An example of this in .NET is shown below.下面显示了 .NET 中的一个示例。 I'm simulating a failure to illustrate.我正在模拟失败来说明。

using System;
using System.Threading.Tasks;
using Microsoft.Toolkit.Services;

public static void Run(string myQueueItem, Int32 deliveryCount, ILogger log)
{
    log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
    log.LogInformation($"Delivery count : {deliveryCount}");
    if (deliveryCount < 3)
    {
        log.LogInformation("Simulating failure");
        throw new TooManyRequestsException();
    }
    else
    {
        log.LogInformation($"Delivery count is now {deliveryCount}. Time to write this message into another queue.");
    } 
}

On my 3rd retry, I would like to send my message to another queue.在第 3 次重试时,我想将消息发送到另一个队列。 After sending a test message to my service bus, I get the following output.向我的服务总线发送测试消息后,我得到以下 output。

2022-02-22T03:05:35.281 [Information] Executing 'Functions.ServiceBusQueueTrigger1' (Reason='(null)', Id=eed59015-f100-42b4-b0bf-57a9a6ab226f)
2022-02-22T03:05:35.281 [Information] Trigger Details: MessageId: 86f8250e82fb42deb88e6b91244ca2b7, SequenceNumber: 3, DeliveryCount: 1, EnqueuedTimeUtc: 2022-02-22T03:05:35.2980000Z, LockedUntilUtc: 2022-02-22T03:06:05.2980000Z, SessionId: (null)
2022-02-22T03:05:35.531 [Information] C# ServiceBus queue trigger function processed message: my new message
2022-02-22T03:05:35.531 [Information] Delivery count : 1
2022-02-22T03:05:35.532 [Information] Simulating failure
2022-02-22T03:05:35.557 [Error] Executed 'Functions.ServiceBusQueueTrigger1' (Failed, Id=eed59015-f100-42b4-b0bf-57a9a6ab226f, Duration=253ms)Exception of type 'Microsoft.Toolkit.Services.TooManyRequestsException' was thrown.
2022-02-22T03:05:35.644 [Information] Executing 'Functions.ServiceBusQueueTrigger1' (Reason='(null)', Id=78d2df93-11f6-412b-9ae7-880ac3bf4213)
2022-02-22T03:05:35.645 [Information] Trigger Details: MessageId: 86f8250e82fb42deb88e6b91244ca2b7, SequenceNumber: 3, DeliveryCount: 2, EnqueuedTimeUtc: 2022-02-22T03:05:35.2980000Z, LockedUntilUtc: 2022-02-22T03:06:05.6730000Z, SessionId: (null)
2022-02-22T03:05:35.645 [Information] C# ServiceBus queue trigger function processed message: my new message
2022-02-22T03:05:35.646 [Information] Delivery count : 2
2022-02-22T03:05:35.646 [Information] Simulating failure
2022-02-22T03:05:35.674 [Error] Executed 'Functions.ServiceBusQueueTrigger1' (Failed, Id=78d2df93-11f6-412b-9ae7-880ac3bf4213, Duration=2ms)Exception of type 'Microsoft.Toolkit.Services.TooManyRequestsException' was thrown.
2022-02-22T03:05:35.782 [Information] Executing 'Functions.ServiceBusQueueTrigger1' (Reason='(null)', Id=b856141d-d891-4edb-a910-7e53b40b96a0)
2022-02-22T03:05:35.783 [Information] Trigger Details: MessageId: 86f8250e82fb42deb88e6b91244ca2b7, SequenceNumber: 3, DeliveryCount: 3, EnqueuedTimeUtc: 2022-02-22T03:05:35.2980000Z, LockedUntilUtc: 2022-02-22T03:06:05.8130000Z, SessionId: (null)
2022-02-22T03:05:35.783 [Information] C# ServiceBus queue trigger function processed message: my new message
2022-02-22T03:05:35.784 [Information] Delivery count : 3
2022-02-22T03:05:35.784 [Information] Delivery count is now 3. Time to write this message into another queue.**
2022-02-22T03:05:35.784 [Information] Executed 'Functions.ServiceBusQueueTrigger1' (Succeeded, Id=b856141d-d891-4edb-a910-7e53b40b96a0, Duration=2ms)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM