Interviewer: Substitution! He doesn't even understand how swoole monitors redis data

Laravel uses swoole to monitor redis

Before starting, please make sure that redis has been installed correctly and is 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 monitor 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 event/listening relationship

protected $listen = [
        'App\Events\RedisTest' => [
            'App\Listeners\RedisTestListener',
        ],
    ];

Monitor command

App\Console\Commands\RedisSubscribe 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 partial code completion

supervisor management process

Create a new echo.conf under 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

After completion, execute the following command to reload

supervisorctl reload

Enter the redis client and post a broadcast notification to the msg_0 channel

publish msg_0 "Hello Bella"

If the broadcast notification is recorded in the last log of storage\logs\laravel.log in the laravel directory, the redis monitoring function is implemented

Pay attention, don't get lost

Alright, everyone, the above is the entire content of this article. The people who can see here are all talents . As I said before, there are many technical points in PHP, because there are too many, it is really impossible to write, and you will not read too much after writing it, so I will organize it into PDF and documents here, if necessary Can

Click to enter the secret code: PHP+「Platform」

Insert picture description here

Insert picture description here


For more learning content, you can visit the [Comparative Standard Factory] Catalogue of Excellent PHP Architect Tutorials, as long as you can read it to ensure that the salary will rise a step (continuous update)

The above content hopes to help you . Many PHPers always encounter some problems and bottlenecks when they are advanced. There is no sense of direction when writing too much business code. I don’t know where to start to improve. I have compiled some information about this, including But not limited to: distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripts, Docker, microservices, Nginx, etc. Multiple knowledge points, advanced advanced dry goods, can be shared with everyone for free, and those needed can join my PHP technology exchange group

Guess you like

Origin blog.csdn.net/weixin_49163826/article/details/108733592