Skip to content

Commit ff68b97

Browse files
committed
Merge branch 'master' into 3.2-merge
# Conflicts: # .github/workflows/test.yml # src/grpc-client/src/BaseClient.php
2 parents ba2e8c0 + 10f176b commit ff68b97

4 files changed

Lines changed: 96 additions & 7 deletions

File tree

src/CloudName.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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;
14+
15+
enum CloudName: string
16+
{
17+
case Aliyun = 'aliyun';
18+
19+
public static function safeFrom(mixed $value): ?CloudName
20+
{
21+
if ($value instanceof CloudName) {
22+
return $value;
23+
}
24+
25+
return CloudName::tryFrom($value);
26+
}
27+
}

src/Config.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class Config
3232

3333
protected ?string $version = '1.0';
3434

35+
protected ?CloudName $cloudName = null;
36+
3537
protected array $grpc = [
3638
'enable' => true,
3739
'heartbeat' => 10,
@@ -56,6 +58,7 @@ public function __construct(
5658
'port' => 'int',
5759
'grpc' => 'array',
5860
'version' => 'string',
61+
'cloud_name' => 'string',
5962
])]
6063
array $config = []
6164
) {
@@ -69,6 +72,7 @@ public function __construct(
6972
isset($config['port']) && $this->port = (int) $config['port'];
7073
isset($config['version']) && $this->version = (string) $config['version'];
7174
isset($config['grpc']) && $this->grpc = array_replace($this->grpc, $config['grpc']);
75+
isset($config['cloud_name']) && $this->cloudName = CloudName::safeFrom($config['cloud_name']);
7276
}
7377

7478
public function getBaseUri(): string
@@ -120,4 +124,9 @@ public function getVersion(): string
120124
{
121125
return $this->version;
122126
}
127+
128+
public function getCloudName(): ?CloudName
129+
{
130+
return $this->cloudName;
131+
}
123132
}

src/GrpcClient.php

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,31 @@ protected function serverCheck(): bool
315315
throw new ConnectToServerFailedException('the nacos server is not ready to work in 30 seconds, connect to server failed');
316316
}
317317

318-
private function isWorkerExit(): bool
318+
protected function getMetadata(RequestInterface $request): array
319319
{
320-
return CoordinatorManager::until(Constants::WORKER_EXIT)->isClosing();
320+
return match ($this->config->getCloudName()) {
321+
CloudName::Aliyun => $this->getAliyunMetadata($request),
322+
default => $this->getDefaultMetadata($request),
323+
};
321324
}
322325

323-
private function getMetadata(RequestInterface $request): array
326+
protected function getAliyunMetadata(RequestInterface $request): array
327+
{
328+
$accessKey = $this->config->getAccessKey();
329+
$accessSecret = $this->config->getAccessSecret();
330+
$signHeaders = $this->getMseSignHeaders($request, $accessSecret);
331+
332+
return [
333+
'type' => $request->getType(),
334+
'clientIp' => $this->ip(),
335+
'headers' => [
336+
'Spas-AccessKey' => $accessKey,
337+
...$signHeaders,
338+
],
339+
];
340+
}
341+
342+
protected function getDefaultMetadata(RequestInterface $request): array
324343
{
325344
if ($token = $this->getAccessToken()) {
326345
return [
@@ -338,6 +357,11 @@ private function getMetadata(RequestInterface $request): array
338357
];
339358
}
340359

360+
private function isWorkerExit(): bool
361+
{
362+
return CoordinatorManager::until(Constants::WORKER_EXIT)->isClosing();
363+
}
364+
341365
private function grpcDefaultHeaders(): array
342366
{
343367
return [
@@ -358,4 +382,33 @@ private function handleResponse(ResponseInterface $response): array
358382

359383
return Json::decode($contents);
360384
}
385+
386+
private function getMseSignHeaders(RequestInterface $request, string $secretKey): array
387+
{
388+
$data = $request->getValue();
389+
$group = $data['group'] ?? '';
390+
$tenant = $data['tenant'] ?? '';
391+
$timeStamp = (string) round(microtime(true) * 1000);
392+
$signStr = '';
393+
394+
if ($tenant) {
395+
$signStr .= "{$tenant}+";
396+
}
397+
398+
if ($group) {
399+
$signStr .= "{$group}+";
400+
}
401+
402+
$signStr .= "{$timeStamp}";
403+
404+
return [
405+
'charset' => 'utf-8',
406+
'exConfigInfo' => 'true',
407+
'Client-RequestToken' => md5($timeStamp),
408+
'Client-RequestTS' => $timeStamp,
409+
'Timestamp' => $timeStamp,
410+
'Spas-Signature' => base64_encode(hash_hmac('sha1', $signStr, $secretKey, true)),
411+
'Client-AppName' => '',
412+
];
413+
}
361414
}

src/Protobuf/Response/ConfigChangeNotifyRequest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ class ConfigChangeNotifyRequest extends Response implements Stringable, JsonSeri
2828

2929
public function __construct(array $json)
3030
{
31-
$this->requestId = $json['requestId'];
32-
$this->tenant = $json['tenant'];
33-
$this->group = $json['group'];
34-
$this->dataId = $json['dataId'];
31+
$this->requestId = $json['requestId'] ?? '';
32+
$this->tenant = $json['tenant'] ?? '';
33+
$this->group = $json['group'] ?? '';
34+
$this->dataId = $json['dataId'] ?? '';
3535
$this->json = $json;
3636
}
3737

0 commit comments

Comments
 (0)