当前位置:网站首页>利用 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)
总结
队列实现方式有很多种,只要选择适合自己业务场景的即可!在实际业务处理中还会涉及到比如从队列取出数据,如果代码业务层处理业务失败,是否会将此队列数据重新压入队列的问题,这里只是稍微提一下。
边栏推荐
- The ecological environmental protection management system based on mobile GIS
- glide set gif start stop
- 力扣刷题之有效的正方形(每日一题7/29)
- 阿里巴巴政委体系-第六章、阿里政委体系运作
- ECCV2022 | 用于视频问题回答的视频图Transformer
- 149. The largest number on a straight line, and check the set
- Postgresql source code (64) Query execution - data structure and execution process before submodule Executor (2) execution
- Unity gets the actual coordinates of the ui on the screen under the canvas
- 百利药业IPO过会:扣非后年亏1.5亿 奥博资本是股东
- Statistical machine learning 】 【 linear regression model
猜你喜欢

Shell编程之循环语句

ECCV2022 | 用于视频问题回答的视频图Transformer

「游戏建模干货」建模大师几步操作,学习经典,赶紧脑补一下吧

花 30 美金请 AI 画家弄了个 logo,网友:画得非常好,下次别画了!

【夜莺监控方案】08-监控msyql集群(prometheuse+n9e+mysqld_exporter)

阿里巴巴政委体系-第八章、阿里政委工作方法论

Shell programming loop statement

Compose原理-compose中是如何实现事件分法的

怎么将自己新文章自动推送给自己的粉丝(巨简单,学不会来打我)

In-depth understanding of JVM-memory structure
随机推荐
Detailed AST abstract syntax tree
LOL英雄联盟卡顿掉帧问题解决办法 2022年8月1日
虚拟机vmware设置nat模式上网
Postgresql source code (65) analysis of the working principle of the new snapshot system Globalvis
傅里叶变换(深入浅出)
Word另存为PDF后无导航栏解决办法
CS免杀姿势
FreeRTOS Intermediate
Force is brushed buckle problem for the sum of two Numbers
ERROR: You don‘t have the SNMP perl module installed.
149. The largest number on a straight line, and check the set
京东云发布新一代分布式数据库StarDB 5.0
力扣刷题之求两数之和
Shell编程之循环语句
Internet Download Manager简介及下载安装包,IDM序列号注册问题解决方法
JWT详解
NNLM、RNNLM等语言模型 实现 下一单词预测(next-word prediction)
Unity获取canvas 下ui 在屏幕中的实际坐标
X86函数调用模型分析
Postgresql源码(65)新快照体系Globalvis工作原理分析