springBoot集成rabbitMQ启动报错,错误信息如下所示
2021-06-04 09:49:52.726 [AMQP Connection 10.27.0.25:5672] ERROR o.s.a.rabbit.connection.CachingConnectionFactory - Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=406, reply-text=PRECONDITION_FAILED - inequivalent arg 'x-queue-type' for queue '****' in vhost '****': received none but current is the value 'classic' of type 'longstr', class-id=50, method-id=10)
上面的错误信息已经很明显了,说明的是队列创建的时候少了 x-queue-type 这个参数。
Map<String, Object> args = new HashMap<>();
args.put("x-queue-type", "classic");
return new Queue(MY_QUEUE_NAME, NON_DURABLE, false, false, args);
方法二:
rabbitMQ消费端如果使用的是RabbitListener注解,在配置的过程中添加上@Argument注解就可以了,具体的实例代码如下
import com.rabbitmq.client.AMQP;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.stereotype.Component;
/**
* @Title aams行为日志消息消费者
* @Author YangWanJie
* @Date 2021/5/27 16:25
* @versioin V1.0
* @RabbitListener bindings:绑定队列
* @QueueBinding value:绑定队列的名称
* exchange:配置交换器
* key:路由键
* @Queue value:配置队列名称
* autoDelete:是否是一个可删除的临时队列
* @Exchange value:为交换器起个名称
* type:指定具体的交换器类型
**/
@Component
@RabbitListener(
bindings = @QueueBinding(
value = @Queue(value = "${mq.config.queue.actionLog}" ,arguments = {
@Argument(name = "x-queue-type", value = "classic")
}),
exchange = @Exchange(value = "${mq.config.exchange}", type = ExchangeTypes.FANOUT)
)
)
public class ActionLogConsumer {
/**
* @Title 接收消息的方法。采用消息队列监听机制
* @Author YangWanJie
* @Date 2021/5/27 16:26
* @versioin V1.0
**/
@RabbitHandler
public void process(String msg) {
//具体的业务处理逻辑
}
}