加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

面试官:换人!他连swoole如何监听redis数据都不懂

发布时间:2020-12-13 21:01:17 所属栏目:PHP教程 来源:网络整理
导读:Laravel使用swoole监听redis 开始之前,请先确保redis已经正确安装,并正常运行。 Laravel代码 在AppEvents目录下新建RedisTest事件 ?phpnamespace AppEvents;use IlluminateBroadcastingChannel;use IlluminateQueueSerializesModels;use Illuminate

Laravel使用swoole监听redis

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

Laravel代码

在AppEvents目录下新建RedisTest事件

<?php
namespace AppEvents;
use IlluminateBroadcastingChannel;
use IlluminateQueueSerializesModels;
use IlluminateBroadcastingPrivateChannel;
use IlluminateBroadcastingPresenceChannel;
use IlluminateFoundationEventsDispatchable;
use IlluminateBroadcastingInteractsWithSockets;
use IlluminateContractsBroadcastingShouldBroadcast;
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 IlluminateBroadcastingChannel|array
    */
    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }
}

AppListenersRedisTestListener 监听事件代码

<?php
namespace AppListeners;
use AppEventsRedisTest;
use IlluminateQueueInteractsWithQueue;
use IlluminateContractsQueueShouldQueue;
use IlluminateSupportFacadesLog;
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);
    }
}

AppProvidersEventServiceProvider 登记事件/监听关系

protected $listen = [
        'AppEventsRedisTest' => [
            'AppListenersRedisTestListener',],];

监听命令

AppConsoleCommandsRedisSubscribe 代码如下

<?php
namespace AppConsoleCommands;
use IlluminateConsoleCommand;
use swoole_redis;
use IlluminateSupportFacadesEvent;
use AppEventsRedisTest;
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 RedisTestn";
                Event::fire(new RedisTest($result[2]));
            }
        });
        $client->connect('127.0.0.1',6379,$result) {
            echo "connectn";
            $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目录下的 storagelogslaravel.log 最后的日志中记录了广播发送的通知,则redis监听功能实现

点关注,不迷路

好了各位,以上就是这篇文章的全部内容了,能看到这里的人呀,都是人才。之前说过,PHP方面的技术点很多,也是因为太多了,实在是写不过来,写过来了大家也不会看的太多,所以我这里把它整理成了PDF和文档,如果有需要的可以

点击进入暗号: PHP+「平台」

在这里插入图片描述

在这里插入图片描述


更多学习内容可以访问【对标大厂】精品PHP架构师教程目录大全,只要你能看完保证薪资上升一个台阶(持续更新)

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读