diff --git a/src/nacos/publish/nacos.php b/src/nacos/publish/nacos.php index 7e1c64211..de1f0fdbc 100644 --- a/src/nacos/publish/nacos.php +++ b/src/nacos/publish/nacos.php @@ -1,10 +1,14 @@ '127.0.0.1', 'port' => '8848', @@ -45,5 +49,5 @@ return [ ], ], // 负载策略 random, RoundRobin, WeightedRandom, WeightedRoundRobin - 'loadBalancer' => 'random' + 'loadBalancer' => 'random', ]; diff --git a/src/nacos/src/Config/FetchConfigProcess.php b/src/nacos/src/Config/FetchConfigProcess.php index 350936bbc..390f8e3d2 100644 --- a/src/nacos/src/Config/FetchConfigProcess.php +++ b/src/nacos/src/Config/FetchConfigProcess.php @@ -1,5 +1,14 @@ get($id); } } -if (!function_exists('is_json_str')) { +if (! function_exists('is_json_str')) { function is_json_str($string) { json_decode($string); - return (json_last_error() == JSON_ERROR_NONE); + return json_last_error() == JSON_ERROR_NONE; } } @@ -34,6 +43,5 @@ function xml2array($xml_string) return []; } - return (array)@simplexml_load_string($xml_string, 'SimpleXMLElement', LIBXML_NOCDATA); + return (array) @simplexml_load_string($xml_string, 'SimpleXMLElement', LIBXML_NOCDATA); } - diff --git a/src/nacos/src/Lib/AbstractNacos.php b/src/nacos/src/Lib/AbstractNacos.php index c58b296d7..cb8386548 100644 --- a/src/nacos/src/Lib/AbstractNacos.php +++ b/src/nacos/src/Lib/AbstractNacos.php @@ -1,5 +1,14 @@ 'application/x-www-form-urlencoded' + 'Content-Type' => 'application/x-www-form-urlencoded', ]; return $this->request('POST', '/nacos/v1/cs/configs', $configModel->toArray(), $headers); diff --git a/src/nacos/src/Lib/NacosInstance.php b/src/nacos/src/Lib/NacosInstance.php index e6a0e7063..c351f1fef 100644 --- a/src/nacos/src/Lib/NacosInstance.php +++ b/src/nacos/src/Lib/NacosInstance.php @@ -1,5 +1,14 @@ list($serviceModel, $clusters, true); $instance = $list['hosts'] ?? []; - if (!$instance) { + if (! $instance) { return false; } $enabled = array_filter($instance, function ($item) { @@ -62,6 +70,34 @@ class NacosInstance extends AbstractNacos return $this->loadBalancer($enabled, $tactics); } + public function detail(InstanceModel $instanceModel) + { + return $this->request('GET', "/nacos/v1/ns/instance?{$instanceModel}"); + } + + public function beat(ServiceModel $serviceModel, InstanceModel $instanceModel) + { + $serviceName = $serviceModel->serviceName; + $groupName = $serviceModel->groupName; + $ephemeral = $instanceModel->ephemeral; + $params = array_filter(compact('serviceName', 'beat', 'groupName', 'ephemeral'), function ($item) { + return $item !== null; + }); + $params['beat'] = $instanceModel->toJson(); + $params_str = http_build_query($params); + + return $this->request('PUT', "/nacos/v1/ns/instance/beat?{$params_str}"); + } + + public function upHealth(InstanceModel $instanceModel) + { + if ($instanceModel->healthy === null) { + $instanceModel->healthy = true; + } + + return $this->request('PUT', "/nacos/v1/ns/health/instance?{$instanceModel}") == 'ok'; + } + protected function loadBalancer($nodes, $tactics = 'random') { $load_nodes = []; @@ -92,32 +128,4 @@ class NacosInstance extends AbstractNacos return $nacos_nodes["{$select->host}:{$select->port}"]; } - - public function detail(InstanceModel $instanceModel) - { - return $this->request('GET', "/nacos/v1/ns/instance?{$instanceModel}"); - } - - public function beat(ServiceModel $serviceModel, InstanceModel $instanceModel) - { - $serviceName = $serviceModel->serviceName; - $groupName = $serviceModel->groupName; - $ephemeral = $instanceModel->ephemeral; - $params = array_filter(compact('serviceName', 'beat', 'groupName', 'ephemeral'), function ($item) { - return $item !== null; - }); - $params['beat'] = $instanceModel->toJson(); - $params_str = http_build_query($params); - - return $this->request('PUT', "/nacos/v1/ns/instance/beat?{$params_str}"); - } - - public function upHealth(InstanceModel $instanceModel) - { - if ($instanceModel->healthy === null) { - $instanceModel->healthy = true; - } - - return $this->request('PUT', "/nacos/v1/ns/health/instance?{$instanceModel}") == 'ok'; - } } diff --git a/src/nacos/src/Lib/NacosOperator.php b/src/nacos/src/Lib/NacosOperator.php index 8ace72efa..7f53c99a1 100644 --- a/src/nacos/src/Lib/NacosOperator.php +++ b/src/nacos/src/Lib/NacosOperator.php @@ -1,5 +1,14 @@ get('nacos'); @@ -32,11 +42,10 @@ class BootAppConfListener implements ListenerInterface $instance = make(ThisInstance::class); /** @var NacosInstance $nacos_instance */ $nacos_instance = make(NacosInstance::class); - if (!$nacos_instance->register($instance)) { + if (! $nacos_instance->register($instance)) { throw new \Exception("nacos register instance fail: {$instance}"); - } else { - $logger->info('nacos register instance success!', compact('instance')); } + $logger->info('nacos register instance success!', compact('instance')); // 注册服务 /** @var NacosService $nacos_service */ @@ -44,11 +53,10 @@ class BootAppConfListener implements ListenerInterface /** @var ServiceModel $service */ $service = make(ServiceModel::class, ['config' => config('nacos.service')]); $exist = $nacos_service->detail($service); - if (!$exist && !$nacos_service->create($service)) { + if (! $exist && ! $nacos_service->create($service)) { throw new \Exception("nacos register service fail: {$service}"); - } else { - $logger->info('nacos register service success!', compact('instance')); } + $logger->info('nacos register service success!', compact('instance')); $remote_config = RemoteConfig::get(); /** @var \Hyperf\Config\Config $config */ diff --git a/src/nacos/src/Listener/OnShutdownListener.php b/src/nacos/src/Listener/OnShutdownListener.php index c5a84e0f4..eebda6a9c 100644 --- a/src/nacos/src/Listener/OnShutdownListener.php +++ b/src/nacos/src/Listener/OnShutdownListener.php @@ -1,5 +1,14 @@ getParams()); + } + public function getParams() { $params = array_filter(get_object_vars($this), function ($item) { @@ -25,17 +39,12 @@ abstract class AbstractModel sort($this->required_field); sort($intersect); if ($intersect !== $this->required_field) { - throw new \Exception("缺少关键信息" . implode(',', $this->required_field)); + throw new \Exception('缺少关键信息' . implode(',', $this->required_field)); } return $params; } - public function __toString() - { - return http_build_query($this->getParams()); - } - public function toJson() { return json_encode($this->getParams()); diff --git a/src/nacos/src/Model/ConfigModel.php b/src/nacos/src/Model/ConfigModel.php index 4df9971aa..d233f7c1b 100644 --- a/src/nacos/src/Model/ConfigModel.php +++ b/src/nacos/src/Model/ConfigModel.php @@ -1,5 +1,14 @@ info('nacos send beat success!', compact('instance')); } else { - $logger->error("nacos send beat fail}", compact('instance')); + $logger->error('nacos send beat fail}', compact('instance')); } } - } public function isEnable(): bool diff --git a/src/nacos/src/ThisInstance.php b/src/nacos/src/ThisInstance.php index a16d48ece..36afc47db 100644 --- a/src/nacos/src/ThisInstance.php +++ b/src/nacos/src/ThisInstance.php @@ -1,5 +1,14 @@ ip = current(swoole_get_local_ip()); $this->port = config('server.servers.0.port'); $client = config('nacos.client', []); - if (!isset($client['serviceName'])) { + if (! isset($client['serviceName'])) { throw new \Exception('nacos.client.serviceName is required'); } unset($client['ip'], $client['port']); diff --git a/src/nacos/src/ThisService.php b/src/nacos/src/ThisService.php index e353936b3..5d4ad866b 100644 --- a/src/nacos/src/ThisService.php +++ b/src/nacos/src/ThisService.php @@ -1,5 +1,14 @@ get('api_request')->error($api, [ 'method' => $method, 'options' => $options, - 'exception' => (string)$e, + 'exception' => (string) $e, ]); return false; diff --git a/src/nacos/src/Util/RemoteConfig.php b/src/nacos/src/Util/RemoteConfig.php index 4adc7c6df..a1d5d0188 100644 --- a/src/nacos/src/Util/RemoteConfig.php +++ b/src/nacos/src/Util/RemoteConfig.php @@ -1,5 +1,14 @@