【Azure 服务总线】Azure Service Bus中私信(DLQ - Dead Letter Queue)如何快速清理
在博文ServiceBus 队列中死信(DLQ - Dead Letter Queue)问题一文中,介绍了服务总线产生私信的原因及可以通过代码的方式来清楚私信队列中的消息,避免长期占用空间(因为私信中的消息不会自动清理)
当前,我们也可以从Azure门户中查看到当前DLQ的数量,所占空间及进入DLQ的原因
问题描述
在使用Azure Service Bus过程中,随着时间的累积,当死信中存积了大量的消息时,如何快速的清理掉这些消息呢?
解决办法
使用Azure官方提供的工具 Service Bus Explorer。 连接到当前的Service Bus,通过选择Receive and Delete操作来获取并从Service Bus服务端中删除消息。
1) 下载Service Bus Explorer,解压文件后,双击ServiceBusExplorer.exe
2) 连接到Service Bus中并查看死信消息
3) Receive and Delete: 数据获取消息的数量,然后再Receive Mode中选择Receive and Delete
附录:另一种方式是通过代码来处理死信消息
如需要通过程序的方式获取死信队列中的消息,获取消息的方式和正常队列一样,把queueName变为死信队列的路径,通过QueueClient.FormatDeadLetterPath(queueName)方式获取
附上.NET伪代码:
static string queueName = "<QUEUE NAME>/$deadletterqueue"; static async Task ReceiveMessagesAsync() { await using (ServiceBusClient client = new ServiceBusClient(connectionString)) { // create a processor that we can use to process the messages ServiceBusProcessor processor = client.CreateProcessor(queueName, new ServiceBusProcessorOptions()); // add handler to process messages processor.ProcessMessageAsync += MessageHandler; // add handler to process any errors processor.ProcessErrorAsync += ErrorHandler; // start processing await processor.StartProcessingAsync(); Console.WriteLine("Wait for a minute and then press any key to end the processing"); Console.ReadKey(); // stop processing Console.WriteLine("\nStopping the receiver..."); await processor.StopProcessingAsync(); Console.WriteLine("Stopped receiving messages"); } }
参考资料
从队列接收消息: https://docs.azure.cn/zh-cn/service-bus-messaging/service-bus-dotnet-get-started-with-queues#receive-messages-from-a-queue
Azure Service Bus 死信队列产生的原因: https://www.cnblogs.com/lulight/p/13652828.html
Azure Service Bus Explorer: https://github.com/paolosalvatori/ServiceBusExplorer/releases
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?