教你使用swoole监听redis数据
程序员文章站
2022-06-15 18:34:09
swoole如何监听redis数据? Laravel使用swoole监听redis 开始之前,请先确保redis已经正确安装,并正常运行。 Laravel代码 在App\Events目录下新建RedisTest事件
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监听功能实现
推荐阅读
-
使用JAVA通过ARP欺骗类似P2P终结者实现数据封包监听
-
Android 使用ContentObserver监听数据库内容是否更改
-
Spring Boot 与 Kotlin 使用Redis数据库的配置方法
-
java连接redis是数据库(redis在java项目中的使用)
-
使用PHP导出Redis数据到另一个Redis中的代码
-
java连接redis是数据库(redis在java项目中的使用)
-
关于使用key/value数据库redis和TTSERVER的心得体会
-
在Laravel5.6中使用Swoole的协程数据库查询
-
Vue数据监听方法watch的使用
-
Vue数据监听方法watch的使用