2023-04-25 09:32:59 +08:00
# Nacos
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
A `PHP` coroutine client of `Nacos` , perfectly combined with the configuration center and microservice governance of `Hyperf` .
2022-02-16 02:13:55 +08:00
2023-04-25 09:32:59 +08:00
## Installation
2022-02-16 02:13:55 +08:00
```shell
composer require hyperf/nacos
```
2023-04-25 09:32:59 +08:00
### Publish the config file
2022-02-16 02:13:55 +08:00
```shell
php bin/hyperf.php vendor:publish hyperf/nacos
```
```php
< ?php
declare(strict_types=1);
return [
2022-10-31 01:47:12 +08:00
// Developers who cannot use the IP port form can directly configure the url
2022-02-16 02:13:55 +08:00
// 'url' => '',
'host' => '127.0.0.1',
'port' => 8848,
'username' => null,
'password' => null,
'guzzle' => [
'config' => null,
],
];
```
2022-10-31 01:47:12 +08:00
## Services and instances
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
The current component still retains the previously provided service registration functionality.
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
Just install the `hyperf/service-governance-nacos` component, then configure the following listeners and custom processes.
2022-02-16 02:13:55 +08:00
`Hyperf\ServiceGovernanceNacos\Listener\MainWorkerStartListener`
`Hyperf\ServiceGovernanceNacos\Listener\OnShutdownListener`
`Hyperf\ServiceGovernanceNacos\Process\InstanceBeatProcess`
2022-10-31 01:47:12 +08:00
Then add the following configuration to listen to the `Shutdown` event
2022-02-16 02:13:55 +08:00
- config/autoload/server.php
```php
< ?php
use Hyperf\Server\Event;
return [
// ...other
'callbacks' => [
// ...other
Event::ON_SHUTDOWN => [Hyperf\Framework\Bootstrap\ShutdownCallback::class, 'onShutdown']
]
];
```
2023-04-25 09:32:59 +08:00
## Aliyun Service Authentication
When using Aliyun's Nacos service, you may need to use AK and SK authentication. The Nacos component supports it natively. We can easily add the corresponding configuration, as follows:
```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,
],
];
```