From 5acba9cb953802f7578fceb8726d2c05352633c5 Mon Sep 17 00:00:00 2001 From: "zhenguo.guan" Date: Tue, 5 Nov 2019 11:39:15 +0800 Subject: [PATCH 1/7] =?UTF-8?q?#815=20rpc=20client=E9=80=9A=E8=BF=87consul?= =?UTF-8?q?=E7=AD=9B=E9=80=89=E5=81=A5=E5=BA=B7=E7=9A=84=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E4=B8=8D=E9=80=82=E7=94=A8=E9=9B=86=E7=BE=A4?= =?UTF-8?q?=E7=8E=AF=E5=A2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/rpc-client/src/AbstractServiceClient.php | 49 +++++++++----------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/src/rpc-client/src/AbstractServiceClient.php b/src/rpc-client/src/AbstractServiceClient.php index 8050074607..ee5b7384a9 100644 --- a/src/rpc-client/src/AbstractServiceClient.php +++ b/src/rpc-client/src/AbstractServiceClient.php @@ -103,7 +103,7 @@ public function __construct(ContainerInterface $container) protected function __request(string $method, array $params, ?string $id = null) { - if ($this->idGenerator instanceof IdGeneratorInterface && ! $id) { + if ($this->idGenerator instanceof IdGeneratorInterface && !$id) { $id = $this->idGenerator->generate(); } $response = $this->client->send($this->__generateData($method, $params, $id)); @@ -120,7 +120,7 @@ protected function __request(string $method, array $params, ?string $id = null) protected function __generateRpcPath(string $methodName): string { - if (! $this->serviceName) { + if (!$this->serviceName) { throw new InvalidArgumentException('Parameter $serviceName missing.'); } return $this->pathGenerator->generate($this->serviceName, $methodName); @@ -145,7 +145,7 @@ protected function createLoadBalancer(array $nodes, callable $refresh = null): L */ protected function createNodes(): array { - if (! $this->container->has(ConfigInterface::class)) { + if (!$this->container->has(ConfigInterface::class)) { throw new RuntimeException(sprintf('The object implementation of %s missing.', ConfigInterface::class)); } $refreshCallback = null; @@ -183,7 +183,7 @@ protected function createNodes(): array $nodes = []; foreach ($consumer['nodes'] ?? [] as $item) { if (isset($item['host'], $item['port'])) { - if (! is_int($item['port'])) { + if (!is_int($item['port'])) { throw new InvalidArgumentException(sprintf('Invalid node config [%s], the port option has to a integer.', implode(':', $item))); } $nodes[] = new Node($item['host'], $item['port']); @@ -196,35 +196,32 @@ protected function createNodes(): array protected function getNodesFromConsul(array $config): array { - $agent = $this->createConsulAgent($config); - $services = $agent->services()->json(); - $nodes = []; - foreach ($services as $serviceId => $service) { - if (! isset($service['Service'], $service['Address'], $service['Port']) || $service['Service'] !== $this->serviceName) { - continue; - } - // @TODO Get and set the weight property. - $nodes[$serviceId] = new Node($service['Address'], $service['Port']); - } - if (empty($nodes)) { - return $nodes; - } $health = $this->createConsulHealth($config); - $checks = $health->checks($this->serviceName)->json(); - foreach ($checks ?? [] as $check) { - if (! isset($check['Status'], $check['ServiceID'])) { - continue; + $services = $health->service($this->serviceName)->json(); + $nodes = []; + foreach ($services as $node) { + $passing = true; + $service = $node['Service'] ?? []; + $checks = $node['Checks'] ?? []; + foreach ($checks as $check) { + $status = $check['Status'] ?? false; + if ($status != 'passing') { + $passing = false; + } } - if ($check['Status'] !== 'passing') { - unset($nodes[$check['ServiceID']]); + + if ($passing) { + $address = $service['Address'] ?? ''; + $port = (int)$service['Port'] ?? 0; + $nodes[] = new Node($address, $port); } } - return array_values($nodes); + return $nodes; } protected function createConsulAgent(array $config) { - if (! $this->container->has(Agent::class)) { + if (!$this->container->has(Agent::class)) { throw new InvalidArgumentException('Component of \'hyperf/consul\' is required if you want the client fetch the nodes info from consul.'); } return make(Agent::class, [ @@ -238,7 +235,7 @@ protected function createConsulAgent(array $config) protected function createConsulHealth(array $config): HealthInterface { - if (! $this->container->has(Health::class)) { + if (!$this->container->has(Health::class)) { throw new InvalidArgumentException('Component of \'hyperf/consul\' is required if you want the client fetch the nodes info from consul.'); } return make(Health::class, [ From 4625e9c662e08196088a191d98de1713899ce9e4 Mon Sep 17 00:00:00 2001 From: "colin.kwan" Date: Tue, 5 Nov 2019 11:46:40 +0800 Subject: [PATCH 2/7] remove useless code --- src/rpc-client/src/AbstractServiceClient.php | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/rpc-client/src/AbstractServiceClient.php b/src/rpc-client/src/AbstractServiceClient.php index ee5b7384a9..14b8e8b4c3 100644 --- a/src/rpc-client/src/AbstractServiceClient.php +++ b/src/rpc-client/src/AbstractServiceClient.php @@ -219,20 +219,6 @@ protected function getNodesFromConsul(array $config): array return $nodes; } - protected function createConsulAgent(array $config) - { - if (!$this->container->has(Agent::class)) { - throw new InvalidArgumentException('Component of \'hyperf/consul\' is required if you want the client fetch the nodes info from consul.'); - } - return make(Agent::class, [ - 'clientFactory' => function () use ($config) { - return $this->container->get(ClientFactory::class)->create([ - 'base_uri' => $config['address'] ?? Agent::DEFAULT_URI, - ]); - }, - ]); - } - protected function createConsulHealth(array $config): HealthInterface { if (!$this->container->has(Health::class)) { From 04160bb07f9b83e583d0ce3caa38ae2e2a3c2aa8 Mon Sep 17 00:00:00 2001 From: "colin.kwan" Date: Tue, 5 Nov 2019 11:54:44 +0800 Subject: [PATCH 3/7] code format --- src/rpc-client/src/AbstractServiceClient.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/rpc-client/src/AbstractServiceClient.php b/src/rpc-client/src/AbstractServiceClient.php index 14b8e8b4c3..a46d07c009 100644 --- a/src/rpc-client/src/AbstractServiceClient.php +++ b/src/rpc-client/src/AbstractServiceClient.php @@ -103,7 +103,7 @@ public function __construct(ContainerInterface $container) protected function __request(string $method, array $params, ?string $id = null) { - if ($this->idGenerator instanceof IdGeneratorInterface && !$id) { + if ($this->idGenerator instanceof IdGeneratorInterface && ! $id) { $id = $this->idGenerator->generate(); } $response = $this->client->send($this->__generateData($method, $params, $id)); @@ -120,7 +120,7 @@ protected function __request(string $method, array $params, ?string $id = null) protected function __generateRpcPath(string $methodName): string { - if (!$this->serviceName) { + if (! $this->serviceName) { throw new InvalidArgumentException('Parameter $serviceName missing.'); } return $this->pathGenerator->generate($this->serviceName, $methodName); @@ -145,7 +145,7 @@ protected function createLoadBalancer(array $nodes, callable $refresh = null): L */ protected function createNodes(): array { - if (!$this->container->has(ConfigInterface::class)) { + if (! $this->container->has(ConfigInterface::class)) { throw new RuntimeException(sprintf('The object implementation of %s missing.', ConfigInterface::class)); } $refreshCallback = null; @@ -183,7 +183,7 @@ protected function createNodes(): array $nodes = []; foreach ($consumer['nodes'] ?? [] as $item) { if (isset($item['host'], $item['port'])) { - if (!is_int($item['port'])) { + if (! is_int($item['port'])) { throw new InvalidArgumentException(sprintf('Invalid node config [%s], the port option has to a integer.', implode(':', $item))); } $nodes[] = new Node($item['host'], $item['port']); @@ -221,7 +221,7 @@ protected function getNodesFromConsul(array $config): array protected function createConsulHealth(array $config): HealthInterface { - if (!$this->container->has(Health::class)) { + if (! $this->container->has(Health::class)) { throw new InvalidArgumentException('Component of \'hyperf/consul\' is required if you want the client fetch the nodes info from consul.'); } return make(Health::class, [ From c3bad4c68dda27f7c5774792ae5788fb530ec81e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=9C=9D=E6=99=96?= Date: Thu, 7 Nov 2019 11:39:31 +0800 Subject: [PATCH 4/7] Update AbstractServiceClient.php --- src/rpc-client/src/AbstractServiceClient.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/rpc-client/src/AbstractServiceClient.php b/src/rpc-client/src/AbstractServiceClient.php index a46d07c009..3afdabfbae 100644 --- a/src/rpc-client/src/AbstractServiceClient.php +++ b/src/rpc-client/src/AbstractServiceClient.php @@ -203,9 +203,10 @@ protected function getNodesFromConsul(array $config): array $passing = true; $service = $node['Service'] ?? []; $checks = $node['Checks'] ?? []; + foreach ($checks as $check) { $status = $check['Status'] ?? false; - if ($status != 'passing') { + if ($status !== 'passing') { $passing = false; } } @@ -213,7 +214,7 @@ protected function getNodesFromConsul(array $config): array if ($passing) { $address = $service['Address'] ?? ''; $port = (int)$service['Port'] ?? 0; - $nodes[] = new Node($address, $port); + $address && $port && $nodes[] = new Node($address, $port); } } return $nodes; From 83ac93f933d860cf926b0f7367c59fad31cde5a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=9C=9D=E6=99=96?= Date: Thu, 7 Nov 2019 11:40:09 +0800 Subject: [PATCH 5/7] Update AbstractServiceClient.php --- src/rpc-client/src/AbstractServiceClient.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/rpc-client/src/AbstractServiceClient.php b/src/rpc-client/src/AbstractServiceClient.php index 3afdabfbae..170b1f4417 100644 --- a/src/rpc-client/src/AbstractServiceClient.php +++ b/src/rpc-client/src/AbstractServiceClient.php @@ -214,6 +214,7 @@ protected function getNodesFromConsul(array $config): array if ($passing) { $address = $service['Address'] ?? ''; $port = (int)$service['Port'] ?? 0; + // @TODO Get and set the weight property. $address && $port && $nodes[] = new Node($address, $port); } } From bfd15a2ae000f511a67a1499be17f3f6d5bcdcef Mon Sep 17 00:00:00 2001 From: huangzhhui Date: Thu, 7 Nov 2019 12:20:17 +0800 Subject: [PATCH 6/7] Add Changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa3e3d8b2e..2a435422f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - [#812](https://github.com/hyperf/hyperf/pull/812) Added singleton crontab task support. - [#832](https://github.com/hyperf/hyperf/pull/832) Added `Hyperf\Utils\Codec\Json`. - [#833](https://github.com/hyperf/hyperf/pull/833) Added `Hyperf\Utils\Backoff`. +- [#859](https://github.com/hyperf/hyperf/pull/859) Added Consul cluster mode support, now available to fetch the service information from Consul cluster. ## Fixed @@ -17,6 +18,7 @@ - [#832](https://github.com/hyperf/hyperf/pull/832) Optimized that response will throw a exception when json format failed. - [#840](https://github.com/hyperf/hyperf/pull/840) Use swoole timer function from namespace. +- [#859](https://github.com/hyperf/hyperf/pull/859) Optimized the logical of fetch health nodes infomation from consul. # v1.1.4 - 2019-10-31 From 16c0934241f34fcc4d03eb3a0064632c3e7c7530 Mon Sep 17 00:00:00 2001 From: huangzhhui Date: Thu, 7 Nov 2019 12:21:14 +0800 Subject: [PATCH 7/7] Format --- src/rpc-client/src/AbstractServiceClient.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/rpc-client/src/AbstractServiceClient.php b/src/rpc-client/src/AbstractServiceClient.php index 170b1f4417..76bbd6bd6e 100644 --- a/src/rpc-client/src/AbstractServiceClient.php +++ b/src/rpc-client/src/AbstractServiceClient.php @@ -12,7 +12,6 @@ namespace Hyperf\RpcClient; -use Hyperf\Consul\Agent; use Hyperf\Consul\Health; use Hyperf\Consul\HealthInterface; use Hyperf\Contract\ConfigInterface; @@ -203,7 +202,7 @@ protected function getNodesFromConsul(array $config): array $passing = true; $service = $node['Service'] ?? []; $checks = $node['Checks'] ?? []; - + foreach ($checks as $check) { $status = $check['Status'] ?? false; if ($status !== 'passing') { @@ -213,7 +212,7 @@ protected function getNodesFromConsul(array $config): array if ($passing) { $address = $service['Address'] ?? ''; - $port = (int)$service['Port'] ?? 0; + $port = (int) $service['Port'] ?? 0; // @TODO Get and set the weight property. $address && $port && $nodes[] = new Node($address, $port); }