1、多消费者的时候,mq的调试很麻烦!因为dev,local环境的消费者也会消费rabbitmq的消息,一般的办法是多发几次
优先消费者
rabbitmq3.2以上可以对消费者进行排序。
springboot方案
对于springboot工程@RabbitListener,里面有一个参数priority,这个是配置消费者的优先级,默认为0,数值越大优先级越高。
@RabbitListener(queues="${xxQueueName}",containerFactory = "xxxContainerFactory",priority = "3")
public void onMessage(Message message,Channel channel) throws Exception {
try catch finally
}
原生api方案
如果不是使用springboot工程,用原生的api,可以在创建消费者的时候传入参数x-priority
Map<String, Object> map = new HashMap<>();
map.put("x-priority",5);
channel.basicConsume("queueName",true,map,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag,
Envelope envelope,
AMQP.BasicProperties properties,
byte[] body)
throws IOException {
}
});
注意点
1.消费者的优先级设定是通过建立消费的basicConsume的时候设定的arguements的x-priority
2.当高优先级的消费者未出现阻塞或者限制的时候,那么低优先级的消费者是不能拿到消息的
3.当高优先级的消费者出现阻塞等其他限制的情况的时候,消息才会发向给低优先级的消费者
4.对于是否设定消费者优先级需要按照需求进行设定,如果盲目添加可能导致其他消费者拿不到消息!