RabbitMQ之交换机
目录
一、RabbitMQ交换机
1、交换机的由来
2、交换机类型
2.1直连交换机(Direct Exchange)
2.2主题交换机(Topic Exchange)
2.3扇形交换机(Fanout Exchange)
2.4首部交换机(Headers Exchange)
2.5默认交换机(Default Exchange)
二、RabbitMQ交换机实例讲解
一、直连交换机讲解
1、先在生产者中创建一个直连交换机配置类
2、之后创建一个控制类,用来发信息
3、在消费者中定义好接受者
二、主题交换机讲解
1、先在生产者中创建一个直连交换机配置类
2、之后创建一个控制类,用来发信息
3、在消费者中定义好接受者:
三、扇形(广播)交换机讲解
1、先在生产者中创建一个直连交换机配置类
2、之后创建一个控制类,用来发信息
3、在消费者中定义好接受者:
一、RabbitMQ交换机
1、交换机的由来
在RabbitMQ中,生产者发送信息不会直接将消息投递到队列中,而是将消息投递到交换机中,再由交换机转发到具体的队列中,队列再将消息以推送或者拉取方式给消费进行消费
在交换机诞生了两个概念
1、路由键:
2、绑定键:
3、两者中的关系
2、交换机类型
2.1直连交换机(Direct Exchange)
如图所示:
2.2主题交换机(Topic Exchange)
2.3扇形交换机(Fanout Exchange)
2.4首部交换机(Headers Exchange)
2.5默认交换机(Default Exchange)
二、RabbitMQ交换机实例讲解
一、直连交换机讲解
1、先在生产者中创建一个直连交换机配置类
DirectQueueConfig:生成队列,交换机,以及路由键,定义三个队列
package com.zj.provider;import lombok.With;
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;@Configuration
@SuppressWarnings("all")
public class DirectQueueConfig {/*** 生成一个队列* @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");}}
2、之后创建一个控制类,用来发信息
DirectController:其中rabbitTemplate用来发送信息辅助类
package com.zj.provider;import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.Map;@RestController
@RequestMapping("/sendDirect")
@SuppressWarnings("all")
public class DirectController {@Autowiredprivate RabbitTemplate rabbitTemplate;@RequestMapping("/sendDirect")public String sendDirect(String routerKey) {rabbitTemplate.convertAndSend("directExchange", routerKey, "Hello world");return "yes";}
}
3、在消费者中定义好接受者
DirectReciverA:再生成连个同样的类但是要注意的是必须要打@RabbitHandler和@RabbitListener(queues = "directQueueA")第一个是对队列处理者,第二个是队列的监听者,监听队列,不加第一个注解,消息将会接收不到
package com.zj.consumer.mq;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;@Component
@SuppressWarnings("all")
@Slf4j
@RabbitListener(queues = "directQueueA")
public class DirectReciverA {@RabbitHandlerpublic void process(String message){log.warn("A接收到了"+message);}}
结果运行成功:
二、主题交换机讲解
1、先在生产者中创建一个直连交换机配置类
TopicQueueConfig:注意:这里面需要特定指定键
注意:必须在绑定键前加一个Topic来区分,必须介以区别,不然将会报错,因为加入了bean对象
package com.zj.provider.MQ;import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
@SuppressWarnings("all")
public class TopicQueueConfig {private final static String KEY_A="*.orange.*";private final static String KEY_B="*.*.rabbit";private 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);}}
2、之后创建一个控制类,用来发信息
@RequestMapping("/sendTopic")public String sendTopic(String routerKey) {rabbitTemplate.convertAndSend("topicExchange", routerKey, "Hello world");return "yes";}
3、在消费者中定义好接受者:
package com.zj.consumer.mq;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;@Component
@SuppressWarnings("all")
@Slf4j
@RabbitListener(queues = "topicQueueA")
public class TopicReciverA {@RabbitHandlerpublic void process(String message){log.info("A接收到了"+message);}}
注意:需要进行发信息才能在RabbitMQ发现队列
显示出队列:
接收成功:
三、扇形(广播)交换机讲解
扇形交换机和其他两个交换机不一样,扇形交换机不用绑定键,因为他会进行广播,同样的在队列与交换机进行绑定时,需要加上不同的名字来进行区分
1、先在生产者中创建一个直连交换机配置类
package com.zj.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;@Configuration
@SuppressWarnings("all")
public class FanoutQueueConfig {/*** 生成一个队列* @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());}}
2、之后创建一个控制类,用来发信息
没有绑定键,但是要写空值,不然fanoutExchange会被认为是路由键 @RequestMapping("/sendFanout") public String sendFanout() {rabbitTemplate.convertAndSend("fanoutExchange", "null" ,"Hello world");return "yes"; }
3、在消费者中定义好接受者:
package com.zj.consumer.mq;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;@Component
@SuppressWarnings("all")
@Slf4j
@RabbitListener(queues = "fanoutQueueA")
public class FanoutReciverA {@RabbitHandlerpublic void process(String message){log.info("A接收到了"+message);}}
生产者运行效果:
消费者接收到信息
今天的知识就分享到这了,希望能够帮助到你!
RabbitMQ之交换机相关推荐
- RabbitMQ的交换机类型和工作模式
RabbitMQ的交换机类型有四种 1.direct 直流交换机: 根据消息的路由键routingkey,将消息以完全匹配的方式路由到指定的队列中. 这里的匹配指的是消息本身携带的路由键和队列与交换机 ...
- RabbitMQ之交换机的四种类型和属性
交换机主要包括如下4种类型: Direct exchange(直连交换机) Fanout exchange(扇型交换机) Topic exchange(主题交换机) Headers exchange( ...
- RabbitMQ持久化交换机队列
持久化 将交换机或队列的数据保存到磁盘 服务器宕机或重启之后依然存在 读写速度比较慢 非持久化 将交换机或队列的数据保存到内存 服务器宕机或重启之后将不存在 读写速度比较快 配置方式 <!-- ...
- 003 Rabbitmq中交换机的类型
一 注意点 在Rabbitmq之中,存在绑定键和路由键的概念. [1]绑定键 : 交换机和队列关系的一种描述. [2]路由键: 消息之中消息标签的内容,描述了消息最终到达哪些队列之中. 在Rabbit ...
- RabbitMQ exchange交换机机制
目录 RabbitMQ 概念 exchange交换机机制 什么是交换机 binding? Direct Exchange交换机 Topic Exchange交换机 Fanout Exchange交换机 ...
- RabbitMQ中交换机的几种模式
目录 简述 交换机模式 Fanout模式 Direct模式 Topic模式 Headers模式 简述 生产者不直接跟队列打交道,而是通过交换机.交换机类似于生产者和队列直接的一个管理者,它将生产的消息 ...
- RabbitMQ之交换机的讲解
一.交换机 1.Exchange 在RabbitMQ中,生产者发送消息不会直接将消息投递到队列中,而是先将消息投递到交换机中, 在由交换机转发到具体的队列, 队列再将消息以推送或者拉取方式给消费者进行 ...
- RabbitMQ Topic交换机(结果成功)
public class EmitLogTopic {//交换机的名称public static final String EXCHANGE_NAME = "topic_logs" ...
- RabbitMQ Topic交换机(生产者)
/* * 声明主题交换机及相关队列 * 消费者C1 * */ public class ReceiveLogsTopic01 {//交换机名称public static final String EX ...
最新文章
- leetcode算法第三题
- java 二进制,八进制,十进制,十六进制间相互转换的方法
- mysql命令行导入和导出数据
- sun.misc.BASE64Encoder 不建议使用java.sun自带包中的内容
- Oracle数据库表中字段顺序的修改方法
- continue后面的语句还执行吗_循环结构辅助控制语句
- 在Release版本中如何关闭Debug版本中的log
- ArcView GIS 应用与开发技术(9)- 创建空间数据
- fcm算法matlab实现,fcm算法matlab
- 2023养老展/山东养老服务业展/济南老年用品展/老龄产业展
- aliases节点解析
- 华为机试_HJ24 合唱队【中等】【收藏】
- 记一次 关于Android studio 编译报错compileDebugJavaWithJavac FAILED
- 如何快速搭建个人博客网站(详解)
- java getiotype_坑爹微信之读取PKCS12流时出现的java.io.IOException: DerInputStream.getLength...
- 将html页面中部分div 导出为word ,纯前端处理,解决word导出视图 问题
- python爬取今日头条后台数据_爬虫爬取今日头条数据代码实现
- 做数据分析的女孩子,职业发展前景在哪里?数据分析枯燥吗?
- 常见的个人电脑入侵方式
- JQuery前后端分离
热门文章
- three.js和D3.js
- 企业级监控平台,监控系统选型
- 2022新版彩虹易支付系统源码/运营版/支持当面付/通道轮询/16支付插件/免签约支付系统
- matlab 实现低通巴特沃斯滤波器、切比雪夫1型/2型滤波器 和 椭圆滤波器
- Golang源码探索----GC的实现原理(6)
- 大数据与AI时代,企业爆发之道?Tesra超算网络保驾护航!
- 深度学习(二)---算法岗面试题
- 原根(知识学习+板子总结+例题+应用)
- vscode cshtml 智能提示
- 第一章 FANUC数控机床采集方案