Fixed bug that the metadata of nacos instance can't be registered successfully. (#3843)

* Check the status code and body of the response to ensure whether the instance already be registered.

Co-authored-by: 李铭昕 <715557344@qq.com>
This commit is contained in:
alexzy228 2021-07-25 21:36:31 +08:00 committed by GitHub
parent 28b73036e4
commit 9331644273
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 7 deletions

View File

@ -5,6 +5,11 @@
- [#3828](https://github.com/hyperf/hyperf/pull/3828) Fixed bug that lazy inject does not work for `Hyperf\Redis\Redis` in `PHP8.0`.
- [#3845](https://github.com/hyperf/hyperf/pull/3845) Fixed bug that `watcher` does not work for `v2.2`.
- [#3848](https://github.com/hyperf/hyperf/pull/3848) Fixed bug that the usage of registering itself like `nacos v2.1` does not work.
- [#3843](https://github.com/hyperf/hyperf/pull/3843) Fixed bug that the metadata of nacos instance can't be registered successfully.
## Optimized
- [#3843](https://github.com/hyperf/hyperf/pull/3843) Check the status code and body of the response to ensure whether the instance already be registered.
# v2.2.0 - 2021-07-19

View File

@ -20,6 +20,7 @@ use Hyperf\Utils\Coordinator\Constants;
use Hyperf\Utils\Coordinator\CoordinatorManager;
use Hyperf\Utils\Coroutine;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
class NacosDriver implements DriverInterface
{
@ -87,7 +88,7 @@ class NacosDriver implements DriverInterface
$response = $this->client->service->create($name, [
'groupName' => $this->config->get('services.drivers.nacos.group_name'),
'namespaceId' => $this->config->get('services.drivers.nacos.namespace_id'),
'metadata' => $metadata,
'metadata' => $this->formatMetadata($metadata),
]);
if ($response->getStatusCode() !== 200 || (string) $response->getBody() !== 'ok') {
@ -98,9 +99,9 @@ class NacosDriver implements DriverInterface
}
$response = $this->client->instance->register($host, $port, $name, [
'metadata' => $metadata,
'groupName' => $this->config->get('services.drivers.nacos.group_name'),
'namespaceId' => $this->config->get('services.drivers.nacos.namespace_id'),
'metadata' => $this->formatMetadata($metadata),
]);
if ($response->getStatusCode() !== 200 || (string) $response->getBody() !== 'ok') {
@ -138,11 +139,7 @@ class NacosDriver implements DriverInterface
'namespaceId' => $this->config->get('services.drivers.nacos.namespace_id'),
]);
if ($response->getStatusCode() === 404) {
return false;
}
if ($response->getStatusCode() === 500 && strpos((string) $response->getBody(), 'no ips found') > 0) {
if ($this->isNoIpsFound($response)) {
return false;
}
@ -155,6 +152,42 @@ class NacosDriver implements DriverInterface
return true;
}
protected function isNoIpsFound(ResponseInterface $response): bool
{
if ($response->getStatusCode() === 404) {
return true;
}
if ($response->getStatusCode() === 500) {
$messages = [
'no ips found',
'no matched ip',
];
$body = (string) $response->getBody();
foreach ($messages as $message) {
if (strpos($body, $message) !== false) {
return true;
}
}
}
return false;
}
protected function formatMetadata(array $metadata): ?string
{
if (empty($metadata)) {
return null;
}
$result = '';
foreach ($metadata as $k => $v) {
$result .= $k . '=' . $v;
}
return urlencode($result);
}
protected function registerHeartbeat(string $name, string $host, int $port): void
{
Coroutine::create(function () use ($name, $host, $port) {