您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)swoole監(jiān)聽redis數(shù)據(jù)的方法,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
swoole如何監(jiān)聽redis數(shù)據(jù)?
Laravel使用swoole監(jiān)聽redis
開始之前,請先確保redis已經(jīng)正確安裝,并正常運(yùn)行。
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 監(jiān)聽事件代碼
<?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 登記事件/監(jiān)聽關(guān)系
protected $listen = [ 'App\Events\RedisTest' => [ 'App\Listeners\RedisTestListener', ], ];
監(jiān)聽命令
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 管理進(jìn)程
在 /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
完成后,執(zhí)行以下命令重載
supervisorctl reload
=================================
進(jìn)入redis 客戶端,發(fā)布一個廣播通知到 msg_0 頻道
publish msg_0 "Hello Bella"
如果 laravel目錄下的 storage\logs\laravel.log 最后的日志中記錄了廣播發(fā)送的通知,則redis監(jiān)聽功能實(shí)現(xiàn)
關(guān)于swoole監(jiān)聽redis數(shù)據(jù)的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。