微服务:Rabbitmq的WorkQueue模型的使用、默认消费方式(消息队列中间件)

本文主要是介绍微服务:Rabbitmq的WorkQueue模型的使用、默认消费方式(消息队列中间件),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • WorkQueue模型
      • 控制预取消息个数

WorkQueue模型

当然,一个队列,可以由多个消费者去监听。

来实现一下.

生产者:

    @Testpublic void testWorkQueue() throws InterruptedException {// 队列名称String queueName = "simple.queue";// 消息String message = "hello, message-";for (int i = 0; i < 50; i++) {// 发送消息rabbitTemplate.convertAndSend(queueName, message + i);Thread.sleep(30); // 每次}}

消费者(这里我们弄两个):

@Component
public class SpringRabbitListener {@RabbitListener(queues = "simple.queue")public void listenSimpleQueueMessage1(String msg) throws InterruptedException {System.out.println("消费者1接收到消息:" + msg);}@RabbitListener(queues = "simple.queue")public void listenSimpleQueueMessage2(String msg) throws InterruptedException {System.out.println("消费者2接收到消息:" + msg);}
}

启动看一下结果:

先启动消费者,再发送大量消息:

在这里插入图片描述

这里是因为mq有预分配,一人一半,消费能力一样,所以看起来像是轮流一人执行了一次一样,其实不是,后面会说到。

先发送大量消息,再启动消费者:

在这里插入图片描述

这里是因为消费者1先启动了,2还没启动呢,就被1消费完了。

所以我们改造一下测试代码,让消费者消费能力不同,同时让消费者先都启动,然后再送大量消息:

    @RabbitListener(queues = "simple.queue")public void listenSimpleQueueMessage1(String msg) throws InterruptedException {System.out.println("消费者1接收到消息:" + msg);Thread.sleep(20);}@RabbitListener(queues = "simple.queue")public void listenSimpleQueueMessage2(String msg) throws InterruptedException {System.out.println("消费者2接收到消息:" + msg);Thread.sleep(200);}
消费者2接收到消息:hello, message-0
消费者1接收到消息:hello, message-1
消费者1接收到消息:hello, message-3
消费者1接收到消息:hello, message-5
消费者2接收到消息:hello, message-2
消费者1接收到消息:hello, message-7
消费者1接收到消息:hello, message-9
消费者1接收到消息:hello, message-11
消费者2接收到消息:hello, message-4
消费者1接收到消息:hello, message-13
消费者1接收到消息:hello, message-15
消费者1接收到消息:hello, message-17
消费者1接收到消息:hello, message-19
消费者2接收到消息:hello, message-6
消费者1接收到消息:hello, message-21
消费者1接收到消息:hello, message-23
消费者1接收到消息:hello, message-25
消费者2接收到消息:hello, message-8
消费者1接收到消息:hello, message-27
消费者1接收到消息:hello, message-29
消费者1接收到消息:hello, message-31
消费者2接收到消息:hello, message-10
消费者1接收到消息:hello, message-33
消费者1接收到消息:hello, message-35
消费者1接收到消息:hello, message-37
消费者2接收到消息:hello, message-12
消费者1接收到消息:hello, message-39
消费者1接收到消息:hello, message-41
消费者1接收到消息:hello, message-43
消费者2接收到消息:hello, message-14
消费者1接收到消息:hello, message-45
消费者1接收到消息:hello, message-47
消费者1接收到消息:hello, message-49
消费者2接收到消息:hello, message-16
消费者2接收到消息:hello, message-18
消费者2接收到消息:hello, message-20
消费者2接收到消息:hello, message-22
消费者2接收到消息:hello, message-24
消费者2接收到消息:hello, message-26
消费者2接收到消息:hello, message-28
消费者2接收到消息:hello, message-30
消费者2接收到消息:hello, message-32
消费者2接收到消息:hello, message-34
消费者2接收到消息:hello, message-36
消费者2接收到消息:hello, message-38
消费者2接收到消息:hello, message-40
消费者2接收到消息:hello, message-42
消费者2接收到消息:hello, message-44
消费者2接收到消息:hello, message-46
消费者2接收到消息:hello, message-48

可以看到,其实rabbitmq默认有预分配(预取,每个消费者和队列中有一个通道,存放预取的消息),平均分消息,然后各自独立消费,所以消费者2要比消费者1消费完25条(50/2)消息时间长。

显然是不合理的,我们可以改造一下:

控制预取消息个数

配置中prefetch设置为1,每次消费完消息才取下一个。(能力越大,责任越大,消费快的,消费越多)

spring:rabbitmq:host: ip # 主机名port: 5672 # 端口virtual-host: / # 虚拟主机username: guest # 用户名password: guest # 密码listener:simple:prefetch: 1

重新试一下:

消费者2接收到消息:hello, message-0
消费者1接收到消息:hello, message-1
消费者1接收到消息:hello, message-2
消费者1接收到消息:hello, message-3
消费者1接收到消息:hello, message-4
消费者2接收到消息:hello, message-5
消费者1接收到消息:hello, message-6
消费者1接收到消息:hello, message-7
消费者1接收到消息:hello, message-8
消费者1接收到消息:hello, message-9
消费者2接收到消息:hello, message-10
消费者1接收到消息:hello, message-11
消费者1接收到消息:hello, message-12
消费者1接收到消息:hello, message-13
消费者1接收到消息:hello, message-14
消费者2接收到消息:hello, message-15
消费者1接收到消息:hello, message-16
消费者1接收到消息:hello, message-17
消费者1接收到消息:hello, message-18
消费者2接收到消息:hello, message-19
消费者1接收到消息:hello, message-20
消费者1接收到消息:hello, message-21
消费者1接收到消息:hello, message-22
消费者1接收到消息:hello, message-23
消费者2接收到消息:hello, message-24
消费者1接收到消息:hello, message-25
消费者1接收到消息:hello, message-26
消费者1接收到消息:hello, message-27
消费者1接收到消息:hello, message-28
消费者2接收到消息:hello, message-29
消费者1接收到消息:hello, message-30
消费者1接收到消息:hello, message-31
消费者1接收到消息:hello, message-32
消费者1接收到消息:hello, message-33
消费者2接收到消息:hello, message-34
消费者1接收到消息:hello, message-35
消费者1接收到消息:hello, message-36
消费者1接收到消息:hello, message-37
消费者1接收到消息:hello, message-38
消费者2接收到消息:hello, message-39
消费者1接收到消息:hello, message-40
消费者1接收到消息:hello, message-41
消费者1接收到消息:hello, message-42
消费者1接收到消息:hello, message-43
消费者2接收到消息:hello, message-44
消费者1接收到消息:hello, message-45
消费者1接收到消息:hello, message-46
消费者1接收到消息:hello, message-47
消费者1接收到消息:hello, message-48
消费者2接收到消息:hello, message-49

这样,消费能力大的(消费者1),消费的越多。

这篇关于微服务:Rabbitmq的WorkQueue模型的使用、默认消费方式(消息队列中间件)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1026672

相关文章

HTTP 与 SpringBoot 参数提交与接收协议方式

《HTTP与SpringBoot参数提交与接收协议方式》HTTP参数提交方式包括URL查询、表单、JSON/XML、路径变量、头部、Cookie、GraphQL、WebSocket和SSE,依据... 目录HTTP 协议支持多种参数提交方式,主要取决于请求方法(Method)和内容类型(Content-Ty

sky-take-out项目中Redis的使用示例详解

《sky-take-out项目中Redis的使用示例详解》SpringCache是Spring的缓存抽象层,通过注解简化缓存管理,支持Redis等提供者,适用于方法结果缓存、更新和删除操作,但无法实现... 目录Spring Cache主要特性核心注解1.@Cacheable2.@CachePut3.@Ca

C#下Newtonsoft.Json的具体使用

《C#下Newtonsoft.Json的具体使用》Newtonsoft.Json是一个非常流行的C#JSON序列化和反序列化库,它可以方便地将C#对象转换为JSON格式,或者将JSON数据解析为C#对... 目录安装 Newtonsoft.json基本用法1. 序列化 C# 对象为 JSON2. 反序列化

RabbitMQ 延时队列插件安装与使用示例详解(基于 Delayed Message Plugin)

《RabbitMQ延时队列插件安装与使用示例详解(基于DelayedMessagePlugin)》本文详解RabbitMQ通过安装rabbitmq_delayed_message_exchan... 目录 一、什么是 RabbitMQ 延时队列? 二、安装前准备✅ RabbitMQ 环境要求 三、安装延时队

sysmain服务可以禁用吗? 电脑sysmain服务关闭后的影响与操作指南

《sysmain服务可以禁用吗?电脑sysmain服务关闭后的影响与操作指南》在Windows系统中,SysMain服务(原名Superfetch)作为一个旨在提升系统性能的关键组件,一直备受用户关... 在使用 Windows 系统时,有时候真有点像在「开盲盒」。全新安装系统后的「默认设置」,往往并不尽编

Python ORM神器之SQLAlchemy基本使用完全指南

《PythonORM神器之SQLAlchemy基本使用完全指南》SQLAlchemy是Python主流ORM框架,通过对象化方式简化数据库操作,支持多数据库,提供引擎、会话、模型等核心组件,实现事务... 目录一、什么是SQLAlchemy?二、安装SQLAlchemy三、核心概念1. Engine(引擎)

Java Stream 并行流简介、使用与注意事项小结

《JavaStream并行流简介、使用与注意事项小结》Java8并行流基于StreamAPI,利用多核CPU提升计算密集型任务效率,但需注意线程安全、顺序不确定及线程池管理,可通过自定义线程池与C... 目录1. 并行流简介​特点:​2. 并行流的简单使用​示例:并行流的基本使用​3. 配合自定义线程池​示

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

GO语言中函数命名返回值的使用

《GO语言中函数命名返回值的使用》在Go语言中,函数可以为其返回值指定名称,这被称为命名返回值或命名返回参数,这种特性可以使代码更清晰,特别是在返回多个值时,感兴趣的可以了解一下... 目录基本语法函数命名返回特点代码示例命名特点基本语法func functionName(parameters) (nam

使用shardingsphere实现mysql数据库分片方式

《使用shardingsphere实现mysql数据库分片方式》本文介绍如何使用ShardingSphere-JDBC在SpringBoot中实现MySQL水平分库,涵盖分片策略、路由算法及零侵入配置... 目录一、ShardingSphere 简介1.1 对比1.2 核心概念1.3 Sharding-Sp