PHPackages                             imiphp/imi-nacos - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. imiphp/imi-nacos

ActiveLibrary[Framework](/categories/framework)

imiphp/imi-nacos
================

imi 框架 Nacos 组件。imi Nacos component.

2.1.x-dev(3y ago)5191MulanPSL-2.0PHP

Since Sep 16Pushed 3y ago1 watchersCompare

[ Source](https://github.com/imiphp/imi-nacos)[ Packagist](https://packagist.org/packages/imiphp/imi-nacos)[ RSS](/packages/imiphp-imi-nacos/feed)WikiDiscussions 2.1 Synced 1mo ago

READMEChangelog (4)Dependencies (12)Versions (5)Used By (0)

imi-nacos
=========

[](#imi-nacos)

[![Latest Version](https://camo.githubusercontent.com/ec6e3257f80ec4bfc14cc45fe1aa5ab49ae4372fa30789d52c45cf9a01f53f59/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696d697068702f696d692d6e61636f732e737667)](https://packagist.org/packages/imiphp/imi-nacos)[![Php Version](https://camo.githubusercontent.com/4a5c2ab20974058a8bab53ecb30ac4c2e6bb961df6229b7386fdc097ab53dfa8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d2533453d372e342d627269676874677265656e2e737667)](https://secure.php.net/)[![Swoole Version](https://camo.githubusercontent.com/2b08ceed852c672ccac1ba1e0db0399fd6c269b1f40d58b8d4cf11455a95446d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f73776f6f6c652d2533453d342e382e302d627269676874677265656e2e737667)](https://github.com/swoole/swoole-src)[![imi License](https://camo.githubusercontent.com/98bdbfaa35e79370786c656980aab520be3e8af9551d8418a3ca4da1775f84c9/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d756c616e50534c253230322e302d627269676874677265656e2e737667)](https://github.com/imiphp/imi-nacos/blob/master/LICENSE)

介绍
--

[](#介绍)

此项目是 imi 框架的 Nacos 组件。

> 正在开发中，随时可能修改，请勿用于生产环境！

**支持的功能：**

- 配置中心
- 服务注册

安装
--

[](#安装)

`composer require imiphp/imi-nacos:~2.1.0`

使用说明
----

[](#使用说明)

### 配置中心

[](#配置中心)

#### 配置

[](#配置)

`@app.beans`：

```
[
    'ConfigCenter' => [
        // 'mode'    => \Imi\ConfigCenter\Enum\Mode::WORKER, // 工作进程模式
        'mode'    => \Imi\ConfigCenter\Enum\Mode::PROCESS, // 进程模式
        'configs' => [
            'nacos' => [
                'driver'  => \Imi\Nacos\Config\NacosConfigDriver::class,
                // Nacos 客户端连接配置
                'client'  => [
                    'host'                => '127.0.0.1', // 主机名
                    'port'                => 8848, // 端口号
                    'prefix'              => '/', // 前缀
                    'username'            => 'nacos', // 用户名
                    'password'            => 'nacos', // 密码
                    'timeout'             => 60000, // 网络请求超时时间，单位：毫秒
                    'ssl'                 => false, // 是否使用 ssl(https) 请求
                    'authorizationBearer' => false, // 是否使用请求头 Authorization: Bearer {accessToken} 方式传递 Token，旧版本 Nacos 需要设为 true
                ],
                // 监听器配置
                'listener' => [
                    'timeout'         => 30000, // 配置监听器长轮询超时时间，单位：毫秒
                    'failedWaitTime'  => 3000, // 失败后等待重试时间，单位：毫秒
                    'savePath'        => Imi::getRuntimePath('config-cache'), // 配置保存路径，默认为空不保存到文件。php-fpm 模式请一定要设置！
                    'fileCacheTime'   => 30, // 文件缓存时间，默认为0时不受缓存影响，此配置只影响 pull 操作。php-fpm 模式请一定要设置为大于0的值！
                    'pollingInterval' => 10000, // 客户端轮询间隔时间，单位：毫秒
                ],
                // 配置项
                'configs' => [
                    'nacos' => [
                        'key'   => 'imi-nacos-key1',
                        'group' => 'imi',
                        'type'  => 'json', // 配置内容类型，Nacos >= 1.3 可以不配，由配置项类型智能指定
                    ],
                ],
            ],
        ],
    ],
]
```

#### 获取配置

[](#获取配置)

```
\Imi\Config::get('nacos'); // 对应 imi-nacos-key1
```

#### 写入配置

[](#写入配置)

```
/** @var \Imi\ConfigCenter\ConfigCenter $configCenter */
$configCenter = App::getBean('ConfigCenter');
$name = 'imi-nacos-key1';
$group = 'imi';
$type = 'json';
$value = json_encode(['imi' => 'niubi']);
$configCenter->getDriver('nacos')->push($name, $value, [
    'group' => $group,
    'type'  => $type,
]);
```

### 注册中心

[](#注册中心)

#### 引入依赖

[](#引入依赖)

`composer require imiphp/imi-service`

#### 配置

[](#配置-1)

`@app.beans`：

```
[
    'ServiceRegistry' => [
        'drivers' => [
            [
                'driver' => \Imi\Nacos\Service\NacosServiceRegistry::class, // 驱动类名
                // 注册的服务列表
                'services' => [
                    'main', // 格式1：主服务器是 main，子服务器就是子服务器名
                    // 格式2：数组配置
                    [
                        // 所有参数按需设置
                        'server'     => 'main', // 主服务器是 main，子服务器就是子服务器名
                        // 'instanceId' => '实例ID',
                        'serviceId'  => 'main_test',
                        'weight'     => 1, // 权重
                        'uri'        => 'http://127.0.0.1:8080', // uri
                        // 'host'       => '127.0.0.1',
                        // 'port'       => 8080,
                        'metadata'   => [
                            // 'group' => 'DEFAULT_GROUP', // 分组
                            // 'namespaceId' => '', // 命名空间
                            // 'metadata' => [], // metadata
                            // 'ephemeral' => true, // 是否为临时实例
                        ],
                        // 'interface'  => 'eth0', // 网卡 interface 名，自动获取当前网卡IP时有效
                    ],
                ],
                'client' => [
                    // Nacos 客户端连接配置
                    'host'                => '127.0.0.1', // 主机名
                    'port'                => 8848, // 端口号
                    'prefix'              => '/', // 前缀
                    'username'            => 'nacos', // 用户名
                    'password'            => 'nacos', // 密码
                    'timeout'             => 60000, // 网络请求超时时间，单位：毫秒
                    'ssl'                 => false, // 是否使用 ssl(https) 请求
                    'authorizationBearer' => false, // 是否使用请求头 Authorization: Bearer {accessToken} 方式传递 Token，旧版本 Nacos 需要设为 true
                ],
                'heartbeat' => 3, // 心跳时间，单位：秒
            ],
        ],
    ],
]
```

### 服务发现（负载均衡）

[](#服务发现负载均衡)

#### 引入依赖

[](#引入依赖-1)

`composer require imiphp/imi-service`

#### 配置

[](#配置-2)

`@app.beans`：

```
[
    'ServiceDiscovery' => [
        'drivers' => [
            [
                'driver'       => \Imi\Nacos\Service\NacosServiceDiscoveryDriver::class, // 服务发现驱动
                // 'client' => \Imi\Service\Discovery\DiscoveryClient::class, // 服务发现客户端，如无必要不需要修改
                // 负载均衡配置
                'loadBalancer' => \Imi\Service\LoadBalancer\RandomLoadBalancer::class, // 负载均衡-随机
                // 'loadBalancer' => \Imi\Service\LoadBalancer\RoundRobinLoadBalancer::class, // 负载均衡-轮询
                // 'loadBalancer' => \Imi\Service\LoadBalancer\WeightLoadBalancer::class, // 负载均衡-权重
                // 发现服务列表
                'services' => [
                    'serviceName', // 改为你的服务名称
                ],
                'clientConfig' => [
                    // Nacos 客户端连接配置
                    'host'                => '127.0.0.1', // 主机名
                    'port'                => 8848, // 端口号
                    'prefix'              => '/', // 前缀
                    'username'            => 'nacos', // 用户名
                    'password'            => 'nacos', // 密码
                    'timeout'             => 60000, // 网络请求超时时间，单位：毫秒
                    'ssl'                 => false, // 是否使用 ssl(https) 请求
                    'authorizationBearer' => false, // 是否使用请求头 Authorization: Bearer {accessToken} 方式传递 Token，旧版本 Nacos 需要设为 true
                ],
                'cacheTTL' => 60, // 缓存时间，单位：秒。默认为60秒，设为0不启用缓存
            ],
        ],
    ],
]
```

#### 获取服务可用节点

[](#获取服务可用节点)

```
/** @var \Imi\Service\Discovery\ServiceDiscovery $serviceDiscovery */
$serviceDiscovery = \Imi\App::getBean('ServiceDiscovery');
$service = $serviceDiscovery->getInstance('服务名称');

$service->getInstanceId(); // 实例ID，string
$service->getServiceId(); // 服务ID，string
$service->getWeight(); // 权重，float
$service->getUri(); // \Imi\Util\Uri
$service->getMetadata(); // 元数据，数组

// 获取服务实例的ip及端口的常用写法
$uri = $service->getUri();
$host = $uri->getHost();
$port = $uri->getPort();
```

免费技术支持
------

[](#免费技术支持)

QQ群：17916227 [![点击加群](https://camo.githubusercontent.com/75b53e353bb9e5064662e185a6d39f4bb88c4e45bd3a1240ddf599525edb6afa/68747470733a2f2f7075622e69647171696d672e636f6d2f7770612f696d616765732f67726f75702e706e67 "点击加群")](https://jq.qq.com/?_wv=1027&k=5wXf4Zq)，如有问题会有人解答和修复。

运行环境
----

[](#运行环境)

- [PHP](https://php.net/) &gt;= 7.4
- [Composer](https://getcomposer.org/) &gt;= 2.0
- [Swoole](https://www.swoole.com/) &gt;= 4.8.0
- [imi](https://www.imiphp.com/) &gt;= 2.1

版权信息
----

[](#版权信息)

`imi-nacos` 遵循 MulanPSL-2.0 开源协议发布，并提供免费使用。

捐赠
--

[](#捐赠)

[![](https://camo.githubusercontent.com/24487a1dd6ab2d9c7b7492027024eae97eba149b95eab696a568034005ae335d/68747470733a2f2f63646e2e6a7364656c6976722e6e65742f67682f696d697068702f696d6940322e312f7265732f7061792e706e67)](https://camo.githubusercontent.com/24487a1dd6ab2d9c7b7492027024eae97eba149b95eab696a568034005ae335d/68747470733a2f2f63646e2e6a7364656c6976722e6e65742f67682f696d697068702f696d6940322e312f7265732f7061792e706e67)

开源不求盈利，多少都是心意，生活不易，随缘随缘……

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity36

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~10 days

Total

5

Last Release

1291d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6f917bb42280d114c53cebadc2942a13ee03abe14971089f88895e266d637169?d=identicon)[Yurunsoft](/maintainers/Yurunsoft)

---

Top Contributors

[![Yurunsoft](https://avatars.githubusercontent.com/u/20104656?v=4)](https://github.com/Yurunsoft "Yurunsoft (30 commits)")

---

Tags

nacosphpswoole

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/imiphp-imi-nacos/health.svg)

```
[![Health](https://phpackages.com/badges/imiphp-imi-nacos/health.svg)](https://phpackages.com/packages/imiphp-imi-nacos)
```

###  Alternatives

[laravel/telescope

An elegant debug assistant for the Laravel framework.

5.2k67.8M191](/packages/laravel-telescope)[spiral/roadrunner

RoadRunner: High-performance PHP application server and process manager written in Go and powered with plugins

8.4k12.2M84](/packages/spiral-roadrunner)[nolimits4web/swiper

Most modern mobile touch slider and framework with hardware accelerated transitions

41.8k177.2k1](/packages/nolimits4web-swiper)[laravel/dusk

Laravel Dusk provides simple end-to-end testing and browser automation.

1.9k36.7M256](/packages/laravel-dusk)[laravel/prompts

Add beautiful and user-friendly forms to your command-line applications.

708181.8M592](/packages/laravel-prompts)[cakephp/chronos

A simple API extension for DateTime.

1.4k47.7M121](/packages/cakephp-chronos)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
