Skip to content

Commit

Permalink
Merge pull request #859 from crystal9002/master
Browse files Browse the repository at this point in the history
#815 rpc client通过consul筛选健康的服务节点不适用集群环境
  • Loading branch information
huangzhhui committed Nov 7, 2019
2 parents f40e7d4 + 17bd837 commit 0f48a8c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 35 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand All @@ -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

Expand Down
54 changes: 19 additions & 35 deletions src/rpc-client/src/AbstractServiceClient.php
Expand Up @@ -12,7 +12,6 @@

namespace Hyperf\RpcClient;

use Hyperf\Consul\Agent;
use Hyperf\Consul\Health;
use Hyperf\Consul\HealthInterface;
use Hyperf\Contract\ConfigInterface;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0f48a8c

Please sign in to comment.