原因一:交换机名称写错了
解决:查看config中定义的交换机名字和实际使用的交换机名称是否相同
config
类:
//Fanout交换机 起名:fanout_order2_exchange
@Bean
public FanoutExchange fanoutOrderExchange(){
return new FanoutExchange("fanout_order2_exchange",false,false);
}
保证实际使用的交换机名称一致:
//定义交换机名称
private final String exchangeName = "fanout_order2_exchange";
原因二:rabbitTemplate.convertAndSend()
方法参数问题
本人刚开始学习rabbitmq
,这次使用的是fanout
交换机,fanout
交换机不是不需要routekey
吗?
使用fanout
交换机,生产者发送消息有两种写法:
写法一:
rabbitTemplate.convertAndSend(exchangeName,"",message);
写法二:
rabbitTemplate.convertAndSend(exchangeName,null,message);
这两种写法都在routekey
的参数位置传递了一个空值,运行起来没有问题。
我的写法:
rabbitTemplate.convertAndSend(exchangeName,message);
然后rabbitmq
生产者的消息就没法进入消息队列了。。。
解决:改回上面的写法一或者写法二消息就能成功进入队列。
但是我不知道为啥不能用我的写法。。。