swoole How to monitor redis data?
Laravel uses swoole to monitor redis
Before you start, make sure redis is installed properly and running properly.
Laravel code
Create a RedisTest event in the App\Events directory
<?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 listening event code
<?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 Registration events/Monitoring relationship protected $listen = [ 'App\Events\RedisTest' => [ 'App\Listeners\RedisTestListener', ], ];
Monitor command
The code of App\Console\Commands\RedisSubscribe is as follows
<?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'); }); } }
Part of Laravel code completed
==================================
supervisor management process
Create echo.conf in the / etc/supervisor/conf.d folder. The code is as follows
[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
When finished, execute the following command overload
supervisorctl reload
=================================
Enter the redis client and issue a broadcast notification to the MSG? 0 channel
publish msg_0 "Hello Bella"
If the last log of storage\logs\laravel.log in the laravel directory records the notifications sent by the broadcast, the redis monitoring function is implemented