NacosGrpcDriver.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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\ServiceGovernanceNacos;
  12. use Hyperf\Codec\Json;
  13. use Hyperf\Contract\ConfigInterface;
  14. use Hyperf\Contract\StdoutLoggerInterface;
  15. use Hyperf\Coordinator\Constants;
  16. use Hyperf\Coordinator\CoordinatorManager;
  17. use Hyperf\Coroutine\Coroutine;
  18. use Hyperf\Engine\Channel;
  19. use Hyperf\LoadBalancer\Node;
  20. use Hyperf\Nacos\Exception\RequestException;
  21. use Hyperf\Nacos\Module;
  22. use Hyperf\Nacos\Protobuf\ListenHandler\NamingPushRequestHandler;
  23. use Hyperf\Nacos\Protobuf\Message\Instance;
  24. use Hyperf\Nacos\Protobuf\Request\InstanceRequest;
  25. use Hyperf\Nacos\Protobuf\Request\NamingRequest;
  26. use Hyperf\Nacos\Protobuf\Request\ServiceQueryRequest;
  27. use Hyperf\Nacos\Protobuf\Request\SubscribeServiceRequest;
  28. use Hyperf\Nacos\Protobuf\Response\NotifySubscriberRequest;
  29. use Hyperf\Nacos\Protobuf\Response\SubscribeServiceResponse;
  30. use Hyperf\ServiceGovernance\DriverInterface;
  31. use Hyperf\ServiceGovernance\Exception\RegisterInstanceException;
  32. use InvalidArgumentException;
  33. use Psr\Container\ContainerInterface;
  34. use Psr\Http\Message\ResponseInterface;
  35. use Psr\Log\LoggerInterface;
  36. use Throwable;
  37. use function Hyperf\Support\retry;
  38. class NacosGrpcDriver implements DriverInterface
  39. {
  40. protected Client $client;
  41. protected LoggerInterface $logger;
  42. protected ConfigInterface $config;
  43. protected array $serviceRegistered = [];
  44. protected array $serviceCreated = [];
  45. protected array $registerHeartbeat = [];
  46. private array $metadata = [];
  47. private Channel $nodeChannel;
  48. private array $nodes = [];
  49. private bool $listening = false;
  50. public function __construct(protected ContainerInterface $container)
  51. {
  52. $this->client = $container->get(Client::class);
  53. $this->logger = $container->get(StdoutLoggerInterface::class);
  54. $this->config = $container->get(ConfigInterface::class);
  55. $this->nodeChannel = new Channel(1);
  56. }
  57. public function isLongPolling(): bool
  58. {
  59. return true;
  60. }
  61. public function getNodes(string $uri, string $name, array $metadata): array
  62. {
  63. if (! $this->listening) {
  64. $namespaceId = $this->config->get('services.drivers.nacos.namespace_id');
  65. $groupName = $this->config->get('services.drivers.nacos.group_name');
  66. $cluster = $this->config->get('services.drivers.nacos.cluster', 'DEFAULT');
  67. $client = $this->client->grpc->get($namespaceId, Module::NAMING);
  68. $client->listenNaming($cluster, $groupName, $name, new NamingPushRequestHandler(function (NotifySubscriberRequest $request) {
  69. $nodes = [];
  70. foreach ($request->serviceInfo->hosts as $host) {
  71. if ($host->enabled && $host->healthy) {
  72. $nodes[] = [
  73. 'host' => $host->ip,
  74. 'port' => $host->port,
  75. 'weight' => $this->getWeight($host->weight),
  76. 'path_prefix' => $host->metadata['path_prefix'] ?? null,
  77. ];
  78. }
  79. }
  80. $this->nodes = $nodes;
  81. $chan = $this->nodeChannel;
  82. $this->nodeChannel = new Channel(1);
  83. $chan->close();
  84. }));
  85. }
  86. /** @var Node[] $nodes */
  87. $nodes = $metadata['nodes'] ?? [];
  88. $isChanged = $this->isChanged($nodes);
  89. if ($this->nodes && $isChanged) {
  90. return $this->nodes;
  91. }
  92. $this->nodeChannel->pop(60);
  93. return $this->nodes;
  94. }
  95. public function register(string $name, string $host, int $port, array $metadata): void
  96. {
  97. $namespaceId = $this->config->get('services.drivers.nacos.namespace_id');
  98. $groupName = $this->config->get('services.drivers.nacos.group_name');
  99. $cluster = $this->config->get('services.drivers.nacos.cluster', 'DEFAULT');
  100. $ephemeral = (bool) $this->config->get('services.drivers.nacos.ephemeral');
  101. $this->setMetadata($name, $metadata);
  102. if (! $ephemeral) {
  103. throw new InvalidArgumentException('nacos grpc driver only support ephemeral.');
  104. }
  105. $client = $this->client->grpc->get($namespaceId, Module::NAMING);
  106. $res = $client->request(new InstanceRequest(
  107. new NamingRequest($name, $groupName, $namespaceId),
  108. new Instance($host, $port, 0, true, true, $cluster, $ephemeral, $metadata),
  109. InstanceRequest::TYPE_REGISTER
  110. ));
  111. if (! $res->success) {
  112. throw new RegisterInstanceException('Register instance failed. The response is ' . $res);
  113. }
  114. $client->request(new ServiceQueryRequest(
  115. new NamingRequest($name, $groupName, $namespaceId),
  116. $cluster,
  117. false,
  118. 0
  119. ));
  120. $this->serviceRegistered[$name] = true;
  121. $this->registerHeartbeat($name, $host, $port);
  122. }
  123. public function isRegistered(string $name, string $host, int $port, array $metadata): bool
  124. {
  125. if (array_key_exists($name, $this->serviceRegistered)) {
  126. return true;
  127. }
  128. $namespaceId = $this->config->get('services.drivers.nacos.namespace_id');
  129. $groupName = $this->config->get('services.drivers.nacos.group_name');
  130. $cluster = $this->config->get('services.drivers.nacos.cluster', 'DEFAULT');
  131. $this->setMetadata($name, $metadata);
  132. $client = $this->client->grpc->get($namespaceId, Module::NAMING);
  133. /** @var SubscribeServiceResponse $response */
  134. $response = $client->request(new SubscribeServiceRequest(
  135. new NamingRequest(
  136. $name,
  137. $groupName,
  138. $namespaceId,
  139. ),
  140. ));
  141. if ($response->errorCode !== 0) {
  142. $this->logger->error((string) $response);
  143. throw new RequestException(sprintf('Failed to get nacos service %s!', $name), $response->errorCode);
  144. }
  145. $this->serviceCreated[$name] = true;
  146. $service = $response->service;
  147. $instances = $service->hosts;
  148. foreach ($instances as $instance) {
  149. if ($instance->ip === $host && $instance->port === $port) {
  150. $this->serviceRegistered[$name] = true;
  151. $this->registerHeartbeat($name, $host, $port);
  152. return true;
  153. }
  154. }
  155. return false;
  156. }
  157. /**
  158. * @param Node[] $nodes
  159. */
  160. protected function isChanged(array $nodes): bool
  161. {
  162. $now = [];
  163. foreach ($nodes as $node) {
  164. $now[] = $node->host . ':' . $node->port . ':' . $node->weight . ':' . $node->pathPrefix;
  165. }
  166. $assert = [];
  167. foreach ($this->nodes as $node) {
  168. $assert[] = $node['host'] . ':' . $node['port'] . ':' . $node['weight'] . ':' . $node['path_prefix'];
  169. }
  170. return Json::encode($now) !== Json::encode($assert);
  171. }
  172. protected function isNoIpsFound(ResponseInterface $response): bool
  173. {
  174. if ($response->getStatusCode() === 404) {
  175. return true;
  176. }
  177. if ($response->getStatusCode() === 500) {
  178. $messages = [
  179. 'no ips found',
  180. 'no matched ip',
  181. ];
  182. $body = (string) $response->getBody();
  183. foreach ($messages as $message) {
  184. if (str_contains($body, $message)) {
  185. return true;
  186. }
  187. }
  188. }
  189. return false;
  190. }
  191. protected function setMetadata(string $name, array $metadata)
  192. {
  193. $this->metadata[$name] = $metadata;
  194. }
  195. protected function getMetadata(string $name): ?string
  196. {
  197. if (empty($this->metadata[$name])) {
  198. return null;
  199. }
  200. unset($this->metadata[$name]['methodName']);
  201. return Json::encode($this->metadata[$name]);
  202. }
  203. protected function registerHeartbeat(string $name, string $host, int $port): void
  204. {
  205. $key = $name . $host . $port;
  206. if (isset($this->registerHeartbeat[$key])) {
  207. return;
  208. }
  209. $this->registerHeartbeat[$key] = true;
  210. Coroutine::create(function () use ($name, $host, $port) {
  211. retry(INF, function () use ($name, $host, $port) {
  212. $lightBeatEnabled = false;
  213. $namespaceId = $this->config->get('services.drivers.nacos.namespace_id');
  214. $groupName = $this->config->get('services.drivers.nacos.group_name');
  215. $cluster = $this->config->get('services.drivers.nacos.cluster', 'DEFAULT');
  216. $ephemeral = (bool) $this->config->get('services.drivers.nacos.ephemeral');
  217. while (true) {
  218. try {
  219. $heartbeat = $this->config->get('services.drivers.nacos.heartbeat', 5);
  220. if (CoordinatorManager::until(Constants::WORKER_EXIT)->yield($heartbeat)) {
  221. break;
  222. }
  223. $response = $this->client->instance->beat(
  224. $name,
  225. [
  226. 'ip' => $host,
  227. 'port' => $port,
  228. 'serviceName' => $groupName . '@@' . $name,
  229. 'cluster' => $cluster,
  230. ],
  231. $groupName,
  232. $namespaceId,
  233. $ephemeral,
  234. $lightBeatEnabled
  235. );
  236. $result = Json::decode((string) $response->getBody());
  237. if ($response->getStatusCode() === 200) {
  238. $this->logger->debug(sprintf('Instance %s:%d heartbeat successfully, result code:%s', $host, $port, $result['code']));
  239. } else {
  240. $this->logger->error(sprintf('Instance %s:%d heartbeat failed! %s', $host, $port, (string) $response->getBody()));
  241. continue;
  242. }
  243. $lightBeatEnabled = false;
  244. if (isset($result['lightBeatEnabled'])) {
  245. $lightBeatEnabled = $result['lightBeatEnabled'];
  246. }
  247. if ($result['code'] == 20404) {
  248. $this->client->instance->register($host, $port, $name, [
  249. 'groupName' => $this->config->get('services.drivers.nacos.group_name'),
  250. 'namespaceId' => $this->config->get('services.drivers.nacos.namespace_id'),
  251. 'metadata' => $this->getMetadata($name),
  252. ]);
  253. }
  254. } catch (Throwable $exception) {
  255. $this->logger->error('The nacos heartbeat failed, caused by ' . $exception);
  256. throw $exception;
  257. }
  258. }
  259. });
  260. });
  261. }
  262. private function getWeight($weight): int
  263. {
  264. return intval(100 * $weight);
  265. }
  266. }