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-23 11:14:35 +08:00
|
|
|
|
`MainWorkerStartListener.php` 將在系統啟動完成時自動完成 `實例註冊`,`服務註冊`
|
2020-07-27 15:52:51 +08:00
|
|
|
|
|
|
|
|
|
如果需要在服務下線時自動註銷服務,請增加如下配置,以監聽 `Shutdown` 事件
|
|
|
|
|
|
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
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
```
|
|
|
|
|
|
2021-06-23 11:14:35 +08:00
|
|
|
|
### 獲取服務的可用節點列表
|
2020-07-27 15:52:51 +08:00
|
|
|
|
|
|
|
|
|
```php
|
2020-08-10 13:28:28 +08:00
|
|
|
|
use Hyperf\Utils\ApplicationContext;
|
2021-06-23 11:14:35 +08:00
|
|
|
|
use Hyperf\Nacos\Client;
|
2020-08-10 13:28:28 +08:00
|
|
|
|
|
|
|
|
|
$container = ApplicationContext::getContainer();
|
2021-06-23 11:14:35 +08:00
|
|
|
|
$client = $container->get(Client::class);
|
2020-07-27 15:52:51 +08:00
|
|
|
|
|
2021-06-23 11:14:35 +08:00
|
|
|
|
$optimal = $client->getValidNodes('hyperf', [
|
|
|
|
|
'groupName' => 'api',
|
|
|
|
|
'namespaceId' => '5ce9d1c1-6732-4ccc-ae1f-5139af86a845'
|
2020-07-27 15:52:51 +08:00
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 配置中心
|
|
|
|
|
|
|
|
|
|
`MainWorkerStartListener.php` 系統啟動時將拉取遠程配置, 併合入`hyperf` 的 `Config`
|
|
|
|
|
|
|
|
|
|
`FetchConfigProcess.php` 自定義進程將監聽配置, 若有更新將發送 `PipeMessage` 到各服務`worker` 進程, 併合入當前進程的 `Config`
|
|
|
|
|
|
|
|
|
|
如果服務如下配置
|
2021-06-23 11:14:35 +08:00
|
|
|
|
|
2020-07-27 15:52:51 +08:00
|
|
|
|
```php
|
|
|
|
|
// config/autoload/nacos.php
|
|
|
|
|
|
|
|
|
|
return [
|
2021-06-23 11:14:35 +08:00
|
|
|
|
'host' => '127.0.0.1',
|
|
|
|
|
'port' => 8848,
|
|
|
|
|
'username' => null,
|
|
|
|
|
'password' => null,
|
|
|
|
|
'config' => [
|
|
|
|
|
// 是否開啟配置中心
|
|
|
|
|
'enable' => true,
|
|
|
|
|
// 合併模式
|
|
|
|
|
'merge_mode' => Constants::CONFIG_MERGE_OVERWRITE,
|
|
|
|
|
// 配置讀取間隔
|
|
|
|
|
'reload_interval' => 3,
|
|
|
|
|
// 默認的配置 KEY 值
|
|
|
|
|
'default_key' => 'nacos_config',
|
|
|
|
|
'listener_config' => [
|
|
|
|
|
// $key => $config
|
|
|
|
|
// 不設置 key 時,則使用 default_key 配置的 key
|
|
|
|
|
'nacos_config' => [
|
|
|
|
|
'tenant' => 'tenant',
|
|
|
|
|
'data_id' => 'json',
|
|
|
|
|
'group' => 'DEFAULT_GROUP',
|
|
|
|
|
'type' => 'json',
|
|
|
|
|
],
|
|
|
|
|
'nacos_config.data' => [
|
|
|
|
|
'data_id' => 'text',
|
|
|
|
|
'group' => 'DEFAULT_GROUP',
|
|
|
|
|
],
|
2020-07-27 15:52:51 +08:00
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
```
|
|
|
|
|
|
2021-06-23 11:14:35 +08:00
|
|
|
|
系統將自動監聽 `listener_config` 中的配置,並將其合併到對應的節點中,例如上述的 `nacos_config` ,可以用`config('nacos_config.***')`
|
|
|
|
|
獲取,若沒有配置 `$key` 項,將會併入 `default_key` 節點。
|