前言
传统的程序执行流程一般是 即时|同步|串行的,在某些场景下,会存在并发低,吞吐量低,响应时间长等问题。在大型系统中,一般会引入消息队列的组件,将流程中部分任务抽离出来放入消息队列,并由专门的消费者作针对性的处理,从而降低系统耦合度,提高系统性能和可用性。
thinkphp-queue 是thinkphp 官方提供的一个消息队列服务,它支持消息队列的一些基本特性:
消息的发布,获取,执行,删除,重发,失败处理,延迟执行,超时控制等
队列的多队列, 内存限制 ,启动,停止,守护等
消息队列可降级为同步执行
thinkphp-queue 内置了 Redis,Database,Topthink ,Sync这四种驱动。
本文主要介绍如何使用tp5自带的think-queue消息队列结合supervisor进程管理使队列能够常驻进程。
think-queue安装与基本使用
tp5框架及think-queue的安装方法及队列驱动配置
tp5框架及think-queue安装
推荐使用composer安装
tp5安装composer create-project topthink/think 5.0.*
think-queue安装composer require topthink/think-queue
消息队列的驱动配置
配置文件在项目的路径如下图:
内容如下:
return [
'connector' => 'Redis',// Redis 驱动
'expire' => 60,// 任务的过期时间,默认为60秒; 若要禁用,则设置为 null
'default' => 'default',// 默认的队列名称
'host' => '127.0.0.1',// redis 主机ip
'port' => 6379,// redis 端口
'password' => '',// redis 密码
'select' => 0,// 使用哪一个 db,默认为 db0
'timeout' => 0,// redis连接的超时时间
'persistent' => false,// 是否是长连接
];
具体配置可根据实际情况自行调整
创建一张表,用于展示消费队列写入数据库的操作
CREATE TABLE `test` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`task_type` varchar(50) DEFAULT '' COMMENT '任务类型',
`data` text COMMENT '数据',
`pdate` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
创建消息队列任务
入队(生产者)
在index模块新增 \application\index\controller\JobTest.php 控制器,在该控制器中添加 actionWithHelloJob 方法
生产者推送消息到队列有2种方法:push()和later(),push是立即执行,later是推送到队列里,延迟执行。代码如下
public function actionWithHelloJob(){
// 1.当前任务将由哪个类来负责处理。
// 当轮到该任务时,系统将生成一个该类的实例,并调用其 fire 方法
$jobHandlerClassName = 'app\index\job\Hello';
// 2.当前任务归属的队列名称,如果为新队列,会自动创建
$jobQueueName = "helloJobQueue";
// 3.当前任务所需的业务数据 . 不能为 resource 类型,其他类型最终将转化为json形式的字符串
$jobData = [ 'ts' => time(), 'bizId' => uniqid() , 'data' => $_GET ] ;
// 4.将该任务推送到消息队列,等待对应的消费者去执行
$isPushed = Queue::push( $jobHandlerClassName , $jobData , $jobQueueName );
//$isPushed = Queue::later(10,$jobHandlerClassName,$jobData,$jobQueueName); //把任务分配到队列中,延迟10s后执行
// database 驱动时,返回值为 1|false ; redis 驱动时,返回值为 随机字符串|false
if( $isPushed !== false ){
echo date('Y-m-d H:i:s') . " a new Hello Job is Pushed to the MQ"."
";
}else{
echo 'something went wrong.';
}
}
消费者的消费与删除
创建Hello 消费者类,用于处理 helloJobQueue 队列中的任务;新增 \application\index\job\Hello.php 消费者类,并编写其 fire() 方法
代码如下:
namespace app\index\job;
use think\queue\Job;
class Hello {
public function fire(Job $job,$data) {
// 有些消息在到达消费者时,可能已经不再需要执行了
$isJobStillNeedToBeDone = $this->checkDatabaseToSeeIfJobNeedToBeDone($data);
if(!$isJobStillNeedToBeDone){
$job->delete();
return;
}
$isJobDone = $this->doHelloJob($data);
if ($isJobDone) {
// 如果任务执行成功, 记得删除任务
$job->delete();
print("Hello Job has been done and deleted"."\n");
}else{
if ($job->attempts() > 3) {
//通过这个方法可以检查这个任务已经重试了几次了
print("Hello Job has been retried more than 3 times!"."\n");
$job->delete();
// 也可以重新发布这个任务
//print("Hello Job will be availabe again after 2s."."\n");
//$job->release(2); //$delay为延迟时间,表示该任务延迟2秒后再执行
}
}
}
/**
* 有些消息在到达消费者时,可能已经不再需要执行了
* @param array|mixed $data 发布任务时自定义的数据
* @return boolean 任务执行的结果
*/
private function checkDatabaseToSeeIfJobNeedToBeDone($data){
return true;
}
/**
* 根据消息中的数据进行实际的业务处理...
*/
private function doHelloJob($data)
{
print("Hello Job Started. job Data is: ".var_export($data,true)." \n");
print("Hello Job is Fired at " . date('Y-m-d H:i:s') ." \n");
print("Hello Job is Done!"." \n");
return true;
}
}
执行之前,看下现在的目录结构
处理任务(消费者)
打开终端切换到当前项目根目录下,执行下面的命令:
work命令又可分为单次执行和循环执行两种模式。
单次执行:不添加 --daemon参数,该模式下,work进程在处理完下一个消息后直接结束当前进程。当队列为空时,会sleep一段时间然后退出。
循环执行:添加了 --daemon参数,该模式下,work进程会循环地处理队列中的消息,直到内存超出参数配置才结束进程。当队列为空时,会在每次循环中sleep一段时间。
php think queue:work --daemon --queue helloJobQueue
会看到如下信息:
[root@localhost tpqueue]# php think queue:work --daemon --queue helloJobQueue
Hello Job Started. job Data is: array (
'ts' => 1565246136,
'bizId' => '5d4bc2b88f03b',
'data' =>
array (
),
)
Hello Job is Fired at -08-08 14:35:39
Hello Job is Done!
Hello Job has been done and deleted
Processed: app\index\job\Hello
到这里我们成功的经历了一个消息的 创建->推送->消费->删除的基本流程
消息队列的开始,停止与重启
开始一个消息队列:php think queue:work
停止所有的消息队列:php think queue:restart
重启所有的消息队列:php think queue:restart
php think queue:work
多模块,多任务的处理
多模块
单模块项目推荐使用 app\job 作为任务类的命名空间
多模块项目可用使用 app\module\job 作为任务类的命名空间 也可以放在任意可以自动加载到的地方
多任务
多任务例子:
在 \application\index\controller\JobTest.php 控制器中,添加 multiTask()方法:
public function multiTask() {
$taskType = $_GET['taskType'];
switch ($taskType) {
case "taskOne":
$jobHandleClassName = "app\index\job\multiTask@taskOne";
$jobQueueName = "taskOneQueue";
$jobData = ['ts'=>time(), 'bizId'=>uniqid(), 'data'=>$_GET];
break;
case "taskTwo":
$jobHandleClassName = "app\index\job\multiTask@taskTwo";
$jobQueueName = "taskTwoQueue";
$jobData = ['ts'=>time(), 'bizId'=>uniqid(), 'data'=>$_GET];
break;
default:
break;
}
$isPushed = Queue::push($jobHandleClassName, $jobData, $jobQueueName);
if ($isPushed!==false) {
echo date('Y-m-d H:i:s')."the $taskType of multiTask job has been pushed to $jobQueueName
";
}else {
throw new Exception("push a new $taskType of multiTask job Failed!");
}
}
新增 \application\index\job\MultiTask.php 消费者类,并编写其 taskOne() 和 taskTwo()方法
namespace app\index\job;
use think\queue\Job;
class MultiTask {
public function taskOne(Job $job, $data) {
$isDone = $this->doTaskOne($data);
if ($isDone) {
$job->delete();
print ("INFO:the taskOne of multiTask has been done and delete!\n");
return;
}else {
if ($job->attempts()>3) {
$job->delete();
}
}
}
public function taskTwo(Job $job, $data) {
$isDone = $this->doTaskTwo($data);
if ($isDone) {
$job->delete();
print ("INFO:the taskTwo of multiTask has been done and delete! \n");
}else {
if ($job->attempts()>3) {
$job->delete();
}
}
}
private function doTaskOne($data) {
$id = db('test')->insertGetId(['task_type'=>'task one','data'=>json_encode($data)]);
print ("INFO: doing taskOne of multiTask! the db return id is :$id\n");
return true;
}
private function doTaskTwo($data) {
$id = db('test')->insertGetId(['task_type'=>'task two','data'=>json_encode($data)]);
print ("INFO: doing taskTwo of multiTask! the db return id is :$id\n");
return true;
}
}
最终执行结果如下:
supervisor的安装和配置
supervisor是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具。可以很方便的监听、启动、停止、重启一个或多个进程。用supervisor管理的进程,当一个进程意外被杀死,supervisor监听到进程死后,会自动将它重启,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。
yum安装supervisor
yum install epel-release
yum install supervisor
//设置开机自动启动
systemctl enable supervisord
配置
找到/etc/supervisord.conf配置文件,编辑信息如下:
[unix_http_server]
file=/tmp/supervisor.sock ; the path to the socket file
;chmod=0700 ; socket file mode (default 0700)
;chown=nobody:nogroup ; socket file uid:gid owner
;username=user ; default is no username (open server)
;password=123 ; default is no password (open server)
supervisord]
logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log
logfile_maxbytes=50MB ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=10 ; # of main logfile backups; 0 means none, default 10
loglevel=info ; log level; default info; others: debug,warn,trace
pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false ; start in foreground if true; default false
minfds=1024 ; min. avail startup file descriptors; default 1024
minprocs=200 ; min. avail process descriptors;default 200
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
[include]
;files = relative/directory/*.ini
files = /etc/supervisor/*.conf
file,logfile,pidfile,serverurl,files的路径可根据自身需要去自定义
在/etc 目录里创建一个supervisor文件,然后在/etc/supervisor目录下创建一个.conf文件,这里命名为queue.conf。
对于index这个单模块而言,不同的业务逻辑为了区分可能会存在多个队列名,这种情况将多个队列名用逗号拼接起来,内容如下:
[program:queue]
user=root
command=php /www/wwwroot/tpqueue/think queue:work --queue helloJobQueue,taskOneQueue,taskTwoQueue --daemon
启动supervisor
supervisorctl -c /etc/supervisord.conf
上面这个命令会进入 supervisorctl 的 shell 界面,然后可以执行不同的命令了
status # 查看程序状态
stop thrift-log # 关闭 usercenter 程序
start thrift-log # 启动 usercenter 程序
restart thrift-log # 重启 usercenter 程序
reread # 读取有更新(增加)的配置文件,不会启动新添加的程序
update # 重启配置文件修改过的程序
例如启动queue程序:
这时再去推送消息,可以看到如下信息:
红圈中的是日志文件,可见队列消费完成,数据插入成功
数据库插入数据如下:
结束
至此,tp5(think-queue)消息队列结合supervisor已实现进程常驻