SpringBoot整合RabbitMQ實(shí)現(xiàn)交換機(jī)與隊(duì)列的綁定
簡介
本文用實(shí)例介紹SpringBoot中RabbitMQ如何綁定交換機(jī)(交換器)與隊(duì)列。
配置方法概述
交換機(jī)
下邊兩種方式等價。
ExchangeBuilder.topicExchange(EXCHANGE_TOPIC_WELCOME).durable(true).build();
new TopicExchange(EXCHANGE_TOPIC_WELCOME, true, false)
隊(duì)列
下邊兩種方式等價
QueueBuilder.durable("Hi").build();
new Queue(QUEUE_HI, true)
綁定
下邊兩種方式等價
注意:第一種的參數(shù)并不是字符串。
BindingBuilder.bind(helloQueue).to(welcomExchange).with("hello.#") new Binding("Queue@hello", Binding.DestinationType.QUEUE, "Exchange@topic.welcome", "hello.#", null)
法1:配置類(簡潔方法)(推薦)
package com.example.config; import org.springframework.amqp.core.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitMQRouterConfig { public static final String QUEUE_HELLO = "Queue@hello"; public static final String QUEUE_HI = "Queue@hi"; public static final String EXCHANGE_TOPIC_WELCOME = "Exchange@topic.welcome"; public static final String ROUTINGKEY_HELLOS = "hello.#"; @Autowired private AmqpAdmin amqpAdmin; @Bean public Object initBindingTest() { amqpAdmin.declareExchange(new TopicExchange(EXCHANGE_TOPIC_WELCOME, true, false)); amqpAdmin.declareQueue(new Queue(QUEUE_HI, true)); amqpAdmin.declareQueue(new Queue(QUEUE_HELLO, true)); amqpAdmin.declareBinding(new Binding(QUEUE_HELLO, Binding.DestinationType.QUEUE, EXCHANGE_TOPIC_WELCOME, ROUTINGKEY_HELLOS, null)); return new Object(); } }
amqpAdmin.declareBinding
需要一個Binding對象作為參數(shù)
- exchange:交換器名稱
- type:交換器類型。BuiltinExchangeType枚舉類,有以下4中類型交換器:DIRECT(“direct”), FANOUT(“fanout”), TOPIC(“topic”), HEADERS(“headers”)
- durable:設(shè)置是否持久化。true:持久化,false:非持久化。持久化可以將交換器存盤,在服務(wù)器重啟時不會丟失相關(guān)消息。
- autoDelete:設(shè)置是否自動刪除。true:自動刪除,false:不自動刪除。自動刪除的前提是至少有一個隊(duì)列或交換器與這個交換器綁定,之后所有與這個交換器綁定的隊(duì)列或交換器都與此交換器解綁。
- internal:設(shè)置是否內(nèi)置的。true:內(nèi)置交換器,false:非內(nèi)置交換器。內(nèi)置交換器,客戶端無法直接發(fā)送消息到這個交換器中,只能通過交換器路由到交換器這種方式。
- arguments:其他一些結(jié)構(gòu)化參數(shù)。如備份交換器:alternate-exchange、超時時間。示例配置超時時間方法:
Map<String, Object> params = new HashMap(); params.put("x-message-ttl", 2000); amqpAdmin.declareBinding(new Binding(QUEUE_HELLO, Binding.DestinationType.QUEUE, EXCHANGE_TOPIC_WELCOME, ROUTINGKEY_HELLOS, params));
法2:配置類(繁瑣方法)(不推薦)
不推薦的原因
適用于隊(duì)列和交換器不多時。
代碼示例
package com.lly.order.message; import org.springframework.amqp.core.*; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitMQConfig { public final static String QUEUE_DIRECT = "Queue@direct"; public final static String QUEUE_TOPIC_ONE = "Queue@topic_one"; public final static String TOPIC_QUEUE_TWO = "Queue@topic_two"; public final static String QUEUE_FANOUT_ONE = "Queue@fanout_one"; public final static String QUEUE_FANOUT_TWO = "Queue@fanout_two"; public final static String EXCHANGE_TOPIC = "Exchange@topic"; public final static String EXCHANGE_FANOUT = "Exchange@fanout"; public final static String ROUTINGKEY_TOPIC_ONE = "hello.key"; public final static String ROUTINGKEY_TOPIC_TWO = "*.key"; // direct模式隊(duì)列 @Bean public Queue directQueue() { return new Queue(QUEUE_DIRECT, true); } // topic 訂閱者模式隊(duì)列 @Bean public Queue topicQueueOne() { return new Queue(QUEUE_TOPIC_ONE, true); } @Bean public Queue topicQueueTwo() { return new Queue(TOPIC_QUEUE_TWO, true); } // fanout 廣播者模式隊(duì)列 @Bean public Queue fanoutQueueOne() { return new Queue(QUEUE_FANOUT_ONE, true); } @Bean public Queue fanoutQueueTwo() { return new Queue(QUEUE_FANOUT_TWO, true); } // topic 交換器 @Bean public TopicExchange topExchange() { return new TopicExchange(EXCHANGE_TOPIC); } // fanout 交換器 @Bean public FanoutExchange fanoutExchange() { return new FanoutExchange(EXCHANGE_FANOUT); } // 訂閱者模式綁定 @Bean public Binding topicExchangeBingingOne() { return BindingBuilder.bind(topicQueueOne()).to(topExchange()).with(ROUTINGKEY_TOPIC_ONE); } @Bean public Binding topicExchangeBingingTwo() { return BindingBuilder.bind(topicQueueTwo()).to(topicExchange()).with(ROUTINGKEY_TOPIC_TWO); } // 廣播模式綁定 @Bean public Binding fanoutExchangeBingingOne() { return BindingBuilder.bind(fanoutQueueOne()).to(fanoutExchange()); } @Bean public Binding fanoutExchangeBingingTwo() { return BindingBuilder.bind(fanoutQueueTwo()).to(fanoutExchange()); } }
法3:使用方配置(不推薦)
不推薦的原因
RabbitMQ的配置最好是統(tǒng)一在一個地方配置,分散配置不利于后期維護(hù)。
使用方法
@Component public class Receiver { @RabbitListener(queues = "hello") public void process(String hello) { System.out.println ("Receiver : " + hello); } @RabbitListener(bindings = @QueueBinding( exchange = @Exchange(value = "Exchange@topic.Hello",durable = "true",type = "topic"), value = @Queue(value = "Queue@Hello",durable = "true"), key = "key.#" )) public void processMessage1(Message message) { System.out.println(message); } }
法4:MQ服務(wù)端網(wǎng)頁(不推薦)
不推薦的原因
使用方法
添加交換器
http://localhost:15672/#/exchanges //例如:Exchange@topic.Hello
添加隊(duì)列
http://localhost:15672/#/queues //例如:Queue@Hello
交換器添加路由鍵
http://localhost:15672/#/exchanges=> 點(diǎn)擊交換器名字=> Binding=> 添加隊(duì)列與路由
到此這篇關(guān)于SpringBoot整合RabbitMQ實(shí)現(xiàn)交換機(jī)與隊(duì)列的綁定的文章就介紹到這了,更多相關(guān)SpringBoot RabbitMQ綁定交換機(jī) 隊(duì)列內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaWeb請求轉(zhuǎn)發(fā)和請求包含實(shí)現(xiàn)過程解析
這篇文章主要介紹了JavaWeb請求轉(zhuǎn)發(fā)和請求包含實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02kafka 啟動報錯 missingTopicsFatal is true的解決
這篇文章主要介紹了kafka 啟動報錯 missingTopicsFatal is true的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07SpringCloud?hystrix斷路器與全局解耦全面介紹
什么是服務(wù)降級?當(dāng)服務(wù)器壓力劇增的情況下,根據(jù)實(shí)際業(yè)務(wù)情況及流量,對一些服務(wù)和頁面有策略的不處理或換種簡單的方式處理,從而釋放服務(wù)器資源以保證核心交易正常運(yùn)作或高效運(yùn)作2022-10-10springboot2.X整合prometheus監(jiān)控的實(shí)例講解
這篇文章主要介紹了springboot2.X整合prometheus監(jiān)控的實(shí)例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03學(xué)習(xí)Java之Java中的異常處理機(jī)制詳解
在本文中,小編將帶領(lǐng)大家來學(xué)習(xí)Java的異常處理機(jī)制,包括異常機(jī)制、異常類型、如何捕獲異常、如何拋出異常以及如何創(chuàng)建自定義異常等核心內(nèi)容,感興趣的同學(xué)跟著小編一起來看看吧2023-08-08