RabbitMQ 消费端 Client CPU 100%的解决办法
Func<bool> run = () =>
{
try
{
using (IConnection conn = cf.CreateConnection())
{
using (IModel channel = conn.CreateModel())
{
var consumer = createConsumer(channel);
while (channel.IsOpen)
{
Thread.Sleep(10);
BasicDeliverEventArgs ea = null;
try
{
ea = consumer.Queue.DequeueNoWait(null);
if (ea == null) continue;
}
catch (Exception ex)
{
TraceLogHelper.WriteError("RabbitMQ", "【里面】", ex.StackTrace);
break;
}
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
var b = func(message);
if (b)
{
channel.BasicAck(ea.DeliveryTag, false);
Console.WriteLine("接收消息成功:" + message);
}
}
}
}
return true;
}
catch (Exception ex)
{
TraceLogHelper.WriteError("RabbitMQ", "[外层]", ex.StackTrace);
return false;
}
};
增加一个
Thread.Sleep(10);
即可解决。
以后遇到无线循环的时候,需要加Sleep