青海省公路建设服务网站,wordpress 屏蔽插件更新,wordpress 加水印,哪些网站织梦cmsFanout交换机
Fanout#xff0c;英文翻译是扇出#xff0c;我觉得在MQ中叫广播更合适。 在广播模式下#xff0c;消息发送流程是这样的#xff1a;
1#xff09; 可以有多个队列2#xff09; 每个队列都要绑定到Exchange#xff08;交换机#xff09;3#xff09; …Fanout交换机
Fanout英文翻译是扇出我觉得在MQ中叫广播更合适。 在广播模式下消息发送流程是这样的
1 可以有多个队列2 每个队列都要绑定到Exchange交换机3 生产者发送的消息只能发送到交换机4 交换机把消息发送给绑定过的所有队列5 订阅队列的消费者都能拿到消息
我们的计划是这样的
创建一个名为 hmall.fanout的交换机类型是Fanout创建两个队列fanout.queue1和fanout.queue2绑定到交换机hmall.fanout
3.5.1.声明队列和交换机案例练习
利用SpringAMQP演示FanoutExchange的使用 在控制台创建队列fanout.queue1: 在创建一个队列fanout.queue2 然后再创建一个交换机 然后绑定两个队列到交换机
3.5.2.消息发送
在publisher服务的SpringAmqpTest类中添加测试方法 /*** 向新声明的交换机发送消息(hmall.fanout)*/Testpublic void testFanoutExchange() {// 交换机名称String exchangeName hmall.fanout;// 消息String message hello, everyone!;rabbitTemplate.convertAndSend(exchangeName, null, message);}
3.5.3.消息接收
在consumer服务的SpringRabbitListener中添加两个方法作为消费者 /*** 接受hmall.fanout的消息* param msg*/RabbitListener(queues fanout.queue1)public void listenFanoutQueue1(String msg) {System.out.println(消费者1所在的fanout.queue1接收到Fanout消息【 msg 】);}/*** 接受hmall.fanout的消息* param msg*/RabbitListener(queues fanout.queue2)public void listenFanoutQueue2(String msg) {System.out.println(消费者2所在的fanout.queue2接收到Fanout消息【 msg 】);}
3.5.4.总结
交换机的作用是什么
接收publisher发送的消息将消息按照规则路由到与之绑定的队列不能缓存消息路由失败消息丢失FanoutExchange的会将消息路由到每个绑定的队列