>  기사  >  PHP 프레임워크  >  Swoole이 Redis 데이터를 모니터링하는 방법

Swoole이 Redis 데이터를 모니터링하는 방법

藏色散人
藏色散人원래의
2020-04-10 10:00:403276검색

Swoole이 Redis 데이터를 모니터링하는 방법

swooleRedis 데이터를 모니터링하는 방법은 무엇입니까?

Laravel은 swoole을 사용하여 redis를 모니터링합니다

시작하기 전에 Redis가 올바르게 설치되어 있고 정상적으로 실행되고 있는지 확인하세요.

Laravel 코드

AppEvents 디렉터리에 새 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;);
    }
}

AppListenersRedisTestListener 수신 이벤트 코드

<?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);
    }
}

AppProvidersEventServiceProvider 등록 이벤트/수신 관계

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

수신 명령

AppConsoleCommandsRedisSubscribe 코드는 다음과 같습니다

<?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 디렉터리에 있는 Storagelogslaravel.log의 마지막 로그에 방송 알림, redis 청취 기능이 구현되었습니다

위 내용은 Swoole이 Redis 데이터를 모니터링하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.