一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - spring boot整合RabbitMQ實例詳解(Fanout模式)

spring boot整合RabbitMQ實例詳解(Fanout模式)

2020-09-19 18:36牛頭人 JAVA教程

這篇文章主要介紹了spring boot整合RabbitMQ的實例講解(Fanout模式),非常不錯,具有參考借鑒價值,需要的朋友可以參考下

1.Fanout Exchange介紹

Fanout Exchange 消息廣播的模式,不管路由鍵或者是路由模式,會把消息發給綁定給它的全部隊列,如果配置了routing_key會被忽略。

spring boot整合RabbitMQ實例詳解(Fanout模式)

如上圖所示,即當使用fanout交換器時,他會將消息廣播到與該交換器綁定的所有隊列上,這有利于你對單條消息做不同的反應。

例如存在以下場景:一個web服務要在用戶完善信息時,獲得積分獎勵,這樣你就可以創建兩個對列,一個用來處理用戶信息的請求,另一個對列獲取這條消息是來完成積分獎勵的任務。

2.代碼示例

1).Queue配置類

FanoutRabbitConfig.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
package com.example.rabbitmqfanout;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FanoutRabbitConfig {
  //創建隊列
  @Bean
  public Queue AMessage() {
    return new Queue("fanout.A");
  }
  //創建隊列
  @Bean
  public Queue BMessage() {
    return new Queue("fanout.B");
  }
  //創建隊列
  @Bean
  public Queue CMessage() {
    return new Queue("fanout.C");
  }
  //創建Fanout交換器
  @Bean
  FanoutExchange fanoutExchange() {
    return new FanoutExchange("fanoutExchange");
  }
  //將對列綁定到Fanout交換器
  @Bean
  Binding bindingExchangeA(Queue AMessage,FanoutExchange fanoutExchange) {
    return BindingBuilder.bind(AMessage).to(fanoutExchange);
  }
  //將對列綁定到Fanout交換器
  @Bean
  Binding bindingExchangeB(Queue BMessage, FanoutExchange fanoutExchange) {
    return BindingBuilder.bind(BMessage).to(fanoutExchange);
  }
  //將對列綁定到Fanout交換器
  @Bean
  Binding bindingExchangeC(Queue CMessage, FanoutExchange fanoutExchange) {
    return BindingBuilder.bind(CMessage).to(fanoutExchange);
  
}

2).消息生產者

FanoutSender.java類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.example.rabbitmqfanout.rabbitmq;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class FanoutSender {
  @Autowired
  private AmqpTemplate rabbitTemplate;
  public void send() {
    String context = "hi, fanout msg ";
    System.out.println("Sender : " + context);
    this.rabbitTemplate.convertAndSend("fanoutExchange","", context);
  }
}

3).消息消費者

FanoutReceiverA.java類:

?
1
2
3
4
5
6
7
8
9
10
11
12
package com.example.rabbitmqfanout.rabbitmq;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "fanout.A")
public class FanoutReceiverA {
   @RabbitHandler
   public void process(String message) {
     System.out.println("fanout Receiver A : " + message);
  }
}

FanoutReceiverB.java類:

?
1
2
3
4
5
6
7
8
9
10
11
12
package com.example.rabbitmqfanout.rabbitmq;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "fanout.B")
public class FanoutReceiverB {
  @RabbitHandler
  public void process(String message) {
    System.out.println("fanout Receiver B: " + message);
  }
}

FanoutReceiverC.java類:

?
1
2
3
4
5
6
7
8
9
10
11
12
package com.example.rabbitmqfanout.rabbitmq;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "fanout.C")
public class FanoutReceiverC {
  @RabbitHandler
  public void process(String message) {
    System.out.println("fanout Receiver C: " + message);
  }
}

4).測試

FanoutTest.java類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.example.rabbitmqfanout.rabbitmq;
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;
@RunWith(SpringRunner.class)
@SpringBootTest
public class FanoutTest {
  @Autowired
  private FanoutSender sender;
  @Test
  public void fanoutSender() throws Exception {
    sender.send();
  }
}

以上所述是小編給大家介紹的spring boot整合RabbitMQ(Fanout模式),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:http://www.cnblogs.com/web424/p/6767713.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美色图亚洲 | 国产精品日韩欧美在线 | 22222色男人的天堂 | 日韩精品成人 | 九九九好热在线 | 九九在线精品亚洲国产 | 色综合色狠狠天天综合色 | 热剧库| 网红刘婷hd国产高清 | 亚洲第一综合天堂另类专 | 青青草精品在线观看 | 天美传媒影视在线免费观看 | 亚洲四虎永久在线播放 | 超强台风免费观看完整版视频 | 日本花季传媒2020旧版安卓 | 4455四色永久免费 | 欧美整片完整片视频在线 | 欧美一区二区三区大片 | 久久嫩草影院网站 | 亚洲国产精品高清在线 | 亚洲六月丁香婷婷综合 | 久久九九有精品国产23百花影院 | 亚洲AV无码乱码在线观看浪潮 | 亚洲欧美优优色在线影院 | yellow视频在线观看 | 久久这里只有精品视频9 | 亚久久伊人精品青青草原2020 | 精品福利一区二区免费视频 | 无码11久岁箩筣 | 欧美性xxx狂流白浆 欧美性f | 国产成人精品一区二三区 | 国产aⅴ一区二区三区 | 日本艳鉧动漫1~6在线观看 | 精品操 | 91国语自产拍在线观看 | 精品久久香蕉国产线看观看亚洲 | 99热国产这里只有精品 | 美妇在线 | 国产欧美综合一区二区 | 国产精品免费看久久久香蕉 | 免费毛片 |