RabbitMQ:SpringAMQP Fanout Exchange(扇型交换机)

目录


扇形交换机也叫做广播交换机,通过交换机将消息发送给所有的队列。

生产者源码
消费者源码

一、案例需求

  1. 在RabbitMQ控制台中,声明队列fanout.queue1和fanout.queue2。
  2. 在RabbitMQ控制台中,声明交换机mt.fanout,将两个队列与其绑定。
  3. 在生产者服务中,向交换机mt.fanout发送消息。
  4. 在消费者服务中,编写两个消费者方法,分别监听fanout.queue1和fanout.queue2。

二、基础配置

首先需要创建两个队列fanout.queue1和fanout.queue2。

创建一个广播交换机mt.fanout,需要注意的是,在创建交换机的时候需要修改交换机的类型fanount广播交换机。

交换机创建之后,点击交换机的名称,绑定交换机与队列之间的关系。

三、代码实现

生产者

java 复制代码
/**
* 给交换机发送消息(广播交换机)
 * exchange:交换机的名称
 * routingKey:直接填写NULL
 * Object:传递的参数
 * @throws InterruptedException
 */
@Test
public void fanoutExchangeTest() throws InterruptedException {
    String exchangeName = "mt.fanout";
    String message = "Fanout Exchange ......";
    rabbitTemplate.convertAndSend(exchangeName, null, message);
}

消费者,特别注意的是消费者绑定的是队列,而不是交换机。

java 复制代码
@RabbitListener(queues = "fanout.queue1")
public void listenFanoutQueue1(String message) throws InterruptedException {
    System.out.println(String.format("消费者1,收到了fanout.queue1: %s", message));
}

@RabbitListener(queues = "fanout.queue2")
public void listenFanoutQueue2(String message) throws InterruptedException {
    System.err.println(String.format("消费者2,收到了fanout.queue2: %s", message));
}
相关推荐
学长毕设14 小时前
基于SpringBoot智慧养老服务平台设计与实现-附源码
java·spring boot·后端
千寻技术帮15 小时前
10454_基于Springboot的企业购销链管理系统
spring boot·mysql·vue·毕业设计·进销存
Forget the Dream16 小时前
Spring AI 2.0 系列(二):第一个 ChatClient——从注入到一次完整调用
java·人工智能·spring boot·ai·springai
txxzjmzlh17 小时前
RabbitMQ安装
分布式·rabbitmq
她的男孩19 小时前
设计器能拖抄送,一点发布却报 flowable-servicetask-invalid-type:Forge Admin 部署前改写节点属性
spring boot·低代码·工作流引擎
学长毕设2 天前
基于SpringBoot社区快递驿站物流配送系统-附源码
java·spring boot·后端
雾隐隐o2 天前
优搭校园 01|项目概览、技术选型与初始化
spring boot·mysql·vue
雪芽蓝域zzs2 天前
第 10 章:Maven 打包
spring boot
RuoyiOffice2 天前
SpringBoot3+Vue3 采购到付款:ERP、WMS与财务如何按同一来源协同
java·spring boot·架构·vue
企业数字化笔记2 天前
报表上线怎么对账?口径回归、权限抽检与差异定位
java·spring boot·mysql