当前位置:网站首页>消息队列实战之队列优先级
消息队列实战之队列优先级
2022-06-12 17:29:00 【weixin_2047679575】
于队列优先级,学院君在先导篇中已经简单介绍过了,这里再系统介绍下。
Laravel 是在处理消息队列时通过指定不同队列的前后顺序来实现队列优先级的,默认情况下所有队列任务都被推送到名为 default 的默认队列:

所以它们是没有优先级之分的。如果你想要为不同任务定义不同的处理优先级,可以通过将它们推送到不同的队列,然后在启动队列处理进程时,通过 --queue 参数为不同优先级的队列进行排序即可,排在前面的优先级高,排在后面的优先级低,关于这一块的底层实现先导篇中已经详细介绍过,这里不再赘述。
假设你的 Laravel 应用中存在多个队列任务,如 ProcessPayment、SendVerificationMessage 等,并且 ProcessPayment优先级比 SendVerificationMessage 高,要实现这个优先级,可以在推送队列任务到消息对列时通过 onQueue 方法指定推送的队列:

或者在任务类中通过设置 queue 属性来指定:

两者效果是一样的。接下来,在启动处理来自多个队列的消息任务时,通过 --queue 参数根据队列处理优先级显式设置不同队列的排序,越靠前优先级越高,反之越低:

这样一来,就可以实现优先处理 payments 队列消息的功能,也就等同于间接实现了队列优先级功能。
但是,这里会引入一个新的问题:如果 payments 队列一直是满的,那么 default 队列就一直不能被消费!这是我们不能接受的,你可以通过开启多个 php artisan queue:work --queue=payments,default 进程来加速消费 payments 队列中任务。
此外,还可以使用不同的队列处理器消费不同的队列任务,这样就可以保证无论何种极端情况下,两个队列中的任务都会被处理:

如果
payments中的任务比较重要,需要尽快处理,可以通过增加该队列的处理进程来实现。
但这个方案也存在问题,那就是如果 payments 队列是满的、而 default 队列是空的话,会导致第二个处理器进程处于空闲状态,结合 Laravel 队列消费的底层实现,该进程会循环读取空队列,导致 CPU 资源的浪费,就像 Ajax 和 Websocket 两种方案实现长连接一样,Ajax 肯定不如 Websocket,因为 Ajax 基于 HTTP 协议,基于客户端轮询的实现存在无效通信,浪费系统资源。
为了解决这个问题,可以这么做:

不过,和前面一样,这样一来也就不存在什么队列优先级了,和都推送到 default 队列没有什么区别,你可以多启动几个 php artisan queue:work --queue=payments,default 处理进程来加速 payments 队列的消费作为折衷方案。
如果你非要杠的话,payments 和 default 队列都为空的话,还是存在空轮询,确实是这样,这也是基于 Redis 列表结构、数据库这些驱动实现消息队列的弊端,因为都是采用轮询来读取队列消息,而不是采用发布订阅这种更高效的事件驱动机制,基于数据库比 Redis 在高并发时性能还要再差一等。
边栏推荐
- 内核中断整体流程图
- Modify the configuration of the router connected to your computer. The website is 192.168.1.1
- Saturated! Can't future programmers work anymore?
- 记录使用yolov5进行旋转目标的检测
- (4) Golang operator
- The R language uses the pyramid function of epidisplay package to visualize the pyramid graph and the pyramid graph based on the existing summary data (table data)
- C # final review programming question (guessed by the teacher)
- First acquaintance with go language
- 2080 virtual machine login command
- Su directly switches to super administrator mode, so that many error reports can be avoided
猜你喜欢

Selenium element positioning

php 实现无限极分类树(递归及其优化)

MySQL事务简介、事务隔离级别

How to win the "Olympic Games" in retail technology for jd.com, the learning tyrant of the "regular examination"?
Memory control of node

布局管理中的sizePolicy的策略问题

redis. clients. jedis. exceptions. JedisDataException: NOAUTH Authentication required

I heard that distributed IDS cannot be incremented globally?

Schrodinger's Japanese learning applet source code

Sudo of uabntu
随机推荐
Exclusive interview with oppo find X5 Product Manager: deeply cultivate self-developed chips to create the ultimate flagship experience with the highest standards
qemu+gdb小节
[BSP video tutorial] stm32h7 video tutorial Issue 8: the last issue of the MDK theme, the new generation of debugging technologies event recorder and RTT, and using stm32cubemx to generate project tem
R language calculates data Table specifies the mean value of a numeric variable when the value of one grouped variable is fixed and another grouped variable
WinForm, crystal report making
Enterprise internal online training system source code
Operating with idle funds
Learn the mitmproxy packet capturing tool from scratch
R语言使用pdf函数将可视化图像结果保存到pdf文件中、使用pdf函数打开图像设备、使用dev.off函数关闭图像设备、自定义width参数和height参数指定图像的宽度和高度
Kill program errors in the cradle with spotbugs
Flink 维表异步查询的实现以及问题排查
Figma从入门到放弃
Nebula's practice of intelligent risk control in akulaku: training and deployment of graph model
How to view, modify, and delete SSH
To understand Devops, you must read these ten books!
R语言使用epiDisplay包的tabpct函数生成二维列联表并使用马赛克图可视化列联表(二维列联表、边际频数、以及按行、按列的比例)、自定义设置cex.axis参数改变轴标签数值的大小
Su directly switches to super administrator mode, so that many error reports can be avoided
R语言使用epiDisplay包的summ函数计算dataframe中指定变量在不同分组变量下的描述性统计汇总信息并可视化有序点图(名称、有效值个数、均值、中位数、标准差、最大值、最小值)
Li Kou today's question 926 Flip string to monotonic increment
全局锁、表锁、行锁