_rpc[$serviceName] ?? []; if (empty($config)) { return; } $service = Kiri::getDi()->get(Agent::class); $info = $service->service->service_health($config['config']['ID']); if ($info->getStatusCode() == 200) { return; } $service->service->register($config['config']); } /** * @throws Exception */ public function tick(): void { try { foreach ($this->_rpc as $name => $list) { $this->reRegister($name); } } catch (\Throwable $throwable) { $this->logger->error(error_trigger_format($throwable)); } } /** * @param $serviceName * @return array|null */ public function getServices($serviceName): ?array { $lists = $this->health->setQuery('passing=true')->service($serviceName); if ($lists->getStatusCode() != 200) { return null; } $body = json_decode($lists->getBody(), true); if (empty($body)) { return null; } return array_column($body, 'Service'); } /** * @param string $name * @param string $class * @param array $serviceConfig * @return bool */ public function add(string $name, string $class, array $serviceConfig): bool { if (!isset($this->_rpc[$name])) { $this->_rpc[$name] = ['id' => $serviceConfig['ID'], 'config' => $serviceConfig]; } Router::addServer('rpc', static function () use ($name, $class) { Router::get($name, $class); }); return true; } /** * @return array */ public function doneList(): array { $array = []; foreach ($this->_rpc as $list) { $array[] = $list; } return $array; } /** */ public function register() { $agent = Kiri::getDi()->get(Agent::class); foreach ($this->_rpc as $list) { $agent->service->deregister($list['config']['ID']); $data = $agent->service->register($list['config']); if ($data->getStatusCode() != 200) { return; } } } }