Skip to content

Commit 4e2833c

Browse files
authored
Support versions v2 and v3 for nacos sdk. (#7467)
1 parent 6fac00e commit 4e2833c

14 files changed

+919
-3
lines changed

src/Application.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Application
4040

4141
protected array $providers = [];
4242

43-
public function __construct(protected Config $config)
43+
public function __construct(protected Config $conf)
4444
{
4545
}
4646

@@ -54,7 +54,19 @@ public function __get($name)
5454
return $this->providers[$name];
5555
}
5656

57-
$class = $this->alias[$name];
58-
return $this->providers[$name] = new $class($this, $this->config);
57+
$class = $this->resolveVersionClass($this->alias[$name]);
58+
return $this->providers[$name] = new $class($this, $this->conf);
59+
}
60+
61+
public function resolveVersionClass(string $defaultClass): string
62+
{
63+
[$major] = array_pad(explode('.', $this->conf->getVersion()), 2, 0);
64+
$classParts = explode('\\', $defaultClass);
65+
$className = array_pop($classParts);
66+
$classParts[] = 'V' . $major;
67+
$classParts[] = $className;
68+
$class = implode('\\', $classParts);
69+
70+
return class_exists($class) ? $class : $defaultClass;
5971
}
6072
}

src/ApplicationFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function __invoke(ContainerInterface $container)
3535
'guzzle_config' => $config['guzzle']['config'] ?? null,
3636
'host' => $config['host'] ?? null,
3737
'port' => $config['port'] ?? null,
38+
'version' => $config['version'] ?? null,
3839
]));
3940
}
4041
}

src/Config.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class Config
3030

3131
protected int $port = 8848;
3232

33+
protected ?string $version = '1.0';
34+
3335
protected array $grpc = [
3436
'enable' => true,
3537
'heartbeat' => 10,
@@ -52,6 +54,8 @@ public function __construct(
5254
'guzzle_config' => 'array',
5355
'host' => 'string',
5456
'port' => 'int',
57+
'grpc' => 'array',
58+
'version' => 'string',
5559
])]
5660
array $config = []
5761
) {
@@ -63,6 +67,7 @@ public function __construct(
6367
isset($config['guzzle_config']) && $this->guzzleConfig = (array) $config['guzzle_config'];
6468
isset($config['host']) && $this->host = (string) $config['host'];
6569
isset($config['port']) && $this->port = (int) $config['port'];
70+
isset($config['version']) && $this->version = (string) $config['version'];
6671
isset($config['grpc']) && $this->grpc = array_replace($this->grpc, $config['grpc']);
6772
}
6873

@@ -110,4 +115,9 @@ public function getGrpc(): array
110115
{
111116
return $this->grpc;
112117
}
118+
119+
public function getVersion(): string
120+
{
121+
return $this->version;
122+
}
113123
}

src/Provider/V2/AuthProvider.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 [email protected]
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\Nacos\AbstractProvider;
17+
use Psr\Http\Message\ResponseInterface;
18+
19+
class AuthProvider extends AbstractProvider
20+
{
21+
public function login(string $username, string $password): ResponseInterface
22+
{
23+
return $this->client()->request('POST', 'nacos/v2/auth/user/login', [
24+
RequestOptions::QUERY => [
25+
'username' => $username,
26+
],
27+
RequestOptions::FORM_PARAMS => [
28+
'password' => $password,
29+
],
30+
]);
31+
}
32+
}

src/Provider/V2/ConfigProvider.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 [email protected]
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\Nacos\AbstractProvider;
17+
use JetBrains\PhpStorm\ArrayShape;
18+
use Psr\Http\Message\ResponseInterface;
19+
20+
class ConfigProvider extends AbstractProvider
21+
{
22+
public const WORD_SEPARATOR = "\x02";
23+
24+
public const LINE_SEPARATOR = "\x01";
25+
26+
public function get(string $dataId, string $group, ?string $tenant = null): ResponseInterface
27+
{
28+
return $this->request('GET', 'nacos/v2/cs/config', [
29+
RequestOptions::QUERY => $this->filter([
30+
'dataId' => $dataId,
31+
'group' => $group,
32+
'tenant' => $tenant,
33+
]),
34+
]);
35+
}
36+
37+
public function set(string $dataId, string $group, string $content, ?string $type = null, ?string $tenant = null): ResponseInterface
38+
{
39+
return $this->request('POST', 'nacos/v2/cs/config', [
40+
RequestOptions::FORM_PARAMS => $this->filter([
41+
'dataId' => $dataId,
42+
'group' => $group,
43+
'tenant' => $tenant,
44+
'type' => $type,
45+
'content' => $content,
46+
]),
47+
]);
48+
}
49+
50+
public function delete(string $dataId, string $group, ?string $tenant = null): ResponseInterface
51+
{
52+
return $this->request('DELETE', 'nacos/v2/cs/config', [
53+
RequestOptions::QUERY => $this->filter([
54+
'dataId' => $dataId,
55+
'group' => $group,
56+
'tenant' => $tenant,
57+
]),
58+
]);
59+
}
60+
61+
public function listener(
62+
#[ArrayShape([
63+
'dataId' => 'string',
64+
'group' => 'string',
65+
'contentMD5' => 'string', // md5(file_get_contents($configPath))
66+
'tenant' => 'string',
67+
])]
68+
array $options = []
69+
): ResponseInterface {
70+
$config = ($options['dataId'] ?? null) . self::WORD_SEPARATOR
71+
. ($options['group'] ?? null) . self::WORD_SEPARATOR
72+
. ($options['contentMD5'] ?? null) . self::WORD_SEPARATOR
73+
. ($options['tenant'] ?? null) . self::LINE_SEPARATOR;
74+
return $this->request('POST', 'nacos/v2/cs/config/listener', [
75+
RequestOptions::QUERY => [
76+
'Listening-Configs' => $config,
77+
],
78+
RequestOptions::HEADERS => [
79+
'Long-Pulling-Timeout' => 30,
80+
],
81+
]);
82+
}
83+
}
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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 [email protected]
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

Comments
 (0)