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