InstanceProvider.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\Nacos\Provider;
  12. use GuzzleHttp\RequestOptions;
  13. use Hyperf\Codec\Json;
  14. use Hyperf\Nacos\AbstractProvider;
  15. use JetBrains\PhpStorm\ArrayShape;
  16. use Psr\Http\Message\ResponseInterface;
  17. class InstanceProvider extends AbstractProvider
  18. {
  19. public function register(
  20. string $ip,
  21. int $port,
  22. string $serviceName,
  23. #[ArrayShape([
  24. 'groupName' => '',
  25. 'clusterName' => '',
  26. 'namespaceId' => '',
  27. 'weight' => 99.0,
  28. 'metadata' => '',
  29. 'enabled' => true,
  30. 'ephemeral' => false, // 是否临时实例
  31. ])]
  32. array $optional = []
  33. ): ResponseInterface {
  34. return $this->request('POST', 'nacos/v1/ns/instance', [
  35. RequestOptions::QUERY => $this->filter(array_merge($optional, [
  36. 'serviceName' => $serviceName,
  37. 'ip' => $ip,
  38. 'port' => $port,
  39. ])),
  40. ]);
  41. }
  42. public function delete(
  43. string $serviceName,
  44. string $groupName,
  45. string $ip,
  46. int $port,
  47. #[ArrayShape([
  48. 'clusterName' => '',
  49. 'namespaceId' => '',
  50. 'ephemeral' => false,
  51. ])]
  52. array $optional = []
  53. ): ResponseInterface {
  54. return $this->request('DELETE', 'nacos/v1/ns/instance', [
  55. RequestOptions::QUERY => $this->filter(array_merge($optional, [
  56. 'serviceName' => $serviceName,
  57. 'groupName' => $groupName,
  58. 'ip' => $ip,
  59. 'port' => $port,
  60. ])),
  61. ]);
  62. }
  63. public function update(
  64. string $ip,
  65. int $port,
  66. string $serviceName,
  67. #[ArrayShape([
  68. 'groupName' => '',
  69. 'clusterName' => '',
  70. 'namespaceId' => '',
  71. 'weight' => 0.99,
  72. 'metadata' => '', // json
  73. 'enabled' => false,
  74. 'ephemeral' => false,
  75. ])]
  76. array $optional = []
  77. ): ResponseInterface {
  78. return $this->request('PUT', 'nacos/v1/ns/instance', [
  79. RequestOptions::QUERY => $this->filter(array_merge($optional, [
  80. 'serviceName' => $serviceName,
  81. 'ip' => $ip,
  82. 'port' => $port,
  83. ])),
  84. ]);
  85. }
  86. public function list(
  87. string $serviceName,
  88. #[ArrayShape([
  89. 'groupName' => '',
  90. 'namespaceId' => '',
  91. 'clusters' => '', // 集群名称(字
  92. 'healthyOnly' => false,
  93. ])]
  94. array $optional = []
  95. ): ResponseInterface {
  96. return $this->request('GET', 'nacos/v1/ns/instance/list', [
  97. RequestOptions::QUERY => $this->filter(array_merge($optional, [
  98. 'serviceName' => $serviceName,
  99. ])),
  100. ]);
  101. }
  102. public function detail(
  103. string $ip,
  104. int $port,
  105. string $serviceName,
  106. #[ArrayShape([
  107. 'groupName' => '',
  108. 'namespaceId' => '',
  109. 'cluster' => '',
  110. 'healthyOnly' => false,
  111. 'ephemeral' => false,
  112. ])]
  113. array $optional = []
  114. ): ResponseInterface {
  115. return $this->request('GET', 'nacos/v1/ns/instance', [
  116. RequestOptions::QUERY => $this->filter(array_merge($optional, [
  117. 'ip' => $ip,
  118. 'port' => $port,
  119. 'serviceName' => $serviceName,
  120. ])),
  121. ]);
  122. }
  123. public function beat(
  124. string $serviceName,
  125. #[ArrayShape([
  126. 'ip' => '',
  127. 'port' => 9501,
  128. 'serviceName' => '',
  129. 'cluster' => '',
  130. 'weight' => 1,
  131. ])]
  132. array $beat = [],
  133. ?string $groupName = null,
  134. ?string $namespaceId = null,
  135. ?bool $ephemeral = null,
  136. bool $lightBeatEnabled = false
  137. ): ResponseInterface {
  138. return $this->request('PUT', 'nacos/v1/ns/instance/beat', [
  139. RequestOptions::QUERY => $this->filter([
  140. 'serviceName' => $serviceName,
  141. 'ip' => $beat['ip'] ?? null,
  142. 'port' => $beat['port'] ?? null,
  143. 'groupName' => $groupName,
  144. 'namespaceId' => $namespaceId,
  145. 'ephemeral' => $ephemeral,
  146. 'beat' => ! $lightBeatEnabled ? Json::encode($beat) : '',
  147. ]),
  148. ]);
  149. }
  150. public function updateHealth(
  151. string $ip,
  152. int $port,
  153. string $serviceName,
  154. bool $healthy,
  155. #[ArrayShape([
  156. 'namespaceId' => '',
  157. 'groupName' => '',
  158. 'clusterName' => '',
  159. ])]
  160. array $optional = []
  161. ): ResponseInterface {
  162. return $this->request('PUT', 'nacos/v1/ns/health/instance', [
  163. RequestOptions::QUERY => $this->filter(array_merge($optional, [
  164. 'ip' => $ip,
  165. 'port' => $port,
  166. 'serviceName' => $serviceName,
  167. 'healthy' => $healthy,
  168. ])),
  169. ]);
  170. }
  171. }