RabbitMQ主要有六种种工作模式,本文整合SpringBoot分别介绍工作模式的实现。

前提概念

生产者

消息生产者或者发送者,使用P表示:

队列

消息从生产端发送到消费端,一定要通过队列转发,使用queue_name表示:

消费者

消费的消费者或者接收者,使用C表示,如果有多个消费者也可以用C1、C2表示:

SpringBoot整合RabbitMQ基本配置

  1. 添加maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
  1. 添加application.yml 配置
spring:
rabbitmq:
host: 192.168.3.19
port: 5672
username: admin
password: 123456
  1. 消息生产

生产端发送消息,调用RabbitTemplate发送消息,比如:

@Autowired
private RabbitTemplate rabbitTemplate; public String send() {
rabbitTemplate.convertAndSend("routingKey","send message");
}
  1. 消费消息

消费消息使用队列监听注解@RabbitListener,添加队列名称就能消费发送到队列上的消息了:

@RabbitListener(queuesToDeclare = @Queue("queue_name"))
public void consume(String message) {
// 接收消息
}

1. 简单(simple)模式

最简单的消息发送

特点

  • 生产者是消费者是一一对应,也叫做点对点模式,生产者发送消息经过队列直接发送给消费者。
  • 生产者和消费者在发送和接收消息时,只需要指定队列名称,而不需要指定Exchange 交换机。

代码示例

生产消息:

@GetMapping("/simple-send")
public String simpleSend() {
rabbitTemplate.convertAndSend("simple","this is news");
return "ok";
}

消费消息

@RabbitListener(queuesToDeclare = @Queue("simple"))
public void consume(String message) {
System.out.println(message);
}

输出:

this is news

无需创建交换机和绑定队列,只需要匹配发送端和消费端的队列名称就能成功发送消息。

2. 工作模式

在多个消费者之间分配任务

特点

  • 工作模式和简单模式差不多,只需要生产端、消费端、队列。
  • 不同在于一个生产者、一个队列对应多个消费者,也就是一对多的关系。
  • 在多个消费者之间分配消息(竞争消费者模式),类似轮询发送消息,每个消息都只发给一个消费者。

代码示例

  • 生产消息:
@GetMapping("/work-send")
public String simpleSend() {
rabbitTemplate.convertAndSend("work","this is news");
return "ok";
}
  • 消费消息:
@RabbitListener(queuesToDeclare = @Queue("work"))
public void consume(String message) {
System.out.println("first:" + message);
} @RabbitListener(queuesToDeclare = @Queue("work"))
public void consumeSecond(String message) {
System.out.println("second:" + message);
}

创建一个生产者,两个消费者,发送两条消息,两个消费者分别接收到消息,输出:

first:this is news
second:this is news

两个消费者,轮流消费消息。类似nginx负载均衡。

3. 发布订阅模式

一次向多个消费者发送消息

特点

  • 发布订阅类似广播消息,每个消息可以同时发送给订阅该消息的消费者,
  • 上图中的X表示交换机,使用的扇形交换机(fanout),它将发送的消息发送到所有绑定交换机的队列。

代码示例

  • 创建队列、交换机以及绑定:
@Bean
public FanoutExchange fanoutExchange() {
return new FanoutExchange("PUBLISH_SUBSCRIBE_EXCHANGE");
} @Bean
public Queue psFirstQueue() {
return new Queue("psFirstQueue");
} @Bean
public Queue psSecondQueue() {
return new Queue("psSecondQueue");
} @Bean
public Queue psThirdQueue() {
return new Queue("psThirdQueue");
} @Bean
public Binding routingFirstBinding() {
return BindingBuilder.bind(psFirstQueue()).to(fanoutExchange());
} @Bean
public Binding routingSecondBinding() {
return BindingBuilder.bind(psSecondQueue()).to(fanoutExchange());
} @Bean
public Binding routingThirdBinding() {
return BindingBuilder.bind(psThirdQueue()).to(fanoutExchange());
}
  • 上面定义一个交换机fanoutExchange。
  • 分别绑定三个队列psFirstQueue、psSecondQueue、psThirdQueue。
  • 队列绑定交换机不需要routingKey,直接绑定即可。

  • 生产端:
@GetMapping("/publish-sub-send")
public String publishSubSend() {
rabbitTemplate.convertAndSend("PUBLISH_SUBSCRIBE_EXCHANGE", null, "publish/subscribe hello");
return "ok";
}

无需指定routingKey,设置为null。

  • 消费端:
@RabbitListener(queues = "psFirstQueue")
public void pubsubQueueFirst(String message) {
System.out.println("【first】:" + message);
} @RabbitListener(queues = "psSecondQueue")
public void pubsubQueueSecond(String message) {
System.out.println("【second】:" + message);
} @RabbitListener(queues = "psThirdQueue")
public void pubsubQueueThird(String message) {
System.out.println("【third】:" + message);
}
  • 输出:
【first】: publish/subscribe hello
【second】: publish/subscribe hello
【third】: publish/subscribe hello

发送一条消息,绑定的队列都能接收到消息。

4. 路由模式

根据routingKey有选择性的接收消息

特点

  • 每个队列根据不同routingKey绑定交换机
  • 消息发送到交换机后通过routingKey发送给特定的队列,然后传到消费者消费。
  • 交换由扇形交换机(fanout)改成直连交换机(direct)。

代码示例

  • 创建队列、交换机以及绑定:
@Bean
public Queue routingFirstQueue() {
return new Queue("routingFirstQueue");
} @Bean
public Queue routingSecondQueue() {
return new Queue("routingSecondQueue");
} @Bean
public Queue routingThirdQueue() {
return new Queue("routingThirdQueue");
} @Bean
public DirectExchange routingExchange() {
return new DirectExchange("routingExchange");
} @Bean
public Binding routingFirstBind() {
return BindingBuilder.bind(routingFirstQueue()).to(routingExchange()).with("firstRouting");
} @Bean
public Binding routingSecondBind() {
return BindingBuilder.bind(routingSecondQueue()).to(routingExchange()).with("secondRouting");
} @Bean
public Binding routingThirdBind() {
return BindingBuilder.bind(routingThirdQueue()).to(routingExchange()).with("thirdRouting");
}
  • 创建一个交换机,根据不同的路由规则匹配不同的队列routingExchange,根据不同的routingKey绑定不同的队列:
  • firstRouting路由键绑定routingFirstQueue队列。
  • secondRouting路由键绑定routingSecondQueue队列。
  • thirdRouting路由键绑定routingThirdQueue队列。

  • 生产消息:
@GetMapping("/routing-first")
public String routingFirst() {
// 使用不同的routingKey 转发到不同的队列
rabbitTemplate.convertAndSend("routingExchange","firstRouting"," first routing message");
rabbitTemplate.convertAndSend("routingExchange","secondRouting"," second routing message");
rabbitTemplate.convertAndSend("routingExchange","thirdRouting"," third routing message");
return "ok";
}
  • 消费消息:
@RabbitListener(queues = "routingFirstQueue")
public void routingFirstListener(String message) {
System.out.println("【routing first】" + message);
} @RabbitListener(queues = "routingSecondQueue")
public void routingSecondListener(String message) {
System.out.println("【routing second】" + message);
} @RabbitListener(queues = "routingThirdQueue")
public void routingThirdListener(String message) {
System.out.println("【routing third】" + message);
}

输出:

【routing first】first routing message
【routing second】second routing message
【routing third】third routing message

分析:

rabbitTemplate.convertAndSend("routingExchange","firstRouting"," first routing message");

消息从生产者指定firstRouting路由键,找到对应的绑定队列routingFirstQueue,就被routingFirstQueue队列消费了。

5. 主题模式

基于某个主题接收消息

特点

路由模式发送的消息,是需要指定固定的routingKey,如果想要针对一类路由。

比如:

  • 只接收以.com 结尾的消息。
  • www.开头的消息。

主题模式就派上场了,路由模式和主题模式类似,路由模式是设置特定的routingKey绑定唯一的队列,而主题模式的是使用通配符匹配一个或者多个队列。

代码示例

  • 创建交换机和队列:
@Bean
public Queue topicFirstQueue() {
return new Queue("topicFirstQueue");
} @Bean
public Queue topicSecondQueue() {
return new Queue("topicSecondQueue");
} @Bean
public Queue topicThirdQueue() {
return new Queue("topicThirdQueue");
} @Bean
public TopicExchange topicExchange() {
return new TopicExchange("topicExchange");
}
  • 使用通配符绑定交换机和交换机:
@Bean
public Binding topicFirstBind() {
// .com 为结尾
return BindingBuilder.bind(topicFirstQueue()).to(topicExchange()).with("*.com");
} @Bean
public Binding topicSecondBind() {
// www.为开头
return BindingBuilder.bind(topicSecondQueue()).to(topicExchange()).with("www.#");
}

通配符有两种,*和#,

  • * 表示可以匹配一个。
  • # 表示可以匹配多个。

比如:

  • #.com表示接收多个以.com结尾的字段。

    • 例如: taobao.com、www.taobao.com、www.jd.com。
  • *.com表示接收一个以.com结尾的字段。

    • 例如: taobao.com、jd.com。
    • 多个字段是无法匹配的,比如www.taobao.com、cn.taobao.com。
  • www.#可以匹配多个以www开头的字段。

    • 例如www.taobao、www.jd。
  • www.*可以匹配一个以www开头的字段。

    • 例如:www.taobao、www.jd。
    • 多个字段是无法匹配的,比如www.taobao.com、www.jd.com。
  • 生产消息:

@GetMapping("/topic-first-send")
public String topicFirstSend() {
rabbitTemplate.convertAndSend("topicExchange","www.taobao.com","www.taobao.com");
rabbitTemplate.convertAndSend("topicExchange","taobao.com","taobao.com");
rabbitTemplate.convertAndSend("topicExchange","www.jd","www.jd");
return "topic ok";
}
  • 消费消息:
@RabbitListener(queues = "topicFirstQueue")
public void topicFirstListener(String message) {
System.out.println("【topic first】" + message);
} @RabbitListener(queues = "topicSecondQueue")
public void topicSecondListener(String message) {
System.out.println("【topic second】" + message);
}
  • 输出:
【topic second】www.taobao.com
【topic first】taobao.com
【topic second】www.jd

www.#可以匹配多个以www.开头的路由键,例如www.taobao.com、www.jd。而*.com只能匹配一个以.com结尾的路由键,例如taobao.com,而无法匹配www.taobao.com。

6. RPC模式

消息有返回值

特点

  • PRC模式和上面的几种模式唯一不同的点在于,该模式可以收到消费端的返回值。
  • 生成端接收消费端的返回值。

代码示例

  • 消费端添加返回值:
@RabbitListener(queuesToDeclare =@Queue("rpcQueue"))
public String rpcListener(String message) {
System.out.println("【rpc接收消息】" + message);
return "rpc 返回" + message;
}
  • 生产端发送消息:
@GetMapping("/rpc-send")
public void rpcSend() {
Object receive = rabbitTemplate.convertSendAndReceive("rpcQueue","rpc send message");
System.out.println("【发送消息消息】" + receive);
}
  • 输出:
【rpc接收消息】rpc send message
【发送端接收消息】rpc 返回rpc send message

交换机类型

上面的 订阅发布模式、路由模式以及主题模式使用到了不同的交换机,分别是:

  • 直连交换机 Direct
  • 扇形交换机 Fanout
  • 主题交换器 Topic

Direct Exchange(直连)

直连交换机被应用在路由模式下,该交换机需要通过特定的routingKey来绑定队列,交换机只有接收到了匹配的routingKey才会将消息转发到对应的队列中,否则就不会转发消息。

路由模式使用直连交换机,该模式下根据routingKey绑定特定的队列。

Fanout Exchange(扇形)

扇形交换机没有路由键的概念,只需将队列绑定在交换机上,发送到交换机上的消息会转发到交换机所以绑定的队列里面,类似广播,只要打开收音机都能接收到广播消息。扇形交换机应用于发布订阅模式。

Topic Exchange(主题)

主题模式是将路由键根据一个主题进行分类,和直连模式不同的是,直连模式绑定特定的路由键,而主题模式使用通配符绑定路由键,绑定键有两种:

  • * 表示可以匹配仅一个。
  • # 表示可以匹配零个或多个。

总结

整合SpringBoot实现RabbitMQ六种工作模式,并详细讲解RabbitMQ六种工作模式:

  • 简单模式

    • 无需创建交换机,匹配生产端和消费的routingKey即可。
  • 工作模式
    • 多个消费端公平竞争同一个消息。
  • 发布订阅模式
    • 一次向多个消费者发送消息。
  • 路由模式
    • 根据特定的路由键转发消息。
  • 主题模式
    • 根据通配符,匹配路由键转发消息。
  • RPC模式
    • 生产端接收消费端发送的返回值。

源码示例

参考

SpringBoot整合RabbitMQ实现六种工作模式的更多相关文章

  1. RabbitMQ的六种工作模式

    一.基于erlang语言: 是一种支持高并发的语言 RabbitMQ的六种工作模式: 1.1 simple简单模式 消息产生着§将消息放入队列 消息的消费者(consumer) 监听(while) 消 ...

  2. RabbitMQ的六种工作模式总结

    最近学习RabbitMQ的使用方式,记录下来,方便以后使用,也方便和大家共享,相互交流. RabbitMQ的六种工作模式: 1.Work queues2.Publish/subscribe3.Rout ...

  3. rabbitmq官方的六种工作模式

    1.RabbitMq1.1介绍RabbitMQ是一个消息代理:它接受并转发消息.你可以把它当成一个邮局:当你想邮寄信件的时候,你会把信件放在投递箱中,并确信邮递员最终会将信件送到收件人的手里.在这个例 ...

  4. springboot学习笔记-6 springboot整合RabbitMQ

    一 RabbitMQ的介绍 RabbitMQ是消息中间件的一种,消息中间件即分布式系统中完成消息的发送和接收的基础软件.这些软件有很多,包括ActiveMQ(apache公司的),RocketMQ(阿 ...

  5. 一篇学习完rabbitmq基础知识,springboot整合rabbitmq

    一   rabbitmq 介绍 MQ全称为Message Queue,即消息队列, RabbitMQ是由erlang语言开发,基于AMQP(Advanced MessageQueue 高级消息队列协议 ...

  6. 功能:SpringBoot整合rabbitmq,长篇幅超详细

    SpringBoot整合rabbitMq 一.介绍 消息队列(Message Queue)简称mq,本文将介绍SpringBoot整合rabbitmq的功能使用 队列是一种数据结构,就像排队一样,遵循 ...

  7. RabbitMQ入门到进阶(Spring整合RabbitMQ&SpringBoot整合RabbitMQ)

    1.MQ简介 MQ 全称为 Message Queue,是在消息的传输过程中保存消息的容器.多用于分布式系统 之间进行通信. 2.为什么要用 MQ 1.流量消峰 没使用MQ 使用了MQ 2.应用解耦 ...

  8. 【SpringBoot系列5】SpringBoot整合RabbitMQ

    前言: 因为项目需要用到RabbitMQ,前几天就看了看RabbitMQ的知识,记录下SpringBoot整合RabbitMQ的过程. 给出两个网址: RabbitMQ官方教程:http://www. ...

  9. SpringBoot系列八:SpringBoot整合消息服务(SpringBoot 整合 ActiveMQ、SpringBoot 整合 RabbitMQ、SpringBoot 整合 Kafka)

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合消息服务 2.具体内容 对于异步消息组件在实际的应用之中会有两类: · JMS:代表作就是 ...

随机推荐

  1. SQL多表多字段比对方法

    目录 表-表比较 整体思路 找出不同字段的明细 T1/T2两表ID相同的部分,是否存在不同NAME 两表的交集与差集:判断两表某些字段是否相同 两表的交集与差集:找出T2表独有的id 字段-字段比较 ...

  2. ElasticSearch基础学习(SpringBoot集成ES)

    一.概述 什么是ElasticSearch? ElasticSearch,简称为ES, ES是一个开源的高扩展的分布式全文搜索引擎. 它可以近乎实时的存储.检索数据:本身扩展性很好,可以扩展到上百台服 ...

  3. 6. ZigZag Conversion - LeetCode

    Question 6. ZigZag Conversion Solution 题目大意:将字符串按Z字型排列,然后再一行一行按字符输出 思路:按题目中的第一个例子,画出如下图,通过n的不同值,可以找出 ...

  4. unity---脚本创建文本

    脚本创建文本 新建文件夹 Resources 方便引用字体 在文件Resources中新建Fonts,并且下载一个ttf字体 没有字体,文本内容无法显示 脚本如下 public GameObject ...

  5. JavaScript中的??和?.和??=操作符

    JS中两种不常使用但挺实用的操作符:??和?. 一起来了解并学会使用它们吧: 空值合并操作符:?? 只有当操作符左侧为null或undefined时才会返回操作符右侧的值,否则返回左侧的值. eg: ...

  6. Java并发3

    轻量级锁:如果一个有多个线程访问,但多线程访问的时间是错开的,没有竞争,那么可以使用轻量级锁来优化: monitor:重量级锁: 正常我们使用synchronized时,没有竞争则是轻量级锁,当遇到竞 ...

  7. SAP -SE30 程序运行时间分析

    运行SE30 选中Program,点击Excute 点击运行 分析结果

  8. Redis系列2:数据持久化提高可用性

    1 介绍 从上一篇的 <深刻理解高性能Redis的本质> 中可以知道, 我们经常在数据库层上加一层缓存(如Redis),来保证数据的访问效率. 这样性能确实也有了大幅度的提升,但是本身Re ...

  9. ssh空闲一段时间后自动断网

    ssh空闲一段时间后自动断网 用客户端工具,例如securecrt连接linux服务器,有的会出现过一段时间没有任何操作,客户端与服务器就断开了连接. 造成这个的原因,主要是因为客户端与服务器之间存在 ...

  10. Linux开放指定端口命令(CentOS)

    1.开启防火墙 systemctl start firewalld 2.开放指定端口 ##linux打开防火墙3389端口 firewall-cmd --zone=public --add-port= ...