当前位置:网站首页>利用 rpush 和 blpop 实现 Redis 消息队列
利用 rpush 和 blpop 实现 Redis 消息队列
2022-08-03 19:46:00 【叶赫那拉 赫敏】
背景
系统并发比较强的业务中,为了解决并发问题,这个时候就会用到消息队列,异步处理业务。本次就介绍用rpush结合blpop实现Redis的消息队列方案之一。我们此次以取消订单业务为例子(取消订单无业务代码,重点介绍消息队列实现)。
方案介绍
利用redis命令rpush往redis列表尾部插入数据,之后利用blpop阻塞式从列表中获取列表做左边数据(先进先出),阻塞式获取数据是写在后端代码中,也就是消费队列数据的代码中。消费函数
consume(app/WorkQueue.php->consume())由定时任务每5分钟执行一次,在consume()代码中程序执行到290秒会自动结束,因为5分钟后定时任务下一个进程会再次调用
app/WorkQueue.php->consume()。
实现
目录总览
配置
app/config/config.php配置文件,quueDriver为存储的每个队列驱动,cancel_order为取消订单驱动,cancel_order下class值对应app/logic/ Order.php, app/WorkQueue.php->consume()会自动去根据配置到app/logic/下加载这个类;配置method为class对应的类中执行业务逻辑的方法;open代表是否开启此队列,true开启 false关闭,如果为false即使定时任务请求进来,也会中止程序不执行业务代码。

Redis相关
**redis key前缀:**我们使用QUEUE_
**redis队列值得数据方式:**采用json串;在app/WorkQueue.php->consume()中消费队列数据时会转为数组。
入列举例:

执行消费队列数据
消费函数consume(app/WorkQueue.php->consume())由定时任务每5分钟执行一次,在consume()代码中程序执行到290秒会自动结束,因为5分钟后定时任务下一个进程会再次调用app/WorkQueue.php->consume()
定时任务调用消费方法方式
php /app/WorkQueue.php 配置中驱动名称(app/config/config.php比如(cancel_order)
*/5 * * * * php /app/WorkQueue.php cancel_order
<?php
require_once './config/config.php';
class WorkQueue
{
/**
* 执行消息队列消费
*/
public function consume() {
$startTime = time();//程序开始时间
$allowAllExcuteTime = 290;//允许程序总执行时间,因为定时任务会5分钟执行一次,所以到290秒提前结束,因为下一个进程要进来
$configArr = include_once './config/config.php';//配置
$quueDriverConfig = $configArr['quueDriver'];//队列配置
$driverName = $argv[1];//获取定时任务命令传递的驱动名称 比如 php /home/www/app/WorkQueue.php cancel_order 那么此处就是cancelOrder
$redisQueueKey = "QUEUE_{$driverName}";//redis队列的前缀为QUEUE_
//获取队列驱动的logic任务类
$logicName = $quueDriverConfig[$driverName]['class'];
$method = $quueDriverConfig[$driverName]['method'];
//判断此队列驱动是否开启 如果未开启则不消费队列
if($quueDriverConfig[$driverName]['open'] !== true){
return;
}
//加载相应消息队列类
require_once "./logic/queue/{$logicName}.php";
$logicQueueObj = new $logicName();//实例化
$redis = new redis();//redis对象
$redis->connect('127.0.0.1', 6379);
//获取队列任务池列表
while (true) {
$specialTime = $allowAllExcuteTime - (time() - $startTime);//取得程序执行时间和290秒相比较,如果超过280秒直接break
if($specialTime <= 0){
break;
}
//获取任务池的一个任务
$content = $redis->BLPOP($redisQueueKey, $specialTime);//取出左边第一个值 如果没有则阻塞式监听;需要RPUSH入列
$contentArr = json_decode($content,true);
//执行任务
if (is_array($content)) {
$logicQueueObj->$method($contentArr);
}
}
}
}
//实例化类执行消费队列数据
$workQueueObj = new WorkQueue();
$workQueueObj->consume();
BLPOP阻塞式监听队列key $redisQueueKey,可以指定阻塞监听时间$specialTime,如果没有队列数据,会再阻塞监听$specialTime秒
$redis->BLPOP($redisQueueKey, $specialTime)
执行业务代码,$contentArr为队列数据
$logicQueueObj->$method($contentArr)
总结
队列实现方式有很多种,只要选择适合自己业务场景的即可!在实际业务处理中还会涉及到比如从队列取出数据,如果代码业务层处理业务失败,是否会将此队列数据重新压入队列的问题,这里只是稍微提一下。
边栏推荐
- Anaconda virtual environment migration
- 入门3D建模基础教程详细分解
- 百利药业IPO过会:扣非后年亏1.5亿 奥博资本是股东
- Power button brush the topic of merging two orderly array
- X86 function call model analysis
- 危化企业双重预防机制数字化建设进入全面实施阶段
- ScrollView嵌套RV,滑动有阻力不顺滑怎么办?
- MVC vs MVP
- MySQL master-slave, 6 minutes you master!
- ECCV2022 | 用于视频问题回答的视频图Transformer
猜你喜欢
随机推荐
Postgresql-xl全局快照与GTM代码走读(支线)
JMeter笔记5 |Badboy使用和录制
【STM32】标准库-自定义BootLoader
Postgresql source code (64) Query execution - data structure and execution process before submodule Executor (2) execution
1-php学习笔记之数据类型
阿里巴巴政委体系-第九章、阿里政委启示录
LeetCode 952. 按公因数计算最大组件大小
X86函数调用模型分析
pytorch框架实现老照片修复功能详细演示(GPU版)
matplotlib画polygon, circle
149. The largest number on a straight line, and check the set
Handler source code analysis
机器学习中专业术语的个人理解与总结(纯小白)
Reveal how the five operational management level of hundreds of millions of easily flow system
Climbing Stairs (7/30)
高效目标检测:动态候选较大程度提升检测精度(附论文下载)
149. 直线上最多的点数-并查集做法
Calculation of the array serial number of Likou brush questions (one question per day 7/28)
钱江摩托某型号产品ECU货不对版 消费者知情权应如何保障?
MySQL master-slave, 6 minutes you master!









