教你使用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监听功能实现

更多学习内容请访问:

腾讯T3-T4标准精品PHP架构师教程目录大全,只要你看完保证薪资上升一个台阶(持续更新)

以上内容希望帮助到大家,很多PHPer在进阶的时候总会遇到一些问题和瓶颈,业务代码写多了没有方向感,不知道该从那里入手去提升,对此我整理了一些资料,包括但不限于:分布式架构、高可扩展、高性能、高并发、服务器性能调优、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql优化、shell脚本、Docker、微服务、Nginx等多个知识点高级进阶干货需要的可以免费分享给大家,需要的可以加入我的官方群点击此处

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值