spring boot整合RabbitMQ實(shí)例詳解(Fanout模式)
1.Fanout Exchange介紹
Fanout Exchange 消息廣播的模式,不管路由鍵或者是路由模式,會(huì)把消息發(fā)給綁定給它的全部隊(duì)列,如果配置了routing_key會(huì)被忽略。
如上圖所示,即當(dāng)使用fanout交換器時(shí),他會(huì)將消息廣播到與該交換器綁定的所有隊(duì)列上,這有利于你對(duì)單條消息做不同的反應(yīng)。
例如存在以下場(chǎng)景:一個(gè)web服務(wù)要在用戶完善信息時(shí),獲得積分獎(jiǎng)勵(lì),這樣你就可以創(chuàng)建兩個(gè)對(duì)列,一個(gè)用來(lái)處理用戶信息的請(qǐng)求,另一個(gè)對(duì)列獲取這條消息是來(lái)完成積分獎(jiǎng)勵(lì)的任務(wù)。
2.代碼示例
1).Queue配置類
FanoutRabbitConfig.java類:
package com.example.rabbitmqfanout; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.FanoutExchange; import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class FanoutRabbitConfig { //創(chuàng)建隊(duì)列 @Bean public Queue AMessage() { return new Queue("fanout.A"); } //創(chuàng)建隊(duì)列 @Bean public Queue BMessage() { return new Queue("fanout.B"); } //創(chuàng)建隊(duì)列 @Bean public Queue CMessage() { return new Queue("fanout.C"); } //創(chuàng)建Fanout交換器 @Bean FanoutExchange fanoutExchange() { return new FanoutExchange("fanoutExchange"); } //將對(duì)列綁定到Fanout交換器 @Bean Binding bindingExchangeA(Queue AMessage,FanoutExchange fanoutExchange) { return BindingBuilder.bind(AMessage).to(fanoutExchange); } //將對(duì)列綁定到Fanout交換器 @Bean Binding bindingExchangeB(Queue BMessage, FanoutExchange fanoutExchange) { return BindingBuilder.bind(BMessage).to(fanoutExchange); } //將對(duì)列綁定到Fanout交換器 @Bean Binding bindingExchangeC(Queue CMessage, FanoutExchange fanoutExchange) { return BindingBuilder.bind(CMessage).to(fanoutExchange); } }
2).消息生產(chǎn)者
FanoutSender.java類:
package com.example.rabbitmqfanout.rabbitmq; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class FanoutSender { @Autowired private AmqpTemplate rabbitTemplate; public void send() { String context = "hi, fanout msg "; System.out.println("Sender : " + context); this.rabbitTemplate.convertAndSend("fanoutExchange","", context); } }
3).消息消費(fèi)者
FanoutReceiverA.java類:
package com.example.rabbitmqfanout.rabbitmq; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component @RabbitListener(queues = "fanout.A") public class FanoutReceiverA { @RabbitHandler public void process(String message) { System.out.println("fanout Receiver A : " + message); } }
FanoutReceiverB.java類:
package com.example.rabbitmqfanout.rabbitmq; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component @RabbitListener(queues = "fanout.B") public class FanoutReceiverB { @RabbitHandler public void process(String message) { System.out.println("fanout Receiver B: " + message); } }
FanoutReceiverC.java類:
package com.example.rabbitmqfanout.rabbitmq; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component @RabbitListener(queues = "fanout.C") public class FanoutReceiverC { @RabbitHandler public void process(String message) { System.out.println("fanout Receiver C: " + message); } }
4).測(cè)試
FanoutTest.java類:
package com.example.rabbitmqfanout.rabbitmq; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class FanoutTest { @Autowired private FanoutSender sender; @Test public void fanoutSender() throws Exception { sender.send(); } }
以上所述是小編給大家介紹的spring boot整合RabbitMQ(Fanout模式),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
詳解 Java HashMap 實(shí)現(xiàn)原理
這篇文章主要介紹了詳解 Java HashMap 實(shí)現(xiàn)原理的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下2021-03-03Java將Date日期類型字段轉(zhuǎn)換成json字符串的方法
這篇文章主要給大家介紹了關(guān)于Java將Date日期類型字段轉(zhuǎn)換成json字符串的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02JAVA實(shí)現(xiàn)將磁盤中所有空文件夾進(jìn)行刪除的代碼
這篇文章主要介紹了JAVA實(shí)現(xiàn)將磁盤中所有空文件夾進(jìn)行刪除的代碼,需要的朋友可以參考下2017-06-06vue+ java 實(shí)現(xiàn)多級(jí)菜單遞歸效果
這篇文章主要介紹了vue+ java 實(shí)現(xiàn)多級(jí)菜單遞歸效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12詳解SpringBoot健康檢查的實(shí)現(xiàn)原理
這篇文章主要介紹了詳解SpringBoot健康檢查的實(shí)現(xiàn)原理,幫助大家更好的理解和學(xué)習(xí)使用SpringBoot框架,感興趣的朋友可以了解下2021-03-03Java中的SynchronousQueue阻塞隊(duì)列及使用場(chǎng)景解析
這篇文章主要介紹了Java中的SynchronousQueue阻塞隊(duì)列及使用場(chǎng)景解析,SynchronousQueue 是 Java 中的一個(gè)特殊的阻塞隊(duì)列,它的主要特點(diǎn)是它的容量為0,這意味著 SynchronousQueue不會(huì)存儲(chǔ)任何元素,需要的朋友可以參考下2023-12-12使用Spring?Security搭建極簡(jiǎn)的安全網(wǎng)站教程
這篇文章主要為大家介紹了使用Spring?Security搭建極簡(jiǎn)的安全網(wǎng)站教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06