02、RabbitMQ交换机

news/2024/10/17 20:26:58/

目录

1.、Exchange(交换机)的作用

 2、Exchange(交换机)的类型

2.1.直连交换机:Direct Exchange

2.2.主题交换机:Topic Exchange

2.3.扇形交换机:Fanout Exchange

2.4.首部交换机:Headers exchange

2.5.默认交换机

2.6.Dead Letter Exchange(死信交换机)

3、交换机的属性

4、综合案例:交换机的使用

给子模块添加依赖

4.1.直连交换机(Direct Exchange)

4.2.主题交换机(Topic Exchange)

4.3.扇形交换机(Fanout Exchange)

以上就是今天的分享了


1.、Exchange(交换机)的作用

在RabbitMQ中,生产者发送消息不会直接将消息投递到队列中,而是先将消息投递到交换机中,在由交换机转发到具体的队列,队列再将消息以推送或者拉取方式给消费者进行消费

                        创建消息                               路由键                                         pull/push

生产者------------------------------->交换机--------------------------------->队列--------------------------->消费者

 2、Exchange(交换机)的类型

2.1.直连交换机:Direct Exchange

直连交换机是一种带路由功能的交换机,一个队列会和一个交换机绑定,除此之外再绑定一个routing_key,当消息被发送的时候,需要指定一个binding_key,这个消息被送达交换机的时候,就会被这个交换机送到指定的队列里面去。同样的一个binding_key也是支持应用到多个队列中的。这样当一个交换机绑定多个队列,就会被送到对应的队列去处理。

注1:什么是路由键 
          每个消息都有一个称为路由键(routing key)的属性,它其实就是一个简单的字符串

注2:直连交换机适用场景
          有优先级的任务,根据任务的优先级把消息发送到对应的队列,这样可以指派更多的资源去处理高优先级的队列。

2.2.主题交换机:Topic Exchange

直连交换机的缺点!
     直连交换机的routing_key方案非常简单,如果我们希望一条消息发送给多个队列,那么这个交换机需要绑定上非常多的routing_key,
     假设每个交换机上都绑定一堆的routing_key连接到各个队列上。那么消息的管理就会异常地困难。所以RabbitMQ提供了一种主题交换机,发送到主题交换机上的消息需要携带指定规则的routing_key,
     主题交换机会根据这个规则将数据发送到对应的(多个)队列上。

     主题交换机的routing_key需要有一定的规则,交换机和队列的binding_key需要采用*.#.*.....的格式,每个部分用.分开,其中
     *表示一个单词 
     #表示任意数量(零个或多个)单词。

示例:
     队列Q1绑定键为 *.TT.*
     队列Q2绑定键为TT.#

     如果一条消息携带的路由键为 A.TT.B,那么队列Q1将会收到 
     如果一条消息携带的路由键为TT.AA.BB,那么队列Q2将会收到

2.3.扇形交换机:Fanout Exchange

扇形交换机是最基本的交换机类型,它所能做的事情非常简单———广播消息。
扇形交换机会把能接收到的消息全部发送给绑定在自己身上的队列。因为广播不需要“思考”,所以扇形交换机处理消息的速度也是所有的交换机类型里面最快的。 

这个交换机没有路由键概念,就算你绑了路由键也是无视的。 

2.4.首部交换机:Headers exchange

2.5.默认交换机

   实际上是一个由RabbitMQ预先声明好的名字为空字符串的直连交换机(direct exchange)。它有一个特殊的属性使得它对于简单应用特别有用处:那就是每个新建队列(queue)都会自动绑定到默认交换机上,绑定的路由键(routing key)名称与队列名称相同。

     如:当你声明了一个名为”hello”的队列,RabbitMQ会自动将其绑定到默认交换机上,绑定(binding)的路由键名称也是为”hello”。
     因此,当携带着名为”hello”的路由键的消息被发送到默认交换机的时候,此消息会被默认交换机路由至名为”hello”的队列中

类似amq.*的名称的交换机:
     这些是RabbitMQ默认创建的交换机。这些队列名称被预留做RabbitMQ内部使用,不能被应用使用,否则抛出403 (ACCESS_REFUSED)错误 

2.6.Dead Letter Exchange(死信交换机)

在默认情况,如果消息在投递到交换机时,交换机发现此消息没有匹配的队列,则这个消息将被悄悄丢弃。
为了解决这个问题,RabbitMQ中有一种交换机叫死信交换机。当消费者不能处理接收到的消息时,将这个消息重新发布到另外一个队列中,等待重试或者人工干预。这个过程中的exchange和queue就是所谓的”Dead Letter Exchange 和 Queue

3、交换机的属性

 除交换机类型外,在声明交换机时还可以附带许多其他的属性,其中最重要的几个分别是:
   Name:交换机名称
   Durability:是否持久化。如果持久性,则RabbitMQ重启后,交换机还存在
   Auto-delete:当所有与之绑定的消息队列都完成了对此交换机的使用后,删掉它
   Arguments:扩展参数

4、综合案例:交换机的使用

     rabbitmq02                     #主模块    maven
     rabbitmq-provider            #生产者   springboot
     rabbitmq-consumer            #消费者   springboot

给子模块添加依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency>

4.1.直连交换机(Direct Exchange)

1.RabbitmqDirectConfig
2.SendMessageController
         关键代码:rabbitTemplate.convertAndSend("testDirectExchange", "testDirectRouting", map);
                   sendDirectMessage:进行消息推送(也可以改为定时任务,具体看需求)
         
3.查看rabbitmq管理界面
       我们目前还创建消费者rabbitmq-consumer,消息没有被消费的,我们去rabbitMq管理页面看看,是否推送成功 
       Overview选项卡,可以查看到刚刚创建的消息
4.创建消息接收监听类DirectReceiver

注1:新版jdk日期及格式化
          LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

注2:rabbitTemplate和amqpTemplate有什么关系
          查看源码中会发现rabbitTemplate实现自amqpTemplate接口,两者使用起来并无区别,功能一致

注3:不要@Configuration写成了@Configurable,这两个长得很像
          @Configuration该注解是可以用来替代XML文件。
          手动new出来的对象,正常情况下,Spring是无法依赖注入的,这个时候可以使用        

          @Configurable注解

  

 生产者

package com.zking.rabbitmqproduct.config;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
public class RabbitmqDirectConfig {/*** 定义队列* @return*/@Beanpublic Queue directQueue(){return new Queue("direct-queue");}/*** 自定义直连交换机* @return*/@Beanpublic DirectExchange directExchange(){return new DirectExchange("direct-exchange",true,false);}/*** 将队列与交换机进行绑定,并设置路由键* @return*/@Beanpublic Binding binding(){return BindingBuilder.bind(directQueue()).to(directExchange()).with("direct_routing_key");}}

消费者

package com.zking.rabbitmqconsumer.receiver;import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;import java.util.Map;@Component
@RabbitListener(queues = {"direct-queue"})
public class DirectReceiver {@RabbitHandlerpublic void handler(Map<String,Object> json){System.out.println(json);}
}

4.2.主题交换机(Topic Exchange)

RabbitTopicConfig

生产者

package com.zking.rabbitmqproduct.config;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
public class RabbitmqTopicConfig {@Beanpublic Queue queueA(){return new Queue("queue-a");}@Beanpublic Queue queueB(){return new Queue("queue-b");}@Beanpublic Queue queueC(){return new Queue("queue-c");}/*** 定义主题交换机* @return*/@Beanpublic TopicExchange topicExchange(){return new TopicExchange("topic-exchange",true,false);}@Beanpublic Binding bindingA(){return BindingBuilder.bind(queueA()).to(topicExchange()).with("topic.person.aa");}@Beanpublic Binding bindingB(){return BindingBuilder.bind(queueB()).to(topicExchange()).with("topic.person.bb");}@Beanpublic Binding bindingC(){return BindingBuilder.bind(queueC()).to(topicExchange()).with("topic.person.*");}
}

消费者 

package com.zking.rabbitmqconsumer.receiver;import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;import java.util.Map;@Component
public class TopicReceiver {@RabbitListener(queues = {"queue-a"})@RabbitHandlerpublic void handlerA(Map<String,Object> json){System.out.println("已接受到队列queue-a传递过来的消息:"+json);}@RabbitListener(queues = {"queue-b"})@RabbitHandlerpublic void handlerB(Map<String,Object> json){System.out.println("已接受到队列queue-b传递过来的消息:"+json);}@RabbitListener(queues = {"queue-c"})@RabbitHandlerpublic void handlerC(Map<String,Object> json){System.out.println("已接受到队列queue-c传递过来的消息:"+json);}
}

4.3.扇形交换机(Fanout Exchange)

//因为是扇型交换机, 路由键无需配置,配置也不起作用,两处地方均未配置路由键BindingBuilder.bind(queueA()).to(fanoutExchange()); rabbitTemplate.convertAndSend(RabbitFanoutConfig.EXCHANGE_NAME,null, map);

生产者

package com.zking.rabbitmqproduct.config;import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class RabbitmqFanoutConfig {@Beanpublic Queue queueX(){return new Queue("queue-x");}@Beanpublic Queue queueY(){return new Queue("queue-y");}@Beanpublic Queue queueZ(){return new Queue("queue-z");}/*** 定义扇形交换机,与路由键无关* @return*/@Beanpublic FanoutExchange fanoutExchange(){return new FanoutExchange("fanout-exchange",true,false);}@Beanpublic Binding bindingX(){return BindingBuilder.bind(queueX()).to(fanoutExchange());}@Beanpublic Binding bindingY(){return BindingBuilder.bind(queueY()).to(fanoutExchange());}@Beanpublic Binding bindingZ(){return BindingBuilder.bind(queueZ()).to(fanoutExchange());}
}

消费者

package com.zking.rabbitmqconsumer.receiver;import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;import java.util.Map;@Component
public class FanoutReceiver {@RabbitListener(queues = {"queue-x"})@RabbitHandlerpublic void handlerY(Map<String,Object> json){System.out.println("已接受到队列queue-x传递过来的消息:"+json);}@RabbitListener(queues = {"queue-y"})@RabbitHandlerpublic void handlerX(Map<String,Object> json){System.out.println("已接受到队列queue-y传递过来的消息:"+json);}@RabbitListener(queues = {"queue-z"})@RabbitHandlerpublic void handlerZ(Map<String,Object> json){System.out.println("已接受到队列queue-z传递过来的消息:"+json);}
}

以上就是今天的分享了


http://www.ppmy.cn/news/131980.html

相关文章

华为3COM交换机配置命令详解

1、配置文件相关命令[Quidway]display current-configuration ;显示当前生效的配置[Quidway]display saved-configuration &#xff1b;显示flash中配置文件&#xff0c;即下次上电启动时所用的配置文件<Quidway>reset saved-configuration …

能播放任何格式视频的手机视频播放器

手机上如何播放任何格式的视频&#xff0c;手机上默认的视频播放器一般支持的视频格式较少&#xff0c;有时候我们下载了视频&#xff0c;但会视频格式在手机上无法播放的情况&#xff0c;那如何能够在手机上播放任意格式的视频呢&#xff1f; 首先我们在手机上安装“王者剪辑…

ios系统下h264编码的mp4文件无法播放的问题

问题描述&#xff1a;ios系统下&#xff0c;部分h264编码的mp4文件在浏览器中无法播放&#xff0c;而另外一些h264编码的mp4文件正常&#xff0c;同时在安卓系统下全部正常。 问题分析&#xff1a;H.264中有profile&#xff08;画质级别&#xff09;和level&#xff08;用来约…

视频格式怎么改为mp4?有什么好用的视频格式转换软件

视频格式怎么改为mp4&#xff1f;视频给我们的生活、工作带来的影响越来越大&#xff0c;其中mp4格式是我们见过最多的视频格式了&#xff0c;如果遇到不常见的视频格式无法播放的话&#xff0c;可以将其转换为常见的mp4格式&#xff0c;如果你还不知道视频转换方法&#xff0c…

mp4视频损坏无法播放如何修复?

对于我们平时使用到的MP4视频文件&#xff0c;有时候在播放时会遇到文件损坏&#xff0c;无法正常打开&#xff0c;针对这个问题&#xff0c;如何修复损坏的MP4视频&#xff1f; 可以使用牛学长文件修复工具&#xff0c;这是一款专业的视频修复软件&#xff0c;可以修复格式更…

QMediaPlayer 播放视频(MP4、MP3...)

.pro中 QT multimedia QT multimediawidgets常用类&#xff1a; #include <QVideoWidget> #include <QMediaPlayer> #include <QMediaPlaylist>具体使用&#xff1a; QMediaPlayer *m_pQMediaPlayer; QVideoWidget *m_pQVideoWidget; QMedi…

视频mp4与m4v格式区别

一、M4V格式 M4V 是一个标准视频文件格式&#xff0c;由苹果公司创造。此种格式为 iPod 、 iPhone 和 PlayStation Portable 所使用&#xff0c;同时此格式基于 MPEG-4 编码第二版。其视频编码采用H264或H264/AVC&#xff0c;音频编码采用AAC。采用H264高清编码&#xff0c;相比…

m3u8转mp4,不用格式软件

m3u8转mp4&#xff0c;不用格式软件 m3u8视频格式创建bat脚本文件简化代码输入运行脚本文件 m3u8视频格式 手机端QQ浏览器自带视频下载插件&#xff0c;有的网站需要登录才能下载视频&#xff0c;在QQ浏览器中不需要登录就可以下载了&#xff0c;但有时下载下来的格式是m3u8&a…