rabbitmq一共有三種交換機:
- fanout--廣播
- direct--定向
- topic--話題
rabbitmq-web端
首先我們需要建立一個名叫cybg.fanout交換機與兩個自定義的隊列用于測試廣播效果
我這里就起名字叫做fanout_queue1&fanout_queue2
項目中:
首先對我們的Listener加入對上面兩個隊列的監聽
@RabbitListener(queues = "fanout_queue1") public void fanoutQ1(String msg) throws InterruptedException {System.out.println("fanout1接收到的msg:"+msg);Thread.sleep(20); }@RabbitListener(queues = "fanout_queue2") public void fanoutQ2(String msg) throws InterruptedException {System.out.println("fanout2接收到的msg:"+msg);Thread.sleep(20); }
然后我們可以在springtest中添加對于發消息的demo
@Test void testSendMsgToFanout(){String exchangeName = "cybg.fanout";String msg = "fanout everyone";rabbitTemplate.convertAndSend(exchangeName,null,msg); }
這里和直接發送到隊列不同的是,此處的參數變為了交換機名字、routeKey、message
運行listener服務可以發現我們的隊列已經可以拿到我們對交換機發送的消息
fanout1接收到的msg:fanout everyone fanout2接收到的msg:fanout everyone