Client.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. switch ($type) {
  58. case 'json':
  59. return Json::decode($body);
  60. case 'yml':
  61. case 'yaml':
  62. return yaml_parse($body);
  63. case 'xml':
  64. return Xml::toArray($body);
  65. default:
  66. return $body;
  67. }
  68. }
  69. public function getValidNodes(
  70. string $serviceName,
  71. #[ArrayShape([
  72. 'groupName' => 'string',
  73. 'namespaceId' => 'string',
  74. 'clusters' => 'string', // 集群名称(字符串,多个集群用逗号分隔)
  75. 'healthyOnly' => 'bool',
  76. ])]
  77. array $optional = []
  78. ): array {
  79. $response = $this->client->instance->list($serviceName, $optional);
  80. if ($response->getStatusCode() !== 200) {
  81. throw new RequestException((string) $response->getBody(), $response->getStatusCode());
  82. }
  83. $data = Json::decode((string) $response->getBody());
  84. $hosts = $data['hosts'] ?? [];
  85. return array_filter($hosts, function ($item) {
  86. return $item['valid'] ?? false;
  87. });
  88. }
  89. }