Home > Article > PHP Framework > How swoole monitors redis data
swooleHow to monitor redis data?
Laravel uses swoole to monitor redis
Before you start, please make sure that redis is installed correctly and running normally.
Laravel code
Create a new 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 Register event/listening relationship
protected $listen = [ 'App\Events\RedisTest' => [ 'App\Listeners\RedisTestListener', ], ];
Listening command
App\Console\Commands\RedisSubscribe The code 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'); }); } }
Laravel part of the code is completed
==== ==============================
supervisor management process
is in /etc/supervisor Create a new echo.conf in the /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
After completion, execute the following command to reload
supervisorctl reload
============== ===================
Enter the redis client and publish a broadcast notification to the msg_0 channel
publish msg_0 "Hello Bella"
If the storage in the laravel directory \logs\laravel.log The last log records the notification sent by broadcast, then the redis listening function is implemented
The above is the detailed content of How swoole monitors redis data. For more information, please follow other related articles on the PHP Chinese website!