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

RabbitMQ的交换机(原理及代码实现)

1.交换机类型

  • Fanout Exchange(扇形)
  • Direct Exchange(直连)
  • opic Exchange(主题)
  • Headers Exchange(头部)

2.Fanout Exchange

2.1 简介

Fanout 扇形的,散开的; 扇形交换机

投递到所有绑定的队列,不需要路由键,不需要进行路由键的匹配,相当于广播、群发;
如下图所示
在这里插入图片描述
P代表provider提供者,X代表exchange交换机,第三部分代表队列

2.2 代码示例

2.2.1 扇形交换机与队列

定义交换机主要由3部分组成,

  • 1.定义交换机
  • 2.定义队列
  • 3.绑定交换机
@Configuration
public class RabbitConfig {//rabbitmq三部曲//1.定义交换机@Beanpublic FanoutExchange fanoutExchange(){return new FanoutExchange("exchange.fanout");}//2.定义队列//此处定义两个队列@Beanpublic Queue queueA(){return new Queue("queue.fanout.a");}@Beanpublic Queue queueB(){return new Queue("queue.fanout.b");}//3.绑定交换机@Beanpublic Binding bindingA(FanoutExchange fanoutExchange,Queue queueA){//将队列A绑定到扇形交换机return BindingBuilder.bind(queueA).to(fanoutExchange);}@Beanpublic Binding bindingB(FanoutExchange fanoutExchange,Queue queueB){//将队列A绑定到扇形交换机return BindingBuilder.bind(queueB).to(fanoutExchange);}
}

2.2.2 模拟provider发送消息

发送消息模拟

@Component
@Slf4j
public class MessageService {@Resourceprivate RabbitTemplate rabbitTemplate;public void senMsg(){//定义消息String msg="hello world";//发消息Message message= new Message(msg.getBytes());rabbitTemplate.convertAndSend("exchange.fanout","",message);log.info("消息发送完毕,发送时间为:{}", new Date());}
}

2.2.3 接受扇形交换机信息

3.Direct Exchange

根据路由路由键(Routing Key)精确匹配(一模一样)进行路由消息队列;
与扇形交换机不同的是,直连交换机必须要绑定key
在这里插入图片描述

3.2代码示例

@Configuration
public class RabbitConfig {//rabbitmq三部曲//1.定义交换机@Beanpublic DirectExchange directExchange(){return ExchangeBuilder.directExchange("exchange.direct").build();}//2.定义队列//此处定义两个队列@Beanpublic Queue queueA(){return new Queue("queue.direct.a");}@Beanpublic Queue queueB(){return new Queue("queue.direct.b");}//3.绑定交换机@Beanpublic Binding bindingA(DirectExchange directExchange,Queue queueA){//将队列A绑定到扇形交换机return BindingBuilder.bind(queueA).to(directExchange).with("error");}@Beanpublic Binding bindingB1(DirectExchange directExchange,Queue queueB){//将队列A绑定到扇形交换机return BindingBuilder.bind(queueB).to(directExchange).with("error");}@Beanpublic Binding bindingB2(DirectExchange directExchange,Queue queueB){//将队列A绑定到扇形交换机return BindingBuilder.bind(queueB).to(directExchange).with("info");}@Beanpublic Binding bindingB3(DirectExchange directExchange,Queue queueB){//将队列A绑定到扇形交换机return BindingBuilder.bind(queueB).to(directExchange).with("warning");}
}

4. Topic Exchange

通配符匹配,相当于模糊匹配;

#匹配多个单词,用来表示任意数量(零个或多个)单词
*匹配一个单词(必须有一个,而且只有一个),用.隔开的为一个单词

5.Headers Exchange

基于消息内容中的headers属性进行匹配;

用的比较少

http://www.lryc.cn/news/207618.html

相关文章:

  • 【C++进阶】pair容器
  • Linux--进程等待
  • VMware CentOS 虚拟机扩容
  • CentOS 编译安装 nginx
  • 学习笔记-MongoDB(命令增删改查,聚合,权限管理,索引,java使用)
  • 第13期 | GPTSecurity周报
  • OpenCV学习(一)——图像读取
  • 并发编程- 线程池ForkJoinPool工作原理分析(实践)
  • 小程序原生开发中的onLoad和onShow
  • springcloud技术栈以及相关组件
  • An Early Evaluation of GPT-4V(ision)
  • Vue在移动端实现图片的手指缩放
  • Failed to prepare the device for development
  • PPT文档图片设计素材资源下载站模板源码/织梦内核(带用户中心+VIP充值系统+安装教程)
  • 万能鼠标设置 SteerMouse v5.6.8
  • 16 用于NOMA IoT网络上行链路安全速率最大化的HAP和UAV协作框架
  • 【C++】STL容器——vector类的使用指南(含代码演示)(11)
  • elementui 修改 el_table 表格颜色,表格下方多了一条线问题
  • 阿里云/腾讯云国际站代理:阿里云服务器介绍
  • Go学习第十章——文件操作,Json和测试
  • 学习不同概率分布(二项分布、泊松分布等)概念及基础语法
  • 在3台不联网的 CentOS 7.8 服务器上部署 Elasticsearch 6.8 集群
  • CentOS 7
  • 个人记账理财软件 Money Pro mac中文版软件介绍
  • DSP 开发教程(0): 汇总
  • YouTrack 中如何设置邮件通知
  • Prevalence and prevention of large language model use in crowd work
  • 微信小程序学习(02)
  • Transit path
  • backend-learning: personal blog(1)