最近生產上一組RabbitMQ連線數異常升高,需要排查原因,但這組MQ上的業務有很多,以叢集方式部署,客戶端透過VIP連線,中間還有一層F5,所以從管理介面上難以定位到具體是誰導致的,雖然後面透過掃描5672埠定位了罪魁禍首,但如果後續碰到類似情況,如何快速定位具體是哪塊業務或者是哪臺機器影響的呢?我們先看RabbitMQ的管理介面,在Connections頁面中,雖然在Name中提供了【IP+Port】,但並非我們實際的客戶端IP:
所以能否自定義Name呢,答案是肯定的,我們可以透過connectionFactory.CreateConnection(connName);
設定clientProvidedName
來設定連線名稱,直接上程式碼:
Console.WriteLine("開始執行!");
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.AutomaticRecoveryEnabled = true;
connectionFactory.NetworkRecoveryInterval = TimeSpan.FromSeconds(2.0);
connectionFactory.UserName = "admin";
connectionFactory.Password = "123456";
connectionFactory.HostName = "10.10.10.10";
connectionFactory.Port = 5672;
connectionFactory.VirtualHost = "/";
string connName = "連線示例";
var conn = connectionFactory.CreateConnection(connName);
var channel = conn.CreateModel();
EventingBasicConsumer eventingBasicConsumer = new EventingBasicConsumer(channel);
eventingBasicConsumer.Received += (sender, args) =>
{
string msg = Encoding.UTF8.GetString(args.Body.ToArray());
Console.WriteLine(msg);
};
string consumerTag = "消費者示例";
channel.BasicConsume("Q_Test", true, consumerTag, eventingBasicConsumer);
Console.WriteLine("連線中...");
Console.ReadLine();
效果如下:
細心的同學可能會發現我們還設定了consumerTag
,是的,消費者一樣可以在BasicConsume
時透過設定consumerTag
來設定消費者名稱,效果如下: