ホームページ  >  記事  >  PHPフレームワーク  >  swoole が Redis データを監視する方法

swoole が Redis データを監視する方法

藏色散人
藏色散人オリジナル
2020-04-10 10:00:403276ブラウズ

swoole が Redis データを監視する方法

#swooleRedis データを監視するにはどうすればよいですか?

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(&#39;channel-name&#39;);
    }
}

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(&#39;the message received from subscribed redis channel msg_0: &#39;.$message);
    }
}

App\Providers \EventServiceProvider イベント/リスニング関係を登録します

protected $listen = [
        &#39;App\Events\RedisTest&#39; => [
            &#39;App\Listeners\RedisTestListener&#39;,
        ],
    ];

リスニング コマンド

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 = &#39;redis:subscribe&#39;;
    /**
    * The console command description.
    *
    * @var string
    */
    protected $description = &#39;deamon process to subscribe redis broadcast&#39;;
    /**
    * 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(&#39;message&#39;, function (swoole_redis $client, $result) {
            var_dump($result);
            static $more = false;
            if (!$more and $result[0] == &#39;message&#39;)
            {
                echo "trigger Event RedisTest\n";
                Event::fire(new RedisTest($result[2]));
            }
        });
        $client->connect(&#39;127.0.0.1&#39;, 6379, function (swoole_redis $client, $result) {
            echo "connect\n";
            $client->subscribe(&#39;msg_0&#39;);
        });
    }
}

コードの Laravel 部分が完成しました

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

スーパーバイザー管理プロセス

は /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 ディレクトリ \logs\laravel.log にストレージがある場合、最後のログにブロードキャストで送信された通知が記録され、redis リスニング関数が実装されます

以上がswoole が Redis データを監視する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。