日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Springboot整合RabbitMq-用心看完这一篇就够了(最新)

發布時間:2024/9/19 javascript 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Springboot整合RabbitMq-用心看完这一篇就够了(最新) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

SpringBoot-整合MQ---目錄

    • 第一步創建一個springboot的項目
            • 1---HelloWorld模型
            • 2---Work模型
            • 3---Fanout模式
            • 4---direct路由模式
            • 5---主題模式topics

第一步創建一個springboot的項目

創建springboot項目 選好所需要的骨架

1—HelloWorld模型

springboot-整合mq-第一種模型 Hello World 簡單模式
一個生產者丶默認交換機丶一個隊列丶一個消費者。

第一步配置好自己的yml

server:port: 8001spring:rabbitmq:username: guest password: guest virtual-host: / port: 5672host: 192.168.192.168

創建生產者

第一個:HelloWorld模型
1、一個生產者
2、默認交換機
3、一個隊列
4、一個消費者

@SpringBootTest // 1--生產者 class SpringBootBuliangrenApplicationTests {@Autowired//RabbitTemplate是模板對象,用來簡化mq操作private RabbitTemplate rabbitTemplate; // 2--不用寫就是默認交換機 // 3--隊列名稱是:hello // 發送的消息是HelloWorld@Testvoid contextLoads() { // 3--隊列名字是:hellorabbitTemplate.convertAndSend("hello", "HelloWorld");} }

創建消費者

@Component//@RabbitListener 這個注解代表消費者的 一個監聽//消費者監聽hello隊列 @RabbitListener(queuesToDeclare = @Queue(value = "hello")) public class HelloCustomer {//消費者 - 消費 - 提供者 的消息@RabbitHandlerpublic void receival(String message){System.out.println("消費接收到了消息----->"+message);} }

運行

此時hello隊列也創建出了

2—Work模型

springboot-整合mq-第二種模型:work-工作模式
一個生產者丶默認交換機丶一個隊列丶多個消費者。

第一步配置好自己的yml

server:port: 8001spring:rabbitmq:username: guest password: guest virtual-host: / port: 5672host: 192.168.192.168

創建生產者

第二個:Work模型
1、一個生產者
2、默認交換機
3、一個隊列
4、多個消費者

@SpringBootTest //1 一個生產者 class SpringBootBuliangren2ApplicationTests { // 2--不用寫就是默認交換機@Autowired //RabbitTemplate是模板對象,用來簡化mq操作private RabbitTemplate rabbitTemplatel; // 3--隊列名稱是:workTest //發送的消息是hello,work模型 我們發送多條消息 應為有多個消費者@Testvoid contextLoads() {//消費者發送10 條消息for (int i = 0;i<10;i++){rabbitTemplatel.convertAndSend("workTest","hello,work模型");}} }

創建消費者

@Component public class HelloCustomer {//1號消費者 ---監聽workTest隊列@RabbitListener(queuesToDeclare = @Queue("workTest"))public void receive01(String message){//消費者 - 消費 - 提供者 的消息System.out.println("消費者1號 - 接收到了消息----->"+message);}//2號消費者 ---監聽workTest隊列@RabbitListener(queuesToDeclare = @Queue("workTest"))public void receive02(String message){//消費者 - 消費 - 提供者 的消息System.out.println("消費者2號 - 接收到了消息----->"+message);}//3號消費者 ---監聽workTest隊列@RabbitListener(queuesToDeclare = @Queue("workTest"))public void receive03(String message){//消費者 - 消費 - 提供者 的消息System.out.println("消費者3號 - 接收到了消息----->"+message);} }

運行

此時workTest隊列也創建出了

3—Fanout模式

springboot-整合mq-第三種模型:Fanout-廣播模型-訂閱模式
多個消費者,每一個消費這都有自己的隊列,每個隊列都綁定到交換機
生產者發送消息到交換機-交換機發送到哪個隊列


第一步配置好自己的yml

server:port: 8003spring:rabbitmq:virtual-host: /host: 192.168.192.168port: 5672username: guestpassword: guest

創建生產者

第三個:Fanout模式
1、一個生產者
2、一個交換機
3、多個隊列
4、多個消費者

@SpringBootTest//1 一個生產者 class SpringBootBuliangren3ApplicationTests {@Autowired//RabbitTemplate是模板對象,用來簡化mq操作private RabbitTemplate rabbitTemplate;@Testvoid contextLoads() {//參數: 交換機名稱, 路由, 生產者發送的消息//我們使用for循環 ---發送多條消息 ---應為有多個消費者for (int i = 0; i < 10; i++) {rabbitTemplate.convertAndSend("fanoutlogs", "", "Fanout模型發送的消息");}} }

創建消費者

@Component//創建消費者 public class FanoutCustomer {@RabbitListener(bindings = {@QueueBinding(value = @Queue("FanoutTest"),//創建的隊列名稱exchange = @Exchange(value = "fanoutlogs",//綁定的交換機type = "fanout"))})//fanout類型public void receice01(String message) {System.out.println("消費者1號 - 接收到了消息----->" + message);}@RabbitListener(bindings = {@QueueBinding(value = @Queue("FanoutTest"),//創建的隊列名稱exchange = @Exchange(value = "fanoutlogs",//綁定的交換機type = "fanout"))})//fanout類型public void receice02(String message) {System.out.println("消費者2號 - 接收到了消息----->" + message);}@RabbitListener(bindings = {@QueueBinding(value = @Queue("FanoutTest"),//創建的隊列名稱exchange = @Exchange(value = "fanoutlogs",//綁定的交換機type = "fanout"))})//fanout類型public void receice03(String message) {System.out.println("消費者3號 - 接收到了消息----->" + message);} }

運行

此時FanoutTest隊列也創建出了

此時fanoutlogs交換機也創建出了

4—direct路由模式

springboot-整合mq-第四種模型:-路由模型-direct
根據指定路由的key轉向不同的隊列

第一步配置好自己的yml

server:port: 8004 spring:rabbitmq:port: 5672host: 192.168.192.168virtual-host: /username: guestpassword: guest

創建生產者

第四個:direct路由模式
1、一個生產者
2、一個交換機
3、多個隊列
4、多個消費者

根據不同的key 接收不同的消息

創建生產者

@SpringBootTest class SpringBootBuliangren4ApplicationTests {@Autowired //RabbitTemplate是模板對象,用來簡化mq操作private RabbitTemplate rabbitTemplate;//路由模式@Testvoid contextLoads() { 參數一:是交換機參數二:隊列參數三:消息rabbitTemplate.convertAndSend("directlogs", "info", "發送info的key的路由信息");rabbitTemplate.convertAndSend("directlogs", "warn", "發送warn的key的路由信息");} }

創建消費者

@Component public class RouteCusstomer {@RabbitListener(bindings = {@QueueBinding(value = @Queue("directTest01"),//創建隊列名字exchange = @Exchange(value = "directlogs"),//綁定的交換機key = {"info" //指定的key})})public void recrvel01(String message) {System.out.println("消費者1號: 接收的消息是---->" + message);}@RabbitListener(bindings = {@QueueBinding(value = @Queue("directTest02"),//創建隊列的名字exchange = @Exchange(value = "directlogs"),//綁定的交換機key = {"error" //指定的key})})public void recrvel02(String message) {System.out.println("消費者2號: 接收的消息是---->" + message);}@RabbitListener(bindings = {@QueueBinding(value = @Queue("directTest03"),//創建隊列的名字exchange = @Exchange(value = "directlogs"),//綁定的交換機key = {"warn" //指定的key})})public void recrvel03(String message) {System.out.println("消費者3號: 接收的消息是---->" + message);} }

運行

此時directTest隊列也創建出了

此時交換機也創建出了

5—主題模式topics

springboot-整合mq-第五種模型:主題模型-Topics
第五個:動態路由模式Topics
#號 可以代替零個或多個單詞.
*號 可以代替一個完整的單詞

第一步配置好自己的yml

server:port: 8005spring:rabbitmq:host: 192.168.192.168port: 5672username: guestpassword: guestvirtual-host: /

創建生產者

@SpringBootTest class SpringBootBuliangren5ApplicationTests {@Autowired //rabbitmqtemplate 是簡化mq開發private RabbitTemplate rabbitTemplate;@Testvoid contextLoads() { //參數1:交換機 參數2:key 參數3:生產者的消息rabbitTemplate.convertAndSend("topicsTest","a.user.save","user.save-----消息");} }

創建消費者

@Component//消費者 public class TopicCusstomer {@RabbitListener(bindings = {@QueueBinding(value = @Queue("topiceTest01"),//創建的隊列exchange = @Exchange(type = "topic",name = "topicsTest"),//topic類型綁定的交換機topicsTestkey = {"#.user.#"})})public void recevicel01(String messge){System.out.println("消費者1號: 接收的消息是---->"+messge);}@RabbitListener(bindings = {@QueueBinding(value = @Queue("topiceTest02"),//創建的隊列exchange = @Exchange(type = "topic",name = "topicsTest"),//topic類型綁定的交換機topicsTestkey = {"*.user","user.*"})})public void recevicel02(String messge){System.out.println("消費者2號: 接收的消息是---->"+messge);} }

運行

學習是一步一步來的不要跳過

總結

以上是生活随笔為你收集整理的Springboot整合RabbitMq-用心看完这一篇就够了(最新)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。