首页 > 开发 > PHP > 正文

Laravel框架队列原理与用法分析

2024-05-04 22:42:16
字体:
来源:转载
供稿:网友

本文实例讲述了Laravel框架队列原理与用法。分享给大家供大家参考,具体如下:

最近有朋友有朋友问laravel队列的实现原理和经验,刚好用过所以整理了一下分享给大家。

laravel队列配置参见:http://d.laravel-china.org/docs/5.1/queues

原理分析

创建分发任务方法

class TestController extends Controller{  //其他方法  //发送消息  public function SendMessage(Request $request){    ...    $this->dispatch((new SendMessage($sendParams))->onQueue('snail:SendMessage'));  }}

内部实现:

创建消费任务

命令行运行如下命令:

/home/niuyufu/php/bin/php /home/niuyufu/webroot/assistant_api/artisan queue:work --queue=snail:SendMessage --tries=3 --memory=512 --daemon

内部实现:

队列消息分析:

监控redis对应队列消息,具体产生的消息操作,如下:

tail -f | redis-cli -h 10.94.120.13 -p 6380 monitor | grep "queues:snail"
1492446053.406282 [0 10.95.117.155:57132] "WATCH" "queues:snail:SendMessage:delayed"
1492446053.406452 [0 10.95.117.155:57132] "ZRANGEBYSCORE" "queues:snail:SendMessage:delayed" "-inf" "1492446053"
1492446053.406754 [0 10.95.117.155:57132] "WATCH" "queues:snail:SendMessage:reserved"
1492446053.406842 [0 10.95.117.155:57132] "ZRANGEBYSCORE" "queues:snail:SendMessage:reserved" "-inf" "1492446053"
1492446053.407029 [0 10.95.117.155:57132] "LPOP" "queues:snail:SendMessage"
1492446053.407700 [0 10.95.117.155:57132] "ZADD" "queues:snail:SendMessage:reserved" "1492446113" "{job}"
1492446053.463953 [0 10.95.117.155:57132] "ZREM" "queues:snail:SendMessage:reserved" "{job}"

PS:如果你的redis是codis的话,注意了,因为codis禁用方法列表

KEYS, MOVE, OBJECT, RENAME, RENAMENX, SORT, SCAN, BITOP,MSETNX, BLPOP, BRPOP, BRPOPLPUSH, PSUBSCRIBE,PUBLISH, PUNSUBSCRIBE, SUBSCRIBE, UNSUBSCRIBE, DISCARD, EXEC, MULTI, UNWATCH, WATCH, SCRIPT EXISTS, SCRIPT FLUSH, SCRIPT KILL, SCRIPT LOAD, AUTH, ECHO, SELECT, BGREWRITEAOF, BGSAVE, CLIENT KILL, CLIENT LIST, CONFIG GET, CONFIG SET, CONFIG RESETSTAT, DBSIZE, DEBUG OBJECT, DEBUG SEGFAULT, FLUSHALL, FLUSHDB, INFO, LASTSAVE, MONITOR, SAVE, SHUTDOWN, SLAVEOF, SLOWLOG, SYNC, TIME

所以执行消费任务会有以下错误:

[Predis/Connection/ConnectionException]
Error while reading line from the server. [tcp://100.90.154.39:3000]

解决方法为修改config/queue.php

'redis' => [  'driver' => 'redis',  'connection' => 'default',  'queue' => 'default',  'expire' => null,  //禁用即可],
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表