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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 先说几句废话,调和气氛。事情的起由来自客户需求频繁变更,伟大的师傅决定横刀立马的改革使用新的框架(created ...
    wsdadan阅读 3,102评论 0 12
  • 过去做事情急,什么东西拿起来就用,不喜欢进行系统性的学习,造成在使用过程中的错误和低效,现在感觉自己耐心多了,用之...
    马文Marvin阅读 2,029评论 0 10
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,010评论 19 139
  • 七月的最后一个下午 ,燥热的天气。 老婆,孩子,父亲,公司, 人到中年,36岁, 不知道该如何定义,偏青年还是偏老...
    沈阳欣纵美科技阅读 171评论 0 0
  • 01 晨光破晓,阴凉中些许柔和,此刻,出发正好。曲水莹莹,想再伸手捧起,却生怕惊扰了这一河柔情;竹叶飘飞,似是不舍...
    爱喝旺仔的小馒头阅读 632评论 8 13