javascript
Springboot整合RabbitMq-用心看完这一篇就够了(最新)
SpringBoot-整合MQ---目錄
- 第一步創建一個springboot的項目
- 1---HelloWorld模型
- 2---Work模型
- 3---Fanout模式
- 4---direct路由模式
- 5---主題模式topics
第一步創建一個springboot的項目
創建springboot項目 選好所需要的骨架
1—HelloWorld模型
springboot-整合mq-第一種模型 Hello World 簡單模式
一個生產者丶默認交換機丶一個隊列丶一個消費者。
第一步配置好自己的yml
創建生產者
第一個:HelloWorld模型
1、一個生產者
2、默認交換機
3、一個隊列
4、一個消費者
創建消費者
@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
創建生產者
第二個:Work模型
1、一個生產者
2、默認交換機
3、一個隊列
4、多個消費者
創建消費者
@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
創建生產者
第三個:Fanout模式
1、一個生產者
2、一個交換機
3、多個隊列
4、多個消費者
創建消費者
@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
創建生產者
第四個: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
創建生產者
@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-用心看完这一篇就够了(最新)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springboot整合rabbitmq
- 下一篇: RabbitMQ的TTL+死信队列 看完