RabbitMq批量刪除隊列
? 由于部分公司同事使用RabbitMq時,沒有將Client設(shè)置為autodelete,導(dǎo)致大量冗余隊列。其中這些隊列又是無routekey隊列,收到了批量的訂閱消息,占用服務(wù)器內(nèi)存。文章來源:http://www.zghlxwxcb.cn/news/detail-823771.html
? 如何將這些無用的隊列刪除成為一個問題?經(jīng)過多次摸索,在rabbitmq management api里面找到了方案:文章來源地址http://www.zghlxwxcb.cn/news/detail-823771.html
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
}
到了這里,關(guān)于【mq】RabbitMq批量刪除隊列的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!