Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#815 rpc client通过consul筛选健康的服务节点不适用集群环境 #859

Merged
merged 8 commits into from Nov 7, 2019
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 19 additions & 34 deletions src/rpc-client/src/AbstractServiceClient.php
Expand Up @@ -196,44 +196,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