当前位置: 首页 > news >正文

RabbitMQ 声明队列和交换机详解

RabbitMQ 声明队列和交换机详解

一、为什么需要声明队列和交换机?

RabbitMQ先声明再使用的机制:

  • 队列负责存储消息
  • 交换机负责路由消息
  • 绑定关系决定消息从交换机到队列的路径

如果没有事先声明:

  • 队列/交换机不存在时,发送消息会失败
  • 队列没有绑定到交换机时,消息会丢失(除非设置了备用交换机)

二、声明交换机(Exchange)

2.1 交换机参数说明

RabbitMQ 提供四种类型(direct、fanout、topic、headers),声明时需要指定以下参数:

参数类型说明
nameString交换机名称(不为空字符串)
typeString类型:directfanouttopicheaders
durableboolean是否持久化(重启 RabbitMQ 后仍存在)
autoDeleteboolean是否自动删除(最后一个队列解绑后删除)
argumentsMap额外参数(如 TTL、死信交换机配置)

2.2 Java 原生声明交换机

channel.exchangeDeclare("my.direct.exchange", // 交换机名称BuiltinExchangeType.DIRECT, // 类型true,  // durablefalse, // autoDeletenull   // arguments
);

2.3 Spring AMQP 声明交换机

@Bean
public DirectExchange directExchange() {return new DirectExchange("my.direct.exchange", true, false);
}

Spring 会自动在应用启动时向 RabbitMQ 发送声明请求。

三、声明队列(Queue)

3.1 队列参数说明

参数类型说明
nameString队列名称(匿名队列可由 RabbitMQ 自动生成)
durableboolean是否持久化(消息是否持久化取决于发送时的 deliveryMode
exclusiveboolean是否排他队列(仅连接可见,断开即删除)
autoDeleteboolean是否自动删除(最后一个消费者断开时删除)
argumentsMap额外参数(TTL、死信队列、最大长度等)

3.2 Java 原生声明队列

channel.queueDeclare("my.queue", // 队列名称true,       // durablefalse,      // exclusivefalse,      // autoDeletenull        // arguments
);

3.3 Spring AMQP 声明队列

@Bean
public Queue myQueue() {return new Queue("my.queue", true, false, false);
}

四、绑定交换机和队列

4.1 Java 原生绑定

channel.queueBind("my.queue",           // 队列名称"my.direct.exchange", // 交换机名称"order.create"        // routingKey
);

4.2 Spring AMQP 绑定

@Bean
public Binding binding() {return BindingBuilder.bind(myQueue()).to(directExchange()).with("order.create");
}

五、实战示例

5.2 fanout示例

package com.itheima.consumer.config;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 FanoutConfig {/*** 声明交换机* @return Fanout类型交换机*/@Beanpublic FanoutExchange fanoutExchange(){return new FanoutExchange("hmall.fanout");}/*** 第1个队列*/@Beanpublic Queue fanoutQueue1(){return new Queue("fanout.queue1");}/*** 绑定队列和交换机*/@Beanpublic Binding bindingQueue1(Queue fanoutQueue1, FanoutExchange fanoutExchange){return BindingBuilder.bind(fanoutQueue1).to(fanoutExchange);}/*** 第2个队列*/@Beanpublic Queue fanoutQueue2(){return new Queue("fanout.queue2");}/*** 绑定队列和交换机*/@Beanpublic Binding bindingQueue2(Queue fanoutQueue2, FanoutExchange fanoutExchange){return BindingBuilder.bind(fanoutQueue2).to(fanoutExchange);}
}

5.3 direct示例

package com.itheima.consumer.config;import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class DirectConfig {/*** 声明交换机* @return Direct类型交换机*/@Beanpublic DirectExchange directExchange(){return ExchangeBuilder.directExchange("hmall.direct").build();}/*** 第1个队列*/@Beanpublic Queue directQueue1(){return new Queue("direct.queue1");}/*** 绑定队列和交换机*/@Beanpublic Binding bindingQueue1WithRed(Queue directQueue1, DirectExchange directExchange){return BindingBuilder.bind(directQueue1).to(directExchange).with("red");}/*** 绑定队列和交换机*/@Beanpublic Binding bindingQueue1WithBlue(Queue directQueue1, DirectExchange directExchange){return BindingBuilder.bind(directQueue1).to(directExchange).with("blue");}/*** 第2个队列*/@Beanpublic Queue directQueue2(){return new Queue("direct.queue2");}/*** 绑定队列和交换机*/@Beanpublic Binding bindingQueue2WithRed(Queue directQueue2, DirectExchange directExchange){return BindingBuilder.bind(directQueue2).to(directExchange).with("red");}/*** 绑定队列和交换机*/@Beanpublic Binding bindingQueue2WithYellow(Queue directQueue2, DirectExchange directExchange){return BindingBuilder.bind(directQueue2).to(directExchange).with("yellow");}
}

六、基于注解声明

  • Direct模式
@RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue1"),exchange = @Exchange(name = "hmall.direct", type = ExchangeTypes.DIRECT),key = {"red", "blue"}
))
public void listenDirectQueue1(String msg){System.out.println("消费者1接收到direct.queue1的消息:【" + msg + "】");
}@RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue2"),exchange = @Exchange(name = "hmall.direct", type = ExchangeTypes.DIRECT),key = {"red", "yellow"}
))
public void listenDirectQueue2(String msg){System.out.println("消费者2接收到direct.queue2的消息:【" + msg + "】");
}
  • Topic模式
@RabbitListener(bindings = @QueueBinding(value = @Queue(name = "topic.queue1"),exchange = @Exchange(name = "hmall.topic", type = ExchangeTypes.TOPIC),key = "china.#"
))
public void listenTopicQueue1(String msg){System.out.println("消费者1接收到topic.queue1的消息:【" + msg + "】");
}@RabbitListener(bindings = @QueueBinding(value = @Queue(name = "topic.queue2"),exchange = @Exchange(name = "hmall.topic", type = ExchangeTypes.TOPIC),key = "#.news"
))
public void listenTopicQueue2(String msg){System.out.println("消费者2接收到topic.queue2的消息:【" + msg + "】");
}
http://www.dtcms.com/a/325221.html

相关文章:

  • 飞算JavaAI vs 传统开发:效率与质量的双重突破
  • MLAG双活网络妙招:BGP + 静态VRRP实现智能负载均衡
  • 新出Hi3591BV100 AI处理器
  • Agent用户体验设计:人机交互的最佳实践
  • 【前端基础】16、结构伪类(注:粗略说明)
  • 卫星授时原理详解
  • 模考50题卷一 05
  • 《算法导论》第 19 章 - 斐波那契堆
  • 【Node.js从 0 到 1:入门实战与项目驱动】1.4 Node.js 的发展与生态(历史版本、LTS 版本、npm 生态系统)
  • Apache RocketMQ:消息可靠性、顺序性与幂等处理的全面实践
  • 使用docker compose 部署dockge
  • Nmap 渗透测试弹药库:精准扫描与隐蔽渗透技术手册
  • 心理咨询|学生心理咨询评估系统|基于Springboot的学生心理咨询评估系统设计与实现(源码+数据库+文档)
  • CSS accent-color:一键定制表单元素的主题色,告别样式冗余
  • GSON 框架下百度天气 JSON 数据转 JavaBean 的实战攻略
  • 基于 Spring Boot 的登录功能实现详解
  • 基于飞算JavaAI的日志监测系统开发实践:从智能生成到全链路落地
  • 34-Hive SQL DML语法之查询数据-3
  • <typeAliases>
  • Django路由学习笔记
  • word格式设置-论文写作,样式,字号等
  • 在Debian上安装MySQL
  • java设计模式之开闭原则使用举例
  • 5种无需USB线将照片从手机传输到笔记本电脑的方法
  • Linux 流编辑器 sed 详解
  • 实体瘤疗效评估标准
  • 图像打标工具/方法的分类和特点说明
  • Launcher3启动
  • Ansys Mechanical中的声学分析
  • 人工智能与农业:农业的革新