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('channel-name');
}
}
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('the message received from subscribed redis channel msg_0: '.$message);
}
}
App \ Providers \ EventServiceProvider登録イベント/リッスン関係
protected $listen = [
'App\Events\RedisTest' => [
'App\Listeners\RedisTestListener',
],
];
監視コマンド
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 = '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の部分的なコード補完
監督者管理プロセス
/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ディレクトリのstorage \ logs \ laravel.logの最後のログに記録されている場合、redis監視機能が実装されています
注意を払い、迷子にならないでください
さてさて皆さん、以上がこの記事の全内容であり、こちらをご覧いただける方はみなタレントです。前述したように、PHPには多くの技術的なポイントがあります。多すぎるため、書くことは本当に不可能であり、書いた後はあまり読まないので、必要に応じて、PDFとドキュメントに整理します。できる
クリックして秘密のコードを入力してください:PHP +「プラットフォーム」
より多くの学習コンテンツについては、給与が段階的に上がることを確認するために読むことができる限り、[Comparative Standard Factory] Catalog of Excellent PHP Architectチュートリアルのカタログにアクセスできます(継続的な更新)。
上記のコンテンツはあなたを助けることを望んでいます。多くのPHPワーカーは、高度になると常にいくつかの問題やボトルネックに遭遇します。ビジネスコードを書きすぎても、方向性はありません。どこから改善を始めればよいかわかりません。これを含むいくつかの情報をまとめました。ただし、これらに限定されません:分散アーキテクチャ、高スケーラビリティ、高パフォーマンス、高同時実行性、サーバーパフォーマンスチューニング、TP6、laravel、YII2、Redis、Swoole、Swoft、Kafka、Mysql最適化、シェルスクリプト、Docker、マイクロサービス、Nginxなど。複数の知識ポイント、高度で先進的なドライグッズは無料で誰とでも共有でき、必要なものは私のPHPテクノロジー交換グループに参加できます