mongdch / gaia-channel
The Gaia Framework channel process.
1.1.0
2026-11-07 05:43 UTC
Requires
- php: >=7.4.0
- workerman/channel: ^1.1
Requires (Dev)
None
Suggests
- mongdch/gaia: Required as the Gaia framework runtime environment.
Provides
None
Conflicts
None
Replaces
None
README
Gaia框架定制的workerman/channel通道进程,提供跨进程、跨服务的通信能力。
内置通道服务进程与通信客户端封装,支持两种通信模式:
- 广播(Pub/Sub):发布事件,所有订阅者均会收到
- 队列:发布任务,同一事件下的多个消费者争抢消费(单播)
环境要求
- PHP >= 7.4
- Gaia 框架运行环境
- workerman/channel ^1.1
安装
composer require mongdch/gaia-channel
发布插件文件(启动入口与命令支持):
php gaia vendor:publish gaia\channel
配置
通道服务进程默认监听 0.0.0.0:2206,可通过环境变量调整端口:
CHANNEL_PORT = 2206
监听
0.0.0.0允许其他服务远程连接通道服务;本机客户端连接时会自动使用127.0.0.1。
使用
启动通道服务
php channel.php start -d
客户端通信
在任意 Worker 进程的 onWorkerStart 回调内,通过 ChannelService 单例连接并使用:
use support\channel\ChannelService; // 连接通道服务(必须在 Worker 启动回调内首次调用) $channel = ChannelService::instance();
广播模式(Pub/Sub)
// 订阅事件 $channel->on('user.register', function ($data) { echo "新用户注册: {$data['name']}\n"; }); // 发布事件,所有订阅者均会收到 $channel->publish('user.register', ['name' => 'Mon']); // 取消订阅 $channel->unsubscribe('user.register');
队列模式(争抢消费)
// 监听队列,多个进程监听同一事件时,每次发布只有一个进程消费 $channel->watch('mail.send', function ($data) { // 发送邮件... }); // 发布任务 $channel->enqueue('mail.send', ['to' => '985558837@qq.com']); // 取消监听 $channel->unwatch('mail.send');
生成通道业务进程
php gaia make:channel userChannel
生成的进程文件位于 support/process/channel/UserChannel.php,在 onWorkerStart 中已自动连接通道服务,可直接订阅事件:
public function onWorkerStart(Worker $worker) { Client::on('user.register', function ($data) { // 业务处理... }); }
License
MIT