swoole#如何監聽redis資料?
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部分程式碼完成
==== ==============================
supervisor 管理進程
在/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監聽功能實作
以上是swoole如何監聽redis數據的詳細內容。更多資訊請關注PHP中文網其他相關文章!