swoole如何监听redis数据?
laravel使用swoole监听redis
开始之前,请先确保redis已经正确安装,并正常运行。
laravel代码
在app\events目录下新建redistest事件
<?phpnamespace app\events;u illuminate\broadcasting\channel;u illuminate\queue\rializesmodels;u illuminate\broadcasting\privatechannel;u illuminate\broadcasting\prencechannel;u illuminate\foundation\events\dispatchable;u illuminate\broadcasting\interactswithsockets;u illuminate\contracts\broadcasting\shouldbroadcast;class redistest{ u dispatchable, interactswithsockets, rializesmodels; public $message; /** * create a new event instance. * * @return void */ public function __construct($message) { $this->message = $message; } /** * get the channels the event should broadcast on. *写作业有用吗 * @return \illuminate\broadcasting\channel|array */ public function broadcaston() { return new privatechannel('channel-name'); }}
app\listeners\redistestlistener 监听事件代码
<?phpnamespace app\listeners;u app\events\redistest;u illuminate\queue\interactswithqueue;u ill甘草片的副作用uminate\contracts\queue\shouldqueue;u illuminate\support\facades\log;class十字对联 redistestlistener{ /** * create the event listener. * * @return void */ public function __construct() { // } /** * handle the event. * * @param redistest $event * @return void */ public function handle(redistest $event) { $message = $event->message; log::info('the message received from subscribed redis channel msg_0: '.$message); }}app\举报材料providers\eventrviceprovider 登记事件/监听关系protected $listen = [ 'app\events\redistest' => [ 'app\listeners\redistestlistener', ], ];
监听命令
app\console\commands\redissubscribe 代码如下
<?phpnamespace app\console\commands;u illuminate\console\command;u swoole_redis;u illuminate\support\facades\event;u app\events\redistest;class redissubscribe extends command{ /** 治疗青春痘印的方法 * the name and signature of the console command. * * @var string */ protected $signature = 'redis:subscribe'; /** * the console command description. * * @var string */ protected $description = 'deamon process to subscribe redis broadcast'; /** * create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * execute the console command. * * @return mixed */ public function handle() { $client = new swoole_redis; $client->on('message', function (swoole_redis $client, $result) { var_dump($result); static $more = fal; if (!$more and $result[0] == 'message') { echo "trigger event redistest\n"; event::fire(new redistest($result[2])); } }); $client->connect('127.0.0.1', 6379, function (swoole_redis $client, $result) { echo "connect\n"; $client->subscribe('msg_0'); }); }}
laravel部分代码完成
==================================
supervisor 管理进程
在 /etc/supervisor/conf.d 文件夹下新建 echo.conf , 代码如下
[group:echos]programs=echo-queue,echo-redis[program:echo-queue]command=php artisan queue:workdirectory=/home/bella/downloads/lnmp/echo1.0/echour=bellaautorestart=trueredirect_stderr=truestdout_logfile=/home/bella/downloads/lnmp/echo1.0/echo/storage/logs/queue.logloglevel=info[program:echo-redis]command=php artisan redis:subscribedirectory=/home/bella/downloads/lnmp/echo1.0/echour=bellaautorestart=trueredirect_stderr=truestdout_logfile=/home/bella/downloads/lnmp/echo1.0/echo/storage/logs/redis.logloglevel=info
完成后,执行以下命令重载
supervisorctl reload
=================================
进入redis 客户端,发布一个广播通知到 msg_0 频道
publish msg_0 "hello bella"
如果 laravel目录下的 storage\logs\laravel.log 最后的日志中记录了广播发送的通知,则redis监听功能实现
本文发布于:2023-04-08 11:28:46,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/3f4c707ec7400aefc7a630d2b72d30f9.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:教你使用swoole监听redis数据.doc
本文 PDF 下载地址:教你使用swoole监听redis数据.pdf
留言与评论(共有 0 条评论) |