【mq】RabbitMq批量删除队列

1|0RabbitMq批量删除队列


​ 由于部分公司同事使用RabbitMq时,没有将Client设置为autodelete,导致大量冗余队列。其中这些队列又是无routekey队列,收到了批量的订阅消息,占用服务器内存。

​ 如何将这些无用的队列删除成为一个问题?经过多次摸索,在rabbitmq management api里面找到了方案:

using System.Net.Http.Headers; using System.Text; using Newtonsoft.Json; class Program { static async Task Main() { string rabbitMQBaseUrl = "https://your_url"; // Replace with your RabbitMQ management interface URL string vhost = ""; // Replace with the vhost you want to remove queues from string username = ""; // Replace with your RabbitMQ username string password = ""; // Replace with your RabbitMQ password // Get a list of queues in the vhost var queues = await GetQueuesAsync(rabbitMQBaseUrl, vhost, username, password); // Delete each queue foreach (var queue in queues) { await DeleteQueueAsync(rabbitMQBaseUrl, vhost, username, password, queue); } Console.WriteLine("All queues deleted successfully."); } static async Task<string[]> GetQueuesAsync(string baseUrl, string vhost, string username, string password) { using (var httpClient = new HttpClient()) { var byteArray = Encoding.ASCII.GetBytes($"{username}:{password}"); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); var response = await httpClient.GetStringAsync($"{baseUrl}/api/queues/{Uri.EscapeDataString(vhost)}"); // Parse the JSON response using Newtonsoft.Json var queueList = JsonConvert.DeserializeObject<QueueInfo[]>(response); // Extract queue names from the parsed response var queueNames = queueList.Select(queueInfo => queueInfo.Name).ToArray(); return queueNames; } } static async Task DeleteQueueAsync(string baseUrl, string vhost, string username, string password, string queue) { using (var httpClient = new HttpClient()) { var byteArray = Encoding.ASCII.GetBytes($"{username}:{password}"); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); await httpClient.DeleteAsync( $"{baseUrl}/api/queues/{Uri.EscapeDataString(vhost)}/{Uri.EscapeDataString(queue)}"); } } } // Define a class to represent the structure of the QueueInfo received from the API public class QueueInfo { public string Name { get; set; } // Add other properties if needed }

__EOF__

本文作者野菊花
本文链接https://www.cnblogs.com/Jackyye/p/17981719.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   野菊花  阅读(854)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示