詳解rabbitmq使用springboot實現(xiàn)fanout模式
一、fanout模式
- 類型:fanout
- 特點:Fanout—發(fā)布與訂閱模式,是一種廣播機制,它是沒有路由key的模式。
二、實現(xiàn)
1、引入相應(yīng)的pom文件 pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.xpf</groupId> <artifactId>rabbitmq-springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>rabbitmq-springboot</name> <description>Demo project for Spring Boot</description> <properties> <java.version>8</java.version> </properties> <dependencies> <!--rabbitmq依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
2、配置文件 application.properties
server.port=8080 spring.rabbitmq.username=admin spring.rabbitmq.password=admin spring.rabbitmq.virtual-host=/ spring.rabbitmq.host=192.168.199.20 spring.rabbitmq.port=5672
3、使用springboot寫一個配置文件 RabbitMqConfiguration.java
(當(dāng)然此配置類就是用來聲明交換機類型以及交換機和隊列的綁定關(guān)系的,將此配置類放在生產(chǎn)者中,或者放在消費者中都可以,或者兩邊都放。不管是生產(chǎn)者還是消費者,當(dāng)生產(chǎn)者發(fā)送消息(或者消費者監(jiān)聽隊列時),如果發(fā)現(xiàn)rabbitmq中沒有所發(fā)送的交換機(或者監(jiān)聽的隊列),都會根據(jù)此配置類自動創(chuàng)建交換機和隊列以及綁定關(guān)系。但是一般建議下,放在消費者會更好一些,因為一般消費者在springboot系統(tǒng)啟動時,就會開啟監(jiān)聽,如果沒有相應(yīng)的rabbitmq連接有問題能在項目啟動時就發(fā)現(xiàn))
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 RabbitMqConfiguration { //1、聲明注冊fanout模式交換機 (生產(chǎn)者發(fā)送消息給rabbitmq,其實就是發(fā)送到交換機) @Bean public FanoutExchange fanoutExchange(){ return new FanoutExchange("fanout_order_exchange", true, false); } //2、聲明隊列 sms.fanout.queue、email.fanout.queue、duanxin.fanout.queue @Bean public Queue smsQueue(){ return new Queue("sms.fanout.queue", true); } @Bean public Queue emailQueue(){ return new Queue("email.fanout.queue", true); } @Bean public Queue duanxinQueue(){ return new Queue("duanxin.fanout.queue", true); } //3、完成綁定關(guān)系(隊列綁定交換機) @Bean public Binding smsBinding(){ return BindingBuilder.bind(smsQueue()).to(fanoutExchange()); } @Bean public Binding emailBinding(){ return BindingBuilder.bind(emailQueue()).to(fanoutExchange()); } @Bean public Binding duanxinBinding(){ return BindingBuilder.bind(duanxinQueue()).to(fanoutExchange()); } }
4、寫一個生產(chǎn)者 FanoutOrderService.java
import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.UUID; @Service public class FanoutOrderService { @Autowired private RabbitTemplate rabbitTemplate; /** * 模擬用戶下單,發(fā)送消息給下游系統(tǒng) * @param user * @param num */ public void makerOrder(String user, int num){ //1、查詢庫存是否有剩余 //2、保存訂單 String orderId = UUID.randomUUID().toString(); System.out.println("訂單生產(chǎn)成功:" + orderId); //3、通過mq給下游系統(tǒng)發(fā)送消息(交換機的名字千萬別寫錯,和上述配置類應(yīng)相同) String exchangeName = "fanout_order_exchange"; String routingkey = ""; rabbitTemplate.convertAndSend(exchangeName, routingkey, orderId); System.out.println("完成"); } }
5、到這一步就可以再寫一個測試類來測試生產(chǎn)者發(fā)送消息
import com.xpf.rabbitmqspringboot.service.DirectOrderService; import com.xpf.rabbitmqspringboot.service.FanoutOrderService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class RabbitmqSpringbootApplicationTests { @Autowired private FanoutOrderService fanoutOrderService; @Autowired private DirectOrderService directOrderService; /** * Fanout模式生成者發(fā)送消息 */ @Test void setFanoutOrderService() { fanoutOrderService.makerOrder("用戶1", 10); } }
發(fā)送完消息可以檢查rabbitmq是否生成新交換機fanout_order_exchange和三個隊列sms.fanout.queue、email.fanout.queue、duanxin.fanout.queue,以及是否有在三個隊列看到一條剛剛發(fā)送的消息(因為是Fanout—發(fā)布與訂閱模式,是一種沒有路由key的廣播機制,所有隊列都能收到消息)
6、寫一個消費者。(可以另啟一個springboot項目,模擬另外的系統(tǒng)來消費我們發(fā)送的消息,也可像我一樣在原項目編寫,自己消費)
import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; /** * @Author xpf * @Date 2023/7/9 0:44 * @Version 1.0 * Fanout—發(fā)布與訂閱模式消費者(廣播機制) */ @Component @RabbitListener(queues = "sms.fanout.queue") public class smsFanoutConsumer { @RabbitHandler public void receiveMessage(String message){ System.out.println("接收到來自隊列sms.fanout.queue消息訂單的message是:" + message); } }
這里寫了一個監(jiān)聽sms發(fā)送消息的消費者,剩下兩個email和duanxin類似。
特別注意:如果生成者發(fā)送的消息是什么類型,消費者接收時,
public void receiveMessage(String message)方法中的參數(shù)應(yīng)該也是相同的類型,否則會報錯。比如這里的string類型,因為生產(chǎn)者發(fā)送的也是sting類型
直接啟動會發(fā)現(xiàn)sms.fanout.queue隊列中的消息被消費掉了
到此這篇關(guān)于rabbitmq使用springboot實現(xiàn)fanout模式的文章就介紹到這了,更多相關(guān)springboot實現(xiàn)fanout模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot常用計量與bean屬性校驗和進制數(shù)據(jù)轉(zhuǎn)換規(guī)則全面分析
這篇文章主要介紹了SpringBoot常用計量、bean屬性校驗與進制數(shù)據(jù)轉(zhuǎn)換規(guī)則,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-10-10Springboot內(nèi)外部logback多環(huán)境配置詳解
本文主要介紹了Springboot內(nèi)外部logback多環(huán)境配置詳解,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01Java集合中的WeakHashMap、IdentityHashMap、EnumMap詳解
這篇文章主要介紹了Java集合中的WeakHashMap、IdentityHashMap、EnumMap詳解,HashMap的key保留了對實際對象的強引用,這意味著只要HashMap對象不被銷毀,還HashMap的所有key所引用的對象就不會被垃圾回收,需要的朋友可以參考下2023-09-09Java 數(shù)據(jù)結(jié)構(gòu)中二叉樹前中后序遍歷非遞歸的具體實現(xiàn)詳解
樹是一種重要的非線性數(shù)據(jù)結(jié)構(gòu),直觀地看,它是數(shù)據(jù)元素(在樹中稱為結(jié)點)按分支關(guān)系組織起來的結(jié)構(gòu),很象自然界中的樹那樣。樹結(jié)構(gòu)在客觀世界中廣泛存在,如人類社會的族譜和各種社會組織機構(gòu)都可用樹形象表示2021-11-11springboot+vue實現(xiàn)oss文件存儲的示例代碼
對象存儲服務(wù)是一種海量、安全、低成本、高可靠的云存儲服務(wù),本文主要介紹了springboot+vue實現(xiàn)oss文件存儲的示例代碼,具有一定的參考價值,感興趣的可以了解一下2024-02-02