您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)spring boot如何使用RabbitMQ實(shí)現(xiàn)topic主題,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
主題交換器
使用主題交換器時(shí)不能采用任意寫(xiě)法的路由鍵,路由鍵的形式應(yīng)該是由點(diǎn)分割的單詞。用什么詞都行,通常都是能表明意義的。例如"stock.usd.nyse", "nyse.vmw", "quick.orange.rabbit"。但字?jǐn)?shù)大小被限制在最多255字節(jié)。
使用主題交換器定義路由鍵需要注意點(diǎn)2點(diǎn)
*星號(hào)代表一個(gè)單詞。
#井號(hào)代表0個(gè)或多個(gè)單詞。
定義符合主題交換器的路由鍵
在這個(gè)例子中,我們將發(fā)送所有描述動(dòng)物的消息。這個(gè)消息將會(huì)和由3個(gè)單詞2個(gè)點(diǎn)構(gòu)成的路由鍵一起發(fā)送。第一個(gè)單詞是表述速度,第二個(gè)描述顏色,第三個(gè)描述種類:"<speed>.<colour>.<species>"。
創(chuàng)建三種綁定,Q1和鍵"*.orange.*"綁定,Q2和"*.*.rabbit" 、"lazy.#"綁定。
三種綁定關(guān)系的概述為:
Q1 對(duì)橙色的動(dòng)物感興趣。(隊(duì)列1)
Q2 對(duì)所有關(guān)于兔子和所有關(guān)于慢速的動(dòng)物感興趣。(隊(duì)列2)
一個(gè)和路由鍵被設(shè)置成"quick.orange.rabbit"的消息將會(huì)被傳遞到Q1、Q2這兩個(gè)隊(duì)列中。"lazy.orange.elephant" 也會(huì)這樣。"quick.orange.fox"會(huì)去第一個(gè)隊(duì)列,"lazy.brown.fox"會(huì)去第二個(gè)隊(duì)列,"lazy.pink.rabbit"會(huì)去第二個(gè)隊(duì)列及時(shí)它匹配了2次綁定。"quick.brown.fox"因?yàn)椴黄ヅ淠囊踩ゲ涣?,?huì)被丟棄。
那么像"orange" 、 "quick.orange.male.rabbit"這樣的呢?因?yàn)闆](méi)有匹配到任何綁定也會(huì)被丟棄。
那么像"lazy.orange.male.rabbit"也是四個(gè)詞的路由鍵呢?,由于匹配到了lazy.#這個(gè)將會(huì)被傳遞到第二個(gè)隊(duì)列中。
主題交換器的小技巧
主題交換器是牛逼的并且表現(xiàn)的與其它交換器相似。
當(dāng)一個(gè)隊(duì)列和 "#" 綁定鍵綁定時(shí),該隊(duì)列能收到所有的消息,這點(diǎn)與扇形(fanout)交換器類似。
當(dāng)不使用 "*" and "#" 時(shí),主題交換器就與直連交換器沒(méi)啥兩樣。
代碼示例
代碼與之前的路由代碼沒(méi)啥兩樣,請(qǐng)看
Config.java
package com.zb.rabbitMQtest.t5topics.config; import org.springframework.amqp.core.*; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author 張博 */ @Configuration(value = "t5Config") public class Config { /** * 創(chuàng)建人:張博 * 時(shí)間:2018/3/5 上午10:45 * @apiNote 定義主題交換器 */ @Bean public TopicExchange topicExchange() { return new TopicExchange("topic-exchange"); } /** * 創(chuàng)建人:張博 * 時(shí)間:2018/3/5 上午10:48 * @apiNote 定義自動(dòng)刪除匿名隊(duì)列 */ @Bean public Queue autoDeleteQueue0() { return new AnonymousQueue(); } /** * 創(chuàng)建人:張博 * 時(shí)間:2018/3/5 上午10:48 * @apiNote 定義自動(dòng)刪除匿名隊(duì)列 */ @Bean public Queue autoDeleteQueue1() { return new AnonymousQueue(); } /** * 創(chuàng)建人:張博 * 時(shí)間:2018/3/5 上午10:48 * @param topicExchange 主題交換器 * @param autoDeleteQueue0 自動(dòng)刪除隊(duì)列 * @apiNote 綁定使用路由鍵為 orange 的 autoDeleteQueue0 隊(duì)列到主題交換器上 * @return Binding */ @Bean public Binding binding0a(TopicExchange topicExchange, Queue autoDeleteQueue0) { return BindingBuilder.bind(autoDeleteQueue0).to(topicExchange).with("*.orange.*"); } /** * 創(chuàng)建人:張博 * 時(shí)間:2018/3/5 上午10:48 * @param topicExchange 主題交換器 * @param autoDeleteQueue1 自動(dòng)刪除隊(duì)列 * @apiNote 綁定使用路由鍵為 black 的 autoDeleteQueue1 隊(duì)列到主題交換器上 * @return Binding */ @Bean public Binding binding1a(TopicExchange topicExchange, Queue autoDeleteQueue1) { return BindingBuilder.bind(autoDeleteQueue1).to(topicExchange).with("*.*.rabbit"); } /** * 創(chuàng)建人:張博 * 時(shí)間:2018/3/5 上午10:48 * @param topicExchange 主題交換器 * @param autoDeleteQueue1 自動(dòng)刪除隊(duì)列 * @apiNote 綁定使用路由鍵為 green 的 autoDeleteQueue1 隊(duì)列到主題交換器上 * @return Binding */ @Bean public Binding binding1b(TopicExchange topicExchange, Queue autoDeleteQueue1) { return BindingBuilder.bind(autoDeleteQueue1).to(topicExchange).with("lazy.#"); } }
Receiver.java
package com.zb.rabbitMQtest.t5topics.receiver; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; /** * @author 張博 */ @Component(value = "t5Receiver") public class Receiver { @RabbitListener(queues = "#{autoDeleteQueue0.name}") public void receiver0(String str) { System.out.println("receiver0++++++++++:" + str); //try { // Thread.sleep(1000); //} catch (InterruptedException e) { // e.printStackTrace(); //} } @RabbitListener(queues = "#{autoDeleteQueue1.name}") public void receiver1(String str) { System.out.println("receiver1++++++++++:" + str); //try { // Thread.sleep(1000); //} catch (InterruptedException e) { // e.printStackTrace(); //} } }
Send.java
package com.zb.rabbitMQtest.t5topics.send; import org.springframework.amqp.core.TopicExchange; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** * @author 張博 */ @Component(value = "t5Send") public class Send { @Autowired private TopicExchange topicExchange; @Autowired private RabbitTemplate rabbitTemplate; private String[] keys = {"quick.orange.rabbit", "lazy.orange.elephant", "quick.orange.fox", "lazy.brown.fox", "lazy.pink.rabbit", "quick.brown.fox"}; public void send() { String message = "哈哈哈"; for (int i = 0; i < 5; i++) { System.out.println("send++++++++++:".concat(message)); rabbitTemplate.convertAndSend(topicExchange.getName(), keys[5], message); } } }
SendTest.java
package com.zb.rabbitMQtest.t5topics.send; 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; /** * @author 張博 */ @RunWith(SpringRunner.class) @SpringBootTest public class SendTest { @Autowired private Send send; @Test public void send() throws Exception { send.send(); } }
關(guān)于“spring boot如何使用RabbitMQ實(shí)現(xiàn)topic主題”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。