欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

教你使用swoole监听redis数据

程序员文章站 2022-06-15 18:34:09
swoole如何监听redis数据? Laravel使用swoole监听redis 开始之前,请先确保redis已经正确安装,并正常运行。 Laravel代码 在App\Events目录下新建RedisTest事件

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监听功能实现