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
|
|
|
```
|
|
|
|
|
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-20 15:17:27 +08:00
|
|
|
## 服务与实例
|
2020-04-11 21:35:14 +08:00
|
|
|
|
2021-06-27 16:37:24 +08:00
|
|
|
当前组件仍然保留了之前提供的服务注册功能。
|
|
|
|
|
|
|
|
只需要安装 `hyperf/service-governance-nacos` 组件,然后配置以下监听器和自定义进程即可。
|
2020-04-11 21:35:14 +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-04-11 21:35:14 +08:00
|
|
|
|
2021-06-20 20:42:32 +08:00
|
|
|
- config/autoload/server.php
|
2020-04-11 21:35:14 +08:00
|
|
|
|
2021-06-20 20:42:32 +08:00
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
use Hyperf\Server\Event;
|
2020-04-11 21:35:14 +08:00
|
|
|
return [
|
|
|
|
// ...other
|
|
|
|
'callbacks' => [
|
|
|
|
// ...other
|
2021-01-21 14:44:04 +08:00
|
|
|
Event::ON_SHUTDOWN => [Hyperf\Framework\Bootstrap\ShutdownCallback::class, 'onShutdown']
|
2020-04-11 21:35:14 +08:00
|
|
|
]
|
|
|
|
];
|
|
|
|
```
|
|
|
|
|