SpringBoot整合RabbitMQ的5種模式的注解綁定詳解
RabbitMQ 5種模式的注解綁定
1、導(dǎo)入依賴
<!--AMQP依賴,包含RabbitMQ-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!--對象轉(zhuǎn)換-->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>2、配置連接信息
spring:
rabbitmq:
host: localhost
port: 5672
username: root
password: root
virtual-host: /
# listener:
# simple:
# prefetch: 1 # 工作隊列能者多勞模式
3、5種使用模式
1、HelloWorld模式
一個隊列一個消費者
// 消息發(fā)布
@Test
void testHelloWorldMode(){
rabbitTemplate.convertAndSend("helloworld_queue","hello world!");
}
// 消息訂閱
@RabbitListener(queuesToDeclare = {@Queue(name = "helloworld_queue")})
public void helloWorldC1(String msg){
System.out.println("helloWorldC1:-------->"+msg);
}
2、Work模式 按均分配
一個隊列,多個消費者
注意:一條消息只能被消費一次,默認(rèn)是按均分配,在消費者開始消費之前隊列中的消息就已經(jīng)分配好了
往隊列中放入50條消息
@Test
void testWorkMode(){
String msg = "work mode";
for (int i = 1; i <= 50; i++) {
rabbitTemplate.convertAndSend("work_zs_queue","work mode"+i);
}
}創(chuàng)建兩個消費者,并且設(shè)置20ms 和 200ms 的延遲
@RabbitListener(queuesToDeclare = {@Queue(name = "work_zs_queue")})
@SneakyThrows
public void workc1(String msg){
Thread.sleep(20);
System.out.println("workc1:---------------->"+msg);
}
@RabbitListener(queuesToDeclare = {@Queue(name="work_zs_queue")})
@SneakyThrows
public void workc2(String msg){
Thread.sleep(200);
System.err.println("workc2:------------------>"+msg);
}運行結(jié)果:
可以發(fā)現(xiàn)workc1比workc2提前完成消費任務(wù),并且c1 c2是按照奇偶數(shù)順序消費的任務(wù),這也進(jìn)一步驗證了在消費開始前就已經(jīng)分配好了任務(wù)
這種按均分配的效果效率低下,我們應(yīng)該遵循能者多勞的方式去分配任務(wù)

能者多勞
修改配置文件,讓消費者一次只能接收一個任務(wù),當(dāng)前任務(wù)消費完才可以接收下一個任務(wù)
spring:
rabbitmq:
host: 192.168.137.7
port: 5672
username: root
password: root
virtual-host: /
listener: # 消息確認(rèn)機制
simple:
prefetch: 1
重新啟動運行代碼
![[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-yC6unliz-1651579009611)(項目規(guī)約.assets/image-20220503170440687.jpg)]](http://img.jbzj.com/file_images/article/202401/202401251019489.jpg)
3、Fanout模式
fanout模式也叫廣播模式,每一條消息多可以被綁定在同一個交換機上的所有隊列的消費者消費

參數(shù)1:交換機:fanout_exchange
參數(shù)2:routingkey 在fanout模式不使用,會在direct和topic模式使用
參數(shù)3:發(fā)送的消息
@Test
void testFanoutMode(){
rabbitTemplate.convertAndSend("fanout_exchange","","fanout mode 1");
rabbitTemplate.convertAndSend("fanout_exchange","","fanout mode 2");
}
定義消費者
使用@RabbitListener注解中的bindings聲明并綁定交換機和隊列
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = "fanout_queue1"),
exchange = @Exchange(name = "fanout_exchange2",type = ExchangeTypes.FANOUT)
))
public void fanoutc1(String msg){
System.out.println("fanoutc1:------------------>"+msg);
}
@RabbitListener(bindings=@QueueBinding(
value = @Queue(name = "fanout_queue2"),
exchange = @Exchange(name = "fanout_exchange2",type = ExchangeTypes.FANOUT)
))
public void fanoutc2(String msg){
System.out.println("fanoutc1:------------------>"+msg);
}運行結(jié)果:
每一條消息都會被所有消費者消費

4、direct模式
direct模式與fanout模式的區(qū)別在于,隊列都是綁定同一個交換機,但是在隊列上會添加routingkey標(biāo)識
消費者會根據(jù)不同的表示去消費對應(yīng)隊列中的消息
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = "direct_queue1"),
exchange = @Exchange(name = "direct_zs_exchange",type = ExchangeTypes.DIRECT),
key = "zs_news1"
))
public void direct1(String msg){
System.out.println("direct_queuq1:---------------------->"+msg);
}
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = "direct_queue2"),
exchange = @Exchange(name = "direct_zs_exchange",type = ExchangeTypes.DIRECT),
key = "zs_news2"
))
public void direct2(String msg){
System.out.println("direct_queuq2:---------------------->"+msg);
}
@Test
void testDirectMode(){
rabbitTemplate.convertAndSend("direct_zs_exchange","zs_news1","direct mode1");
rabbitTemplate.convertAndSend("direct_zs_exchange","zs_news2","direct mode2");
}
5、topic模式
- Topic交換機接收的消息RoutingKey必須是多個單詞,以 **.** 分割
- Topic交換機與隊列綁定時的bindingKey可以指定通配符
- #:代表0個或多個詞
- *:代表1個詞
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = "topic_queue1"),
exchange = @Exchange(name = "topic_zs_exchange",type = ExchangeTypes.TOPIC),
key = "zs_new.#"
))
public void topic1(String msg){
User user = JSONUtil.toBean(msg, User.class);
System.out.println(user.toString());
}
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = "topic_queue1"),
exchange = @Exchange(name = "topic_zs_exchange",type = ExchangeTypes.TOPIC),
key = "#.zs_new"
))
public void topic2(String msg){
User user = JSONUtil.toBean(msg, User.class);
System.out.println(user.toString());
}
@Test
void testTopicMode(){
String jsonStr1 = JSONUtil.toJsonStr(new User("小爽", 22));
rabbitTemplate.convertAndSend("topic_zs_exchange","zs_new.user",jsonStr1);
String jsonStr2 = JSONUtil.toJsonStr(new User("路飛", 17));
rabbitTemplate.convertAndSend("topic_zs_exchange","lufei.zs_new",jsonStr2);
}![[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-bM3FuukA-1651579009617)(使用攻略.assets/image-20220503195525092.jpg)]](http://img.jbzj.com/file_images/article/202401/2024012510194915.jpg)
到此這篇關(guān)于SpringBoot整合RabbitMQ的5種模式的注解綁定詳解的文章就介紹到這了,更多相關(guān)RabbitMQ 5種模式的注解綁定內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
教你怎么用java實現(xiàn)客戶端與服務(wù)器一問一答
這篇文章主要介紹了教你怎么用java實現(xiàn)客戶端與服務(wù)器一問一答,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
mybatis如何獲取剛剛新插入數(shù)據(jù)的主鍵值id
這篇文章主要介紹了mybatis如何獲取剛剛新插入數(shù)據(jù)的主鍵值id問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
packages思維及使用Java添加Android平臺特定實現(xiàn)
這篇文章主要為大家介紹了packages思維及使用Java添加Android平臺特定實現(xiàn)在Flutter框架里的體現(xiàn)和運用詳解,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12

