Client.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\ConfigNacos;
  12. use Hyperf\Codec\Json;
  13. use Hyperf\Codec\Xml;
  14. use Hyperf\Contract\ConfigInterface;
  15. use Hyperf\Contract\StdoutLoggerInterface;
  16. use Hyperf\Nacos\Application;
  17. use Hyperf\Nacos\Exception\RequestException;
  18. use JetBrains\PhpStorm\ArrayShape;
  19. use Psr\Container\ContainerInterface;
  20. use Psr\Log\LoggerInterface;
  21. class Client implements ClientInterface
  22. {
  23. protected ConfigInterface $config;
  24. protected Application $client;
  25. protected LoggerInterface $logger;
  26. public function __construct(protected ContainerInterface $container)
  27. {
  28. $this->config = $container->get(ConfigInterface::class);
  29. $this->client = $container->get(NacosClient::class);
  30. $this->logger = $container->get(StdoutLoggerInterface::class);
  31. }
  32. public function getClient(): Application
  33. {
  34. return $this->client;
  35. }
  36. public function pull(): array
  37. {
  38. $listener = $this->config->get('config_center.drivers.nacos.listener_config', []);
  39. $config = [];
  40. foreach ($listener as $key => $item) {
  41. $dataId = $item['data_id'] ?? '';
  42. $group = $item['group'] ?? '';
  43. $tenant = $item['tenant'] ?? null;
  44. $type = $item['type'] ?? null;
  45. $response = $this->client->config->get($dataId, $group, $tenant);
  46. if ($response->getStatusCode() !== 200) {
  47. $this->logger->error(sprintf('The config of %s read failed from Nacos.', $key));
  48. continue;
  49. }
  50. $config[$key] = $this->decode((string) $response->getBody(), $type);
  51. }
  52. return $config;
  53. }
  54. public function decode(string $body, ?string $type = null): array|string
  55. {
  56. $type = strtolower((string) $type);
  57. return match ($type) {
  58. 'json' => Json::decode($body),
  59. 'yml', 'yaml' => yaml_parse($body),
  60. 'xml' => Xml::toArray($body),
  61. default => $body,
  62. };
  63. }
  64. public function getValidNodes(
  65. string $serviceName,
  66. #[ArrayShape([
  67. 'groupName' => 'string',
  68. 'namespaceId' => 'string',
  69. 'clusters' => 'string', // 集群名称(字符串,多个集群用逗号分隔)
  70. 'healthyOnly' => 'bool',
  71. ])]
  72. array $optional = []
  73. ): array {
  74. $response = $this->client->instance->list($serviceName, $optional);
  75. if ($response->getStatusCode() !== 200) {
  76. throw new RequestException((string) $response->getBody(), $response->getStatusCode());
  77. }
  78. $data = Json::decode((string) $response->getBody());
  79. $hosts = $data['hosts'] ?? [];
  80. return array_filter($hosts, function ($item) {
  81. return $item['valid'] ?? false;
  82. });
  83. }
  84. }