前一篇我們實現了消息系統的靈活配置。代替了使用扇形(fanout)交換器的配置。使用直連(direct)交換器,并且基于路由鍵后可以有選擇性接收消息的能力。
雖然使用直連交換器可以改善我們的系統,但是它仍有局限性,它不能實現多重條件的路由。
在我們的消息系統中,我們不僅想要訂閱基于路由鍵的隊列,還想訂閱基于生產消息的源。這些概念來自于unix工具syslog。該日志基于嚴格的(info/warn/crit...) 和容易的(auth/cron/kern...)的路由方式。我們的例子比這個要簡單。
這個例子將會給我們很大的靈活性,比如我們既想監聽來‘cron'自錯誤的日志又想監聽來自‘kern'的所有日志。
為了實現這個靈活性,我們需要知道更多關于主題交換器的內容。
主題交換器
使用主題交換器時不能采用任意寫法的路由鍵,路由鍵的形式應該是由點分割的單詞。用什么詞都行,通常都是能表明意義的。例如"stock.usd.nyse", "nyse.vmw", "quick.orange.rabbit"。但字數大小被限制在最多255字節。
使用主題交換器定義路由鍵需要注意點2點
- *星號代表一個單詞。
- #井號代表0個或多個單詞。
定義符合主題交換器的路由鍵
在這個例子中,我們將發送所有描述動物的消息。這個消息將會和由3個單詞2個點構成的路由鍵一起發送。第一個單詞是表述速度,第二個描述顏色,第三個描述種類:"<speed>.<colour>.<species>"。
創建三種綁定,q1和鍵"*.orange.*"綁定,q2和"*.*.rabbit" 、"lazy.#"綁定。
三種綁定關系的概述為:
- q1 對橙色的動物感興趣。(隊列1)
- q2 對所有關于兔子和所有關于慢速的動物感興趣。(隊列2)
一個和路由鍵被設置成"quick.orange.rabbit"的消息將會被傳遞到q1、q2這兩個隊列中。"lazy.orange.elephant" 也會這樣。"quick.orange.fox"會去第一個隊列,"lazy.brown.fox"會去第二個隊列,"lazy.pink.rabbit"會去第二個隊列及時它匹配了2次綁定。"quick.brown.fox"因為不匹配哪也去不了,會被丟棄。
那么像"orange" 、 "quick.orange.male.rabbit"這樣的呢?因為沒有匹配到任何綁定也會被丟棄。
那么像"lazy.orange.male.rabbit"也是四個詞的路由鍵呢?,由于匹配到了lazy.#這個將會被傳遞到第二個隊列中。
主題交換器的小技巧
主題交換器是牛逼的并且表現的與其它交換器相似。
- 當一個隊列和 "#" 綁定鍵綁定時,該隊列能收到所有的消息,這點與扇形(fanout)交換器類似。
- 當不使用 "*" and "#" 時,主題交換器就與直連交換器沒啥兩樣。
代碼示例
代碼與之前的路由代碼沒啥兩樣,請看
config.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
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 { /** * 創建人:張博 * 時間:2018/3/5 上午10:45 * @apinote 定義主題交換器 */ @bean public topicexchange topicexchange() { return new topicexchange( "topic-exchange" ); } /** * 創建人:張博 * 時間:2018/3/5 上午10:48 * @apinote 定義自動刪除匿名隊列 */ @bean public queue autodeletequeue0() { return new anonymousqueue(); } /** * 創建人:張博 * 時間:2018/3/5 上午10:48 * @apinote 定義自動刪除匿名隊列 */ @bean public queue autodeletequeue1() { return new anonymousqueue(); } /** * 創建人:張博 * 時間:2018/3/5 上午10:48 * @param topicexchange 主題交換器 * @param autodeletequeue0 自動刪除隊列 * @apinote 綁定使用路由鍵為 orange 的 autodeletequeue0 隊列到主題交換器上 * @return binding */ @bean public binding binding0a(topicexchange topicexchange, queue autodeletequeue0) { return bindingbuilder.bind(autodeletequeue0).to(topicexchange).with( "*.orange.*" ); } /** * 創建人:張博 * 時間:2018/3/5 上午10:48 * @param topicexchange 主題交換器 * @param autodeletequeue1 自動刪除隊列 * @apinote 綁定使用路由鍵為 black 的 autodeletequeue1 隊列到主題交換器上 * @return binding */ @bean public binding binding1a(topicexchange topicexchange, queue autodeletequeue1) { return bindingbuilder.bind(autodeletequeue1).to(topicexchange).with( "*.*.rabbit" ); } /** * 創建人:張博 * 時間:2018/3/5 上午10:48 * @param topicexchange 主題交換器 * @param autodeletequeue1 自動刪除隊列 * @apinote 綁定使用路由鍵為 green 的 autodeletequeue1 隊列到主題交換器上 * @return binding */ @bean public binding binding1b(topicexchange topicexchange, queue autodeletequeue1) { return bindingbuilder.bind(autodeletequeue1).to(topicexchange).with( "lazy.#" ); } } |
receiver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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(); } } |
測試結果我就不放了 大家請自行查看。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.jianshu.com/p/66c0072e9bf4