wenman中整合getwaywork,可以自定义worker名称吗

zyyzcr

wenman中整合getwaywork,可以自定义worker名称吗?

如图所示

<?php

use Webman\GatewayWorker\Gateway;
use Webman\GatewayWorker\BusinessWorker;
use Webman\GatewayWorker\Register;

return [
    'gateway' => [
        'handler'     => Gateway::class,
        'listen'      => 'websocket://0.0.0.0:7373',
        'count'       => cpu_count(),
        'reloadable'  => false,
        'constructor' => ['config' => [
            'lanIp'           => '127.0.0.1',
            'startPort'       => 2300,
            'pingInterval'    => 25,
            'pingData'        => '{"type":"ping"}',
            'registerAddress' => '127.0.0.1:1236',
            'onConnect'       => function(){},
        ]]
    ],[
        'handler'     => Gateway::class,
        'listen'      => 'tcp://0.0.0.0:8383',
        'count'       => cpu_count(),
        'reloadable'  => false,
        'constructor' => ['config' => [
            'lanIp'           => '127.0.0.1',
            'startPort'       => 2310,
            'pingInterval'    => 25,
            'pingData'        => '{"type":"ping"}',
            'registerAddress' => '127.0.0.1:1236',
            'onConnect'       => function(){},
        ]]
    ],
    'worker' => [
        'handler'     => BusinessWorker::class,
        'count'       => cpu_count()*2,
        'constructor' => ['config' => [
            'eventHandler'    => plugin\webman\gateway_1\Events::class,
            'name'            => 'ChatBusinessWorker',
            'registerAddress' => '127.0.0.1:1236',
        ]]
    ],
    'register' => [
        'handler'     => Register::class,
        'listen'      => 'text://0.0.0.0:1236',
        'count'       => 1, // Must be 1
        'constructor' => []
    ],
];
989 7 0
7个回答

Gin

截图
截图
根据目录显示的

  • ichynul 2023-03-21

    前面会自动加一串plugin.xxx.xxx 的

  • zyyzcr 2023-03-21

    有办法修改吗?

  • iot.workerman.net物联网平台 2023-03-21

    加前缀的目的是为了不跟其他插件冲突

  • zyyzcr 2023-03-21

    当包含两个getway时,第二个会显示为0

        'gateway' => [
            'handler'     => Gateway::class,
            'listen'      => 'websocket://0.0.0.0:7373',
            'count'       => cpu_count(),
            'reloadable'  => false,
            'constructor' => ['config' => [
                'lanIp'           => '127.0.0.1',
                'startPort'       => 2300,
                'pingInterval'    => 25,
                'pingData'        => '{"type":"ping"}',
                'registerAddress' => '127.0.0.1:1236',
                'onConnect'       => function(){},
            ]]
        ],[
            'handler'     => Gateway::class,
            'listen'      => 'tcp://0.0.0.0:8383',
            'count'       => cpu_count(),
            'reloadable'  => false,
            'constructor' => ['config' => [
                'lanIp'           => '127.0.0.1',
                'startPort'       => 2310,
                'pingInterval'    => 25,
                'pingData'        => '{"type":"ping"}',
                'registerAddress' => '127.0.0.1:1236',
                'onConnect'       => function(){},
            ]]
        ]
  • Gin 2023-03-21

    多协议?

  • zyyzcr 2023-03-21

    是的

ichynul

除非改框架才行

  • 暂无评论
Gin

第二个gateway 对应的 key是0 所以显示 0
截图

  • 暂无评论
zyyzcr

第二个gateway 对应的 key是0 所以显示 0

完美解决,多谢

  • 暂无评论
efnic
<?php

namespace david\gateway;

use RuntimeException;
use Workerman\Connection\TcpConnection;
use Workerman\Worker;

/**
 * 启动GatewayWorker服务
 * - @link https://www.workerman.net/doc/gateway-worker/
 */
class Server
{
    /**
     * 运行
     * @return void
     */
    public static function run(): void
    {
        /**
         * 初始化默认的配置
         */
        $config = static::config('default');
        Worker::$pidFile = $config['pid_file'] ?? '';
        Worker::$stdoutFile = $config['stdout_file'] ?? '/dev/null';
        Worker::$logFile = $config['log_file'] ?? '';
        Worker::$eventLoopClass = $config['event_loop'] ?? '';
        TcpConnection::$defaultMaxPackageSize = $config['max_package_size'] ?? 10 * 1024 * 1024;
        if (property_exists(Worker::class, 'statusFile')) {
            Worker::$statusFile = $config['status_file'] ?? '';
        }
        if (property_exists(Worker::class, 'stopTimeout')) {
            Worker::$stopTimeout = $config['stop_timeout'] ?? 2;
        }

        /**
         * 启动进程
         */
        foreach (static::config('process') as $name => $config) {
            if ($config['enable'] ?? null) {
                $handler = $config['handler'];
                $listen = $config['listen'] ?? '';
                $properties = $config['properties'] ?? [];
                /** @var Worker $worker */
                $worker = new $handler($listen);
                $worker->name = $name;
                foreach ($properties as $property => $value) {
                    $worker->{$property} = $value;
                }
            }
        }

        Worker::runAll();
    }

    /**
     * GatewayWorker配置
     * @param string $key
     * @return array
     */
    public static function config(string $key = 'default'): array
    {
        //密钥
        $gatewaySecret = getenv('GATEWAY_SECRET');
        //注册中心监听地址
        $registerListenAddress = getenv('GATEWAY_REGISTER_LISTEN_ADDRESS') ? getenv('GATEWAY_REGISTER_LISTEN_ADDRESS') : '127.0.0.1';
        //注册中心地址
        $registerAddress = getenv('GATEWAY_REGISTER_ADDRESS') ? getenv('GATEWAY_REGISTER_ADDRESS') : '127.0.0.1';
        //注册中心端口
        $registerPort = getenv('GATEWAY_REGISTER_PORT') ? getenv('GATEWAY_REGISTER_PORT') : '1236';
        //本机IP
        $lanIp = getenv('SOCKET_BIND_TO_IP') ? getenv('SOCKET_BIND_TO_IP') : '127.0.0.1';
        if (false === $gatewaySecret) {
            throw new RuntimeException('未设置环境变量:GATEWAY_SECRET');
        }

        //初始化通信密钥
        if (class_exists('\GatewayWorker\Lib\Gateway')) {
            \GatewayWorker\Lib\Gateway::$registerAddress = $registerAddress . ':' . $registerPort;
            \GatewayWorker\Lib\Gateway::$secretKey = $gatewaySecret;
        } else {
            throw new RuntimeException('\GatewayWorker\Lib\Gateway 不存在');
        }

        $config = [
            /**
             * 初始化默认的配置
             */
            'default' => [
                'event_loop' => '',
                'stop_timeout' => 2,
                'pid_file' => runtime_path() . '/gateway_worker.pid',
                'status_file' => runtime_path() . '/gateway_worker.status',
                'stdout_file' => runtime_path() . '/logs/gateway_worker_stdout.log',
                'log_file' => runtime_path() . '/logs/gateway_worker.log',
                'max_package_size' => 10 * 1024 * 1024
            ],
            /**
             * 进程配置
             */
            'process' => [
                //注册中心进程 https://www.workerman.net/doc/gateway-worker/register.html
                'register' => [
                    //使能
                    'enable' => true,
                    //处理类
                    'handler' => \GatewayWorker\Register::class,
                    //监听
                    'listen' => 'text://' . $registerListenAddress . ':' . $registerPort,
                    'properties' => [
                        //进程数
                        'count' => 1,
                        //通信密钥
                        'secretKey' => $gatewaySecret,
                    ],
                ],
                //客户端连接的进程 https://www.workerman.net/doc/gateway-worker/gateway.html
                'gateway' => [
                    //使能
                    'enable' => true,
                    //客户端连接的进程
                    'handler' => \GatewayWorker\Gateway::class,
                    //应用层协议、监听地址、端口
                    'listen' => 'Frame://0.0.0.0:2121',
                    'properties' => [
                        //进程数
                        'count' => 2,
                        //通信密钥
                        'secretKey' => $gatewaySecret,
                        //设置进程收到reload信号后是否reload重启
                        'reloadable' => false,
                        //本机ip,分布式部署时使用内网ip
                        'lanIp' => $lanIp,
                        // 内部通讯起始端口,假如$gateway->count=4,起始端口为4000
                        // 则一般会使用4000 4001 4002 4003 4个端口作为内部通讯端口
                        'startPort' => 4000,
                        //注册中心地址
                        'registerAddress' => $registerAddress . ':' . $registerPort,
                        //心跳检测时间间隔
                        'pingInterval' => 30,
                        //客户端连续$pingNotResponseLimit次$pingInterval时间内不发送任何数据(包括但不限于心跳数据)则断开链接,并触发onClose
                        'pingNotResponseLimit' => 1,
                        'onConnect' => function () {
                        },
                    ],
                ],
                //业务逻辑进程 https://www.workerman.net/doc/gateway-worker/business-worker.html
                'businessWorker' => [
                    //使能
                    'enable' => true,
                    //业务处理类
                    'handler' => \GatewayWorker\BusinessWorker::class,
                    'properties' => [
                        //事件处理类
                        'eventHandler' => \david\gateway\Events::class,
                        //进程数
                        'count' => cpu_count(),
                        //通信密钥
                        'secretKey' => $gatewaySecret,
                        //注册中心地址
                        'registerAddress' => $registerAddress . ':' . $registerPort,
                    ],
                ],
            ],
        ];

        return $config[$key] ?? [];
    }
}
  • 暂无评论
efnic
<?php

namespace david\gateway\bootstrap;

use david\gateway\Server;
use Webman\Bootstrap;
use Workerman\Worker;

/**
 * 初始化\GatewayWorker\Lib\Gateway::class
 */
class LibGateway implements Bootstrap
{
    public static function start(?Worker $worker)
    {
        //读取配置时,自动设置注册中心地址和通信密钥
        Server::config();
    }
}
  • 暂无评论
efnic
#!/usr/bin/env php
<?php

/**
 * GatewayWorker
 * @link https://www.workerman.net/doc/gateway-worker/
 */

use david\gateway\Server;

require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/support/bootstrap.php';
Server::run();
  • efnic 2023-04-02

    启动:php gateway.php start -d

年代过于久远,无法发表回答
×
🔝