hyperf/docs/zh-cn/nacos.md

131 lines
3.3 KiB
Markdown
Raw Normal View History

2020-07-20 15:17:27 +08:00
# Nacos
2020-07-20 00:19:59 +08:00
2020-07-20 15:17:27 +08:00
一个 `Nacos``PHP` 协程客户端,与 `Hyperf` 的配置中心、微服务治理完美结合。
2020-04-11 21:35:14 +08:00
2020-07-20 15:17:27 +08:00
## 安装
2020-07-20 00:19:59 +08:00
2020-04-11 21:35:14 +08:00
```shell
2020-07-20 00:19:59 +08:00
composer require hyperf/nacos
2020-04-11 21:35:14 +08:00
```
2020-07-20 15:17:27 +08:00
### 发布配置文件
2020-04-11 21:35:14 +08:00
```shell
2020-07-20 10:53:09 +08:00
php bin/hyperf.php vendor:publish hyperf/nacos
2020-04-11 21:35:14 +08:00
```
2020-07-20 15:17:27 +08:00
### 目录结构
2020-07-20 00:19:59 +08:00
```
2020-04-11 21:35:14 +08:00
./src
2020-07-20 00:29:00 +08:00
├── Api
2020-04-11 21:35:14 +08:00
│   ├── AbstractNacos.php
│   ├── NacosConfig.php
│   ├── NacosInstance.php
│   ├── NacosOperator.php
│   └── NacosService.php
2020-07-20 00:29:00 +08:00
├── Client.php
├── Config
│   ├── FetchConfigProcess.php
│   ├── OnPipeMessageListener.php
│   └── PipeMessage.php
├── ConfigProvider.php
├── Contract
│   └── LoggerInterface.php
├── Exception
│   ├── InvalidArgumentException.php
│   ├── NacosThrowable.php
│   └── RuntimeException.php
├── Instance.php
├── Listener
│   ├── MainWorkerStartListener.php
2020-07-20 00:19:59 +08:00
│   └── OnShutdownListener.php
├── Model
2020-04-11 21:35:14 +08:00
│   ├── AbstractModel.php
│   ├── ConfigModel.php
│   ├── InstanceModel.php
│   └── ServiceModel.php
2020-07-20 00:19:59 +08:00
├── Process
2020-04-11 21:35:14 +08:00
│   └── InstanceBeatProcess.php
2020-07-20 00:29:00 +08:00
└── Service.php
2020-04-11 21:35:14 +08:00
```
2020-07-20 15:17:27 +08:00
## 服务与实例
2020-04-11 21:35:14 +08:00
2020-07-20 10:48:23 +08:00
`MainWorkerStartListener.php` 将在系统启动完成时自动完成 `实例注册``服务注册`
2020-04-11 21:35:14 +08:00
2020-07-20 00:19:59 +08:00
如果需要在服务下线时自动注销服务,请增加如下配置,以监听 `Shutdown` 事件
2020-04-11 21:35:14 +08:00
```php
// config/autoload/server.php
return [
// ...other
'callbacks' => [
// ...other
SwooleEvent::ON_SHUTDOWN => [Hyperf\Framework\Bootstrap\ShutdownCallback::class, 'onShutdown']
]
];
```
2020-07-20 15:17:27 +08:00
### 获取当前实例
2020-04-11 21:35:14 +08:00
```php
2020-07-20 00:29:00 +08:00
$instance = new \Hyperf\Nacos\Instance();
2020-04-11 21:35:14 +08:00
```
2020-07-20 15:17:27 +08:00
### 获取当前服务
2020-04-11 21:35:14 +08:00
```php
2020-07-20 00:29:00 +08:00
$service = new \Hyperf\Nacos\Service();
2020-04-11 21:35:14 +08:00
```
2020-07-20 15:17:27 +08:00
### 获取一个服务的最优节点
2020-04-11 21:35:14 +08:00
```php
$instance = make(NacosInstance::class);
$service = new ServiceModel([
2020-07-20 00:19:59 +08:00
'service_name' => 'hyperf',
'group_name' => 'api',
'namespace_id' => '5ce9d1c1-6732-4ccc-ae1f-5139af86a845'
2020-04-11 21:35:14 +08:00
]);
$optimal = $instance->getOptimal($service);
```
2020-07-20 15:17:27 +08:00
## 配置中心
2020-04-11 21:35:14 +08:00
2020-07-20 10:53:09 +08:00
`MainWorkerStartListener.php` 系统启动时将拉取远程配置, 并合入`hyperf` 的 `Config`
2020-04-11 21:35:14 +08:00
2020-07-20 15:17:27 +08:00
`FetchConfigProcess.php` 自定义进程将监听配置, 若有更新将发送 `PipeMessage` 到各服务`worker` 进程, 并合入当前进程的 `Config`
2020-04-11 21:35:14 +08:00
如果服务如下配置
```php
// config/autoload/nacos.php
return [
// ...other
2020-07-08 17:32:33 +08:00
'config_reload_interval' => 3,
2020-04-11 21:35:14 +08:00
// 远程配置合并节点, 默认 config 根节点
2020-07-20 00:29:00 +08:00
'config_append_node' => 'nacos_config',
2020-07-08 18:50:34 +08:00
'listener_config' => [
2020-04-11 21:35:14 +08:00
// 配置项 dataId, group, tenant, type, content
[
2020-07-20 00:19:59 +08:00
'data_id' => 'hyperf-service-config',
2020-04-11 21:35:14 +08:00
'group' => 'DEFAULT_GROUP',
],
[
2020-07-20 00:19:59 +08:00
'data_id' => 'hyperf-service-config-yml',
2020-04-11 21:35:14 +08:00
'group' => 'DEFAULT_GROUP',
'type' => 'yml',
],
],
];
```
2020-07-20 15:17:27 +08:00
2020-07-20 00:29:00 +08:00
系统将自动监听`listener_config` 中的配置,并将其合并入`hyperf Config` 对象的指定(`config_append_node`) 节点,可以用`config('nacos_config.***')` 获取,若没有配置 `config_append_node` 项,将会并入 `Config` 对象根节点。
> 所有配置的 `键(key)` 在实际发起 API 请求时会自动从下划线风格转换为驼峰风格。