2020-07-27 15:52:51 +08:00
|
|
|
|
# Nacos
|
|
|
|
|
|
|
|
|
|
一個 `Nacos` 的 `PHP` 協程客戶端,與 `Hyperf` 的配置中心、微服務治理完美結合。
|
|
|
|
|
|
|
|
|
|
## 安裝
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
composer require hyperf/nacos
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 釋出配置檔案
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
php bin/hyperf.php vendor:publish hyperf/nacos
|
|
|
|
|
```
|
|
|
|
|
|
2021-06-27 16:37:24 +08:00
|
|
|
|
```php
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
// 無法使用 IP 埠形式的開發者,直接配置 url 即可
|
|
|
|
|
// 'url' => '',
|
|
|
|
|
'host' => '127.0.0.1',
|
|
|
|
|
'port' => 8848,
|
|
|
|
|
'username' => null,
|
|
|
|
|
'password' => null,
|
|
|
|
|
'guzzle' => [
|
2021-09-06 10:50:20 +08:00
|
|
|
|
'config' => null,
|
2021-06-27 16:37:24 +08:00
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2020-07-27 15:52:51 +08:00
|
|
|
|
## 服務與例項
|
|
|
|
|
|
2021-06-27 16:37:24 +08:00
|
|
|
|
當前元件仍然保留了之前提供的服務註冊功能。
|
|
|
|
|
|
|
|
|
|
只需要安裝 `hyperf/service-governance-nacos` 元件,然後配置以下監聽器和自定義程序即可。
|
2020-07-27 15:52:51 +08:00
|
|
|
|
|
2021-06-27 16:37:24 +08:00
|
|
|
|
`Hyperf\ServiceGovernanceNacos\Listener\MainWorkerStartListener`
|
|
|
|
|
`Hyperf\ServiceGovernanceNacos\Listener\OnShutdownListener`
|
2021-07-20 16:50:40 +08:00
|
|
|
|
`Hyperf\ServiceGovernanceNacos\Process\InstanceBeatProcess`
|
2021-06-27 16:37:24 +08:00
|
|
|
|
|
|
|
|
|
然後增加如下配置,以監聽 `Shutdown` 事件
|
2020-07-27 15:52:51 +08:00
|
|
|
|
|
2021-06-23 11:14:35 +08:00
|
|
|
|
- config/autoload/server.php
|
2020-07-27 15:52:51 +08:00
|
|
|
|
|
2021-06-23 11:14:35 +08:00
|
|
|
|
```php
|
|
|
|
|
<?php
|
|
|
|
|
use Hyperf\Server\Event;
|
2020-07-27 15:52:51 +08:00
|
|
|
|
return [
|
|
|
|
|
// ...other
|
|
|
|
|
'callbacks' => [
|
|
|
|
|
// ...other
|
2021-02-19 12:53:50 +08:00
|
|
|
|
Event::ON_SHUTDOWN => [Hyperf\Framework\Bootstrap\ShutdownCallback::class, 'onShutdown']
|
2020-07-27 15:52:51 +08:00
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-18 06:40:54 +08:00
|
|
|
|
## 阿里雲服務鑑權
|
|
|
|
|
|
|
|
|
|
當使用阿里雲的 Nacos 服務時,可能需要使用 AK 和 SK 鑑權,Nacos 元件對其進行了原生支援,我們可以方便的增加對應配置,如下:
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
// nacos server url like https://nacos.hyperf.io, Priority is higher than host:port
|
|
|
|
|
// 'uri' => 'http://127.0.0.1:8848/',
|
|
|
|
|
// The nacos host info
|
|
|
|
|
'host' => '127.0.0.1',
|
|
|
|
|
'port' => 8848,
|
|
|
|
|
// The nacos account info
|
|
|
|
|
'username' => null,
|
|
|
|
|
'password' => null,
|
|
|
|
|
'access_key' => 'xxxx',
|
|
|
|
|
'access_secret' => 'yyyy',
|
|
|
|
|
'guzzle' => [
|
|
|
|
|
'config' => null,
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
```
|
|
|
|
|
|