前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Laravel Zero 集成 Gateway Worker

Laravel Zero 集成 Gateway Worker

作者头像
iVampireSP.com
发布2023-10-21 10:32:13
1930
发布2023-10-21 10:32:13
举报
文章被收录于专栏:iVampireSPの物语iVampireSPの物语

由于最近想写一个新项目,打算依靠 Gateway Worker分布式部署以及 Laravel 的生态。

我的需求是只需要部署 Business Worker,不需要 Gateway 以及 Register。

依赖安装

首先,我们得安装 Gateway Worker。

代码语言:javascript
复制
composer require workerman/gateway-worker
composer require workerman/gatewayclient

gateway-worker 是 Gateway Worker 本体,Gateway client 是用于连接 Gateway worker 的。

至于为何要连接,那是在 Event 事件之外处理时,需要连接到 Gateway worker 来执行操作。比如队列中给用户发送消息(本示例中并没有用到)。

创建命令

这里我将创建一个 Worker 命令。

代码语言:javascript
复制
php application make:command WorkerCommand

随后编辑该文件。

代码语言:javascript
复制
<?php

namespace App\Commands;

use Workerman\Worker;
use GatewayWorker\BusinessWorker;
use LaravelZero\Framework\Commands\Command;

class WorkerCommand extends Command
{
    /**
     * The signature of the command.
     *
     * @var string
     */
    protected $signature = 'worker {action=start} {--daemon}';

    /**
     * The description of the command.
     *
     * @var string
     */
    protected $description = '启动 Gateway Business Worker';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        global $argv;

        if (!in_array($action = $this->argument('action'), ['start', 'stop', 'restart'])) {
            $this->error('Error Arguments');

            return Command::FAILURE;
        }
        $argv[0] = 'worker';
        $argv[1] = $action;
        $argv[2] = $this->option('daemon') ? '-d' : '';

        $this->startBusinessWorker();
    }

    private function startBusinessWorker()
    {
        $worker                  = new BusinessWorker();
        $worker->name            = 'BusinessWorker';
        $worker->count           = 2;
        $worker->registerAddress = '127.0.0.1:1236'; // Register 的地址,注意修改
        $worker->eventHandler    = \App\GatewayWorker\Events::class; // 你的 Event 类

        Worker::runAll();
    }

}

启动

代码语言:javascript
复制
php application worker

可用参数

这个命令中,我们可以使用以下参数。

代码语言:javascript
复制
php application worker start // 启动
php application worker stop // 停止
php application worker reload // 重载
php application worker start -d // 以 daemon 模式运行。
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-5-25 2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 依赖安装
  • 创建命令
  • 启动
  • 可用参数
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档