总结
豆包 AI 助手文章总结
首页 > php框架 > Swoole > 正文

swoole如何监听redis数据

藏色散人
发布: 2020-04-10 10:00:40
原创
3589人浏览过

swoole如何监听redis数据

swoole如何监听redis数据?

Laravel使用swoole监听redis

开始之前,请先确保redis已经正确安装,并正常运行。

Laravel代码

在App\Events目录下新建RedisTest事件

<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class RedisTest
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
    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 监听事件代码

<?php
namespace App\Listeners;
use App\Events\RedisTest;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use 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\EventServiceProvider 登记事件/监听关系

protected $listen = [
        'App\Events\RedisTest' => [
            'App\Listeners\RedisTestListener',
        ],
    ];
登录后复制

监听命令

App\Console\Commands\RedisSubscribe 代码如下

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use swoole_redis;
use Illuminate\Support\Facades\Event;
use 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 = false;
            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:work
directory=/home/bella/Downloads/lnmp/echo1.0/echo
user=bella
autorestart=true
redirect_stderr=true
stdout_logfile=/home/bella/Downloads/lnmp/echo1.0/echo/storage/logs/queue.log
loglevel=info
[program:echo-redis]
command=php artisan redis:subscribe
directory=/home/bella/Downloads/lnmp/echo1.0/echo
user=bella
autorestart=true
redirect_stderr=true
stdout_logfile=/home/bella/Downloads/lnmp/echo1.0/echo/storage/logs/redis.log
loglevel=info
登录后复制

完成后,执行以下命令重载

supervisorctl reload
登录后复制

=================================

进入redis 客户端,发布一个广播通知到 msg_0 频道

publish msg_0 "Hello Bella"
登录后复制

如果 laravel目录下的 storage\logs\laravel.log 最后的日志中记录了广播发送的通知,则redis监听功能实现

以上就是swoole如何监听redis数据的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
豆包 AI 助手文章总结
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号