MyQEE\Server\Register\Client::init PHP Method

init() public static method

MyQEE\Server\RPC\Client::init();
public static init ( $group, $id, $isTask = false )
    public static function init($group, $id = -1, $isTask = false)
    {
        self::$host = new Host();
        self::$host->group = $isTask ? "{$group}.task" : $group;
        self::$host->id = $id;
        self::$host->workerNum = $isTask ? Server::$config['swoole']['task_worker_num'] : Server::$server->setting['worker_num'];
        self::$host->port = $isTask ? Server::$config['clusters']['task_port'] : Server::$config['clusters']['port'];
        self::$host->encrypt = Server::$config['clusters']['encrypt'] ? true : false;
        if (Server::$config['clusters']['ip'] && Server::$config['clusters']['ip'] !== '0.0.0.0') {
            self::$host->ip = Server::$config['clusters']['ip'];
        }
        $rpc = RPC::Client();
        self::$rpc = $rpc;
        # 定义回调方法
        $rpc->on('connect', [static::class, 'onConnect']);
        $rpc->on('server.add', [static::class, 'onServerAdd']);
        $rpc->on('server.remove', [static::class, 'onServerRemove']);
        $rpc->connect(Server::$config['clusters']['register']['ip'], Server::$config['clusters']['register']['port']);
    }

Usage Example

Beispiel #1
0
 public function onStart()
 {
     if ($this->server->worker_id === 0) {
         $id = isset(Server::$config['clusters']['id']) && Server::$config['clusters']['id'] >= 0 ? (int) Server::$config['clusters']['id'] : -1;
         \MyQEE\Server\Register\Client::init(Server::$config['clusters']['group'] ?: 'default', $id, true);
     }
     global $argv;
     $className = Server::$namespace . 'WorkerTask';
     if (!class_exists($className)) {
         if ($this->id === 0) {
             Server::$instance->warn("任务进程 {$className} 类不存在");
         }
         $className = '\\MyQEE\\Server\\WorkerTask';
     }
     # 内存限制
     ini_set('memory_limit', Server::$config['server']['task_worker_memory_limit'] ?: '4G');
     Server::setProcessName("php " . implode(' ', $argv) . " [taskServer#{$this->id}]");
     # 启动任务进度对象
     Server::$workerTask = new $className($this->server);
     Server::$workerTask->id = $this->id;
     Server::$workerTask->taskId = $this->id;
     Server::$workerTask->onStart();
 }