RabbitMQ交换机的讲解
一、交换机类型
1.扇形交换机
扇形交换机是最基本的交换机,它的作用是广播消息,把能接收到的消息全部发送给绑定在自己身上的队列,因为广播不需要思考,所以扇形交换机处理消息的速度也是所有交换机类型里面最快的
2.直连交换机
将消息推送到binding key与该消息的routing key相同的队列
直连交换机x上绑定了两个队列,第一个列队绑定了绑定键orange,第二个队列有两个绑定键:black和green。在这种情况下,一个消息在布时指定了路由键为orange将会只被路由到列队q1,路由键为black和green的消息都将被路由到队列q2,其他的消息都将被丢失
3.主题交换机
二、交换机代码
1.直连交换机
①创建配置类DirectConfig(创建交换机,队列,进行绑定)
package com.example.provider.mq;import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;/*** @author 小宝的宝*/
@Configuration
@SuppressWarnings("all")
public class DirectConfig {/*** 创建队列* @return*/@Beanpublic Queue directQueueA(){return new Queue("directQueueA",true);}@Beanpublic Queue directQueueB(){return new Queue("directQueueB",true);}@Beanpublic Queue directQueueC(){return new Queue("directQueueC",true);}/*** 创建交换机*/@Beanpublic DirectExchange directExchange(){return new DirectExchange("directExchange");}@Beanpublic Binding bindingA(){return BindingBuilder.bind(directQueueA()).to(directExchange()).with("AA");}@Beanpublic Binding bindingB(){return BindingBuilder.bind(directQueueB()).to(directExchange()).with("BB");}@Beanpublic Binding bindingC(){return BindingBuilder.bind(directQueueC()).to(directExchange()).with("CC");}
}
②创建controller层模拟发送消息
package com.example.provider;import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author 小宝的宝*/
@RestController
public class providerController {@Autowiredprivate RabbitTemplate rabbitTemplate;@RequestMapping("/sendDirect")public String sendDirect(String routingKey){rabbitTemplate.convertAndSend("directExchange",routingKey,"Hello");return "yes";}
}
③运行结果 出现三个列队
④在消费者中创建接收者类,需要一直运行,表示一直处于接收状态
分别监听队列ABC
⑤启动消费者就可以接收消息啦
2.主题交换机
①创建主题交换机的配置类
定义key的规则并与交换机进行绑定
package com.example.provider.mq;import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @author 小宝的宝*/
@Configuration
@SuppressWarnings("all")
public class TopicConfig {public final static String KEY_A="*.black.*";public final static String KEY_B="*.ribbit.#";public final static String KEY_C="#.lazy";/*** 创建队列* @return*/@Beanpublic Queue topicQueueA(){return new Queue("topicQueueA",true);}@Beanpublic Queue topicQueueB(){return new Queue("topicQueueB",true);}@Beanpublic Queue topicQueueC(){return new Queue("topicQueueC",true);}/*** 创建交换机*/@Beanpublic TopicExchange topicExchange(){return new TopicExchange("topicExchange");}@Beanpublic Binding topicbindingA(){return BindingBuilder.bind(topicQueueA()).to(topicExchange()).with(KEY_A);}@Beanpublic Binding topicbindingB(){return BindingBuilder.bind(topicQueueB()).to(topicExchange()).with(KEY_B);}@Beanpublic Binding topicbindingC(){return BindingBuilder.bind(topicQueueC()).to(topicExchange()).with(KEY_C);}
}
②在消费者中创建主题队列的接收者并监听队列
③在controller层中定义发消息的方法
@RequestMapping("/sendTopic") public String sendTopic(String routingKey){rabbitTemplate.convertAndSend("topicExchange",routingKey,"Hello");return "yes"; }
④运行方法
3.扇形交换机
交换机绑定队列时不需要绑定键
①.配置类(新建队列和交换机并将他们绑定)
package com.example.provider.mq;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;/*** @author 小宝的宝*/
@Configuration
@SuppressWarnings("all")
public class FanoutConfig {/*** 创建队列* @return*/@Beanpublic Queue fanoutQueueA(){return new Queue("fanoutQueueA",true);}@Beanpublic Queue fanoutQueueB(){return new Queue("fanoutQueueB",true);}@Beanpublic Queue fanoutQueueC(){return new Queue("fanoutQueueC",true);}/*** 创建交换机*/@Beanpublic FanoutExchange fanoutExchange(){return new FanoutExchange("fanoutExchange");}@Beanpublic Binding fanoutbindingA(){return BindingBuilder.bind(fanoutQueueA()).to(fanoutExchange());}@Beanpublic Binding fanoutbindingB(){return BindingBuilder.bind(fanoutQueueB()).to(fanoutExchange());}@Beanpublic Binding fanoutbindingC(){return BindingBuilder.bind(fanoutQueueC()).to(fanoutExchange());}
}
注意:交换机与列队绑定时,方法名不能与其他的重复
②controller层方法
@RequestMapping("/sendFanout") public String sendFanout(){rabbitTemplate.convertAndSend("fanoutExchange",null,"Hello");return "yes"; }
③重新启动运行controller方法
可看到与三个队列绑定
④消费者模块创建接收类
⑤启动消费者
接收到消息
RabbitMQ交换机的讲解相关推荐
- (需求实战_进阶_02)SSM集成RabbitMQ 关键代码讲解、开发、测试
接上一篇:(企业内部需求实战_进阶_01)SSM集成RabbitMQ 关键代码讲解.开发.测试 https://gblfy.blog.csdn.net/article/details/10419730 ...
- 认识RabbitMQ交换机模型
认识RabbitMQ交换机模型 原文:认识RabbitMQ交换机模型 前言 RabbitMQ是消息队列中间件(Message Queue Middleware)中一种,工作虽然有用到,但是却没有形成很 ...
- Rabbitmq交换机详解
rabbitmq交换机 1.作用: 接受生产者的消息,然后根据路由键routingKey把消息投递到跟交换机绑定的对应的队列上 2.属性 Name: 交换机的名称 Type: 交换机的类型,direc ...
- RabbitMQ交换机类型
RabbitMQ交换机类型 一.Direct Exchange(直连交换机) 二. Fanout Exchange(扇型交换机) 三.Topic Exchange(主题交换机) 四.Headers E ...
- RabbitMQ交换机(Fanout、Direct、Topic)三种模式详解
一. 交换机 1.1 Exchanges 1.1.1 Exchanges概念 RabbitMQ 消息传递模型的核心思想是: 生产者生产的消息从不会直接发送到队列.实际上,通常生产 者甚至都不知道这 ...
- RabbitMQ之交换机的讲解
一.交换机 1.Exchange 在RabbitMQ中,生产者发送消息不会直接将消息投递到队列中,而是先将消息投递到交换机中, 在由交换机转发到具体的队列, 队列再将消息以推送或者拉取方式给消费者进行 ...
- #rabbitMQ #重复消费 #可靠投递 #延时投递 #rabbitMQ交换机类型#重复消费#消息积压#消息丢失
exchange类型: 1, direct 指定direct后, 消息会根据你设置的routeing key(路由键), 发送到对应的队列中 1,新建direct交换机 2,添加队列, 并且绑定路由键 ...
- RabbitMQ交换机简介
介绍 RabbitMQ消息传递模型的核心思想是:生产者生产的消息从不会直接发送到队列.实际上,通常生产者甚至都不知道这些消息传递传递到了哪些队列中. 相反,生产者只能将消息发送到交换机(exchang ...
- (需求实战_进阶_01)SSM集成RabbitMQ 关键代码讲解、开发、测试
背景: 为了减轻服务器的压力,现在原有项目的基础上集成消息队列来异步处理消息! 此项目是企业真实需求,项目的代码属于线上生产代码,直接用于生产即可! 此项目采用MQ发送消息模式为:路由模式,如果对Ra ...
- 消息中间件Rabbitmq核心概念讲解
概述 Rabbitmq是消息中间件的一种落地开源实现,使用Erlang语言编写,基于AMQP消息协议. 核心概念 Message:消息是不具名的,由消息头和消息体组成,消息体是不透明的,也就是可以设置 ...
最新文章
- LeetCode实战:合并两个有序数组
- oracle net manager没有orcl_Oracle-数据库基础知识
- 中国平安:杀进智能合约,你怕不怕?
- PVANET: Deep but Lightweight Neural Networks for Real-time Object Detection
- BugKuCTF WEB 管理员系统
- 最新解决ora-01034:oracle not available 的方法
- docker入门与实践之【05-Dockfile指令】
- 《销售从被拒绝开始》叶冠1
- Github|类别不平衡学习资源(下)
- 【Matlab】模式识别——聚类算法集锦
- python适合自学编程吗-孩子学编程选Scratch还是Python
- iOS10 Xcode 8 中provisioning file 相关bug
- java时间往后一天_往后余生,不能再陪你了
- mysql的tps是什么意思_Mysql数据库的QPS和TPS的意义和计算方法
- 看看阿里双十一970P数据处理得,那叫一个牛啤!
- Github项目:AI消除马赛克实战
- CCF201509-1数列分段(C语言)
- VirtualBox安装虚拟机全过程
- CISCO APIC-M2无法安装APIC软件故障解决
- MacOS下Git与GitHub
热门文章
- 第一章 教育基础(06 小学课程)
- 【K8S】Submariner实现跨集群通信
- 猴子摘香蕉问题python_[转载]猴子摘香蕉问题的状态空间表示法
- CentOS8 Docker 端口映射
- 《凤凰架构》读后感 - 演进中的架构
- 什么是增量绩效管理?华为是如何做
- Kubeadm部署单Master节点
- matlab将声音和噪声叠加,基于MATLAB有噪声语音信号的处理(最终稿)最新版
- UE4 虚幻 常用的流程控制蓝图节点介绍
- c语言dt2文件,JETSON NANO 2G使用笔记2-查看配置信息 第一个C程序 配置VNC 文件管理SCP...