diff --git a/CHANGELOG.md b/CHANGELOG.md index d4e4397c5c..91629217bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - [#833](https://github.com/hyperf/hyperf/pull/833) Added `Hyperf\Utils\Backoff`. - [#852](https://github.com/hyperf/hyperf/pull/852) Added a `clear()` method for `Hyperf\Utils\Parallel` to clear added callbacks. - [#854](https://github.com/hyperf/hyperf/pull/854) Added `GraphQLMiddleware`. +- [#859](https://github.com/hyperf/hyperf/pull/859) Added Consul cluster mode support, now available to fetch the service information from Consul cluster. - [#873](https://github.com/hyperf/hyperf/pull/873) Added redis cluster. ## Fixed @@ -23,6 +24,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::*` to instead of `swoole_timer_*` functions. - [#851](https://github.com/hyperf/hyperf/pull/851) Cache buckets as local variables to avoid extra hop to redis. +- [#859](https://github.com/hyperf/hyperf/pull/859) Optimized the logical of fetch health nodes infomation from consul. # v1.1.4 - 2019-10-31 diff --git a/src/rpc-client/src/AbstractServiceClient.php b/src/rpc-client/src/AbstractServiceClient.php index 8050074607..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; @@ -196,44 +195,29 @@ 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; - } - if ($check['Status'] !== 'passing') { - unset($nodes[$check['ServiceID']]); + $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; + } } - } - return array_values($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.'); + if ($passing) { + $address = $service['Address'] ?? ''; + $port = (int) $service['Port'] ?? 0; + // @TODO Get and set the weight property. + $address && $port && $nodes[] = new Node($address, $port); + } } - return make(Agent::class, [ - 'clientFactory' => function () use ($config) { - return $this->container->get(ClientFactory::class)->create([ - 'base_uri' => $config['address'] ?? Agent::DEFAULT_URI, - ]); - }, - ]); + return $nodes; } protected function createConsulHealth(array $config): HealthInterface