PHPackages                             workbunny/webman-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. [Utility &amp; Helpers](/categories/utility)
4. /
5. workbunny/webman-nacos

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

workbunny/webman-nacos
======================

Webman plugin workbunny/webman-nacos

2.0.1(3mo ago)673.9k↓100%7[1 issues](https://github.com/workbunny/webman-nacos/issues)MITPHPPHP &gt;=8.1CI passing

Since May 10Pushed 3mo ago2 watchersCompare

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

READMEChangelog (10)Dependencies (7)Versions (31)Used By (0)

[![workbunny](https://camo.githubusercontent.com/e2b1164338390ab45783434659e3e419e0c3b50fbe140c697ba1f82f59015ad6/68747470733a2f2f6368617a366368657a2e636e2f696d616765732f776f726b62756e6e792d6c6f676f2e706e67)](https://camo.githubusercontent.com/e2b1164338390ab45783434659e3e419e0c3b50fbe140c697ba1f82f59015ad6/68747470733a2f2f6368617a366368657a2e636e2f696d616765732f776f726b62756e6e792d6c6f676f2e706e67)

**workbunny/webman-nacos**

**🐇 PHP implementation of Nacos OpenAPI for webman plugin. 🐇**

A PHP implementation of Nacos OpenAPI for webman plugin
=======================================================

[](#a-php-implementation-of-nacos-openapi-for-webman-plugin)

 [ ![Build Status](https://github.com/workbunny/webman-nacos/actions/workflows/CI.yml/badge.svg) ](https://github.com/workbunny/webman-nacos/actions) [ ![Latest Stable Version](https://camo.githubusercontent.com/491da54db01b347def16c36199f7fb5c46c348e5bcf71ade053a7a3051aca265/68747470733a2f2f62616467656e2e6e65742f7061636b61676973742f762f776f726b62756e6e792f7765626d616e2d6e61636f732f6c6174657374) ](https://github.com/workbunny/webman-nacos/releases) [ ![PHP Version Require](https://camo.githubusercontent.com/4aeb43c9757b8774b5dcef597c376e9615cd2905824e77eb3896804bf6eebfe5/68747470733a2f2f62616467656e2e6e65742f7061636b61676973742f7068702f776f726b62756e6e792f7765626d616e2d6e61636f73) ](https://github.com/workbunny/webman-nacos/blob/main/composer.json) [ ![GitHub license](https://camo.githubusercontent.com/c1269f3cfa39e22ff249ef521823d8c5f5231bd94cd7816f50c23ab2fbc8319b/68747470733a2f2f62616467656e2e6e65742f7061636b61676973742f6c6963656e73652f776f726b62756e6e792f7765626d616e2d6e61636f73) ](https://github.com/workbunny/webman-nacos/blob/main/LICENSE)

Naocs插件问答帖
----------

[](#naocs插件问答帖)

简介
--

[](#简介)

### 什么是Nacos？

[](#什么是nacos)

[Nacos](https://nacos.io/) 致力于帮助您发现、配置和管理微服务；是微服务/SOA架构体系中服务治理环节的重要成员服务；简单的可以把Nacos理解为一个配置中心和一个服务注册中心。

### 什么时候用Nacos？

[](#什么时候用nacos)

- 当我们的服务越来越大、越来越复杂，需要配置的地方越来越多，配置存放的地方也越来越多的时候，为了可以方便统一管理配置，这时候就可以引入[Nacos](https://nacos.io/)。
- 当我们的服务越来越多，有些时候部署需要做到弹性伸缩，需要用到一些负载策略的时候，可以引入[Nacos](https://nacos.io/)进行服务的治理。

### 生态

[](#生态)

- Webman-naocs是基于PHP开发的[Webman](https://github.com/walkor/webman)插件生态下的Nacos客户端；
- 本项目来源于 [Tinywan/nacos](https://www.workerman.net/plugin/25)，对 Tinywan 表示感谢！区别于 [Tinywan/nacos](https://www.workerman.net/plugin/25)，[workbunny/webman-nacos](https://github.com/workbunny/webman-nacos)在配置监听和实例注册上有不同的实现方式，其他的使用方法与之无异；
- Webman-nacos使用的主要组件：

    - [workerman/http-client](https://github.com/walkor/http-client)
    - [guzzlehttp/guzzle](https://github.com/guzzle/guzzle/)

安装
--

[](#安装)

```
composer require workbunny/webman-nacos

```

使用
--

[](#使用)

### 1. Nacos文档地址

[](#1-nacos文档地址)

- **[Nacos Open-API文档](https://nacos.io/zh-cn/docs/open-api.html)**

### 2. 服务的使用

[](#2-服务的使用)

1. 创建连接通道

```
// 使用默认通道【建议使用】
$client = \Workbunny\WebmanNacos\Client::channel();
// 使用channel.php中键名为ABC的连接配置
$client = \Workbunny\WebmanNacos\Client::channel('ABC');
```

> **注：该方案默认使用channel.php中的连接配置，支持多通道连接，建议使用！**

> **注：获取一个不存在的配置信息时，会抛出一个 NacosException 异常。**

2. 以监听配置举例

```

$client = \Workbunny\WebmanNacos\Client::channel();
// 异步非阻塞监听
// 注：在webman中是异步非阻塞的，不会阻塞当前进程
$response = $client->config->listenerAsyncUseEventLoop();
// 异步阻塞监听
// 注：在webman中是异步阻塞的，返回的是Guzzle/PromiseInterface，配合wait()可以多条请求并行执行；
//     请求会阻塞在 **wait()** 直到执行完毕；详见 **ConfigListernerProcess.php**
$response = $client->config->listenerAsync();
// 同步阻塞监听
$response = $client->config->listener();
```

3. 断开连接

```
$client = \Workbunny\WebmanNacos\Client::channel();
$client->cancel();
```

#### 配置说明：

[](#配置说明)

1. app.php 为基础配置；
2. channel.php 为连接通道配置；
3. process.php 为默认启动进程配置；
4. listeners.php 为监听配置文件；
5. registrars.php 为注册配置文件；

### 3. Nacos相关服务

[](#3-nacos相关服务)

#### 配置相关

[](#配置相关)

- 监听配置

webman-nacos组件默认会启动一个名为 **config-listener** 的进程，用于监听在配置文件 **plugin/workbunny/webman-nacos/listeners.php**下的配置内容。

如果想自行掌控调用，可以使用如下服务：

```
$client = \Workbunny\WebmanNacos\Client::channel();

// 异步非阻塞监听
// 注：在webman中是异步非阻塞的，不会阻塞当前进程
$response = $client->config->listenerAsyncUseEventLoop();

// 异步阻塞监听
// 注：在webman中是异步阻塞的，返回的是Guzzle/PromiseInterface，配合wait()可以多条请求并行执行；
//     请求会阻塞在 **wait()** 直到执行完毕；详见 **ConfigListernerProcess.php**
$response = $client->config->listenerAsync();

# 同步阻塞监听
$response = $client->config->listener();
```

- 获取配置

```
$client = \Workbunny\WebmanNacos\Client::channel();
$response = $client->config->get('database', 'DEFAULT_GROUP');
// 获取有命名空间的配置文件，第三个参数填写对应的命名空间
$response = $client->config->get('database', 'DEFAULT_GROUP', 'b34ea59f-e240-413b-ba3d-bb040981d773');
if (false === $response) {
    var_dump($nacos->config->getMessage());
}
```

- 提交配置

```
$client = \Workbunny\WebmanNacos\Client::channel();
$response = $client->config->publish('database', 'DEFAULT_GROUP', file_get_contents('.env'));
if (false === $response) {
    var_dump($nacos->config->getMessage());
}
```

- 移除配置

```
$client = \Workbunny\WebmanNacos\Client::channel();
$response = $client->config->delete('database', 'DEFAULT_GROUP');;
if (false === $response) {
    var_dump($nacos->config->getMessage());
}
```

#### 服务相关：

[](#服务相关)

- 实例注册

webman-nacos组件默认会启动一个名为 **instance-registrar** 的进程，用于注册在配置文件 **plugin/workbunny/webman-nacos/registrars.php**下的配置内容。

如需动态注册实例，请使用：

```
$client = \Workbunny\WebmanNacos\Client::channel();
$response = $client->instance->register('127.0.0.1', 8848, '猜猜我是谁', [
    'groupName' => 'DEFAULT_GROUP',
]);
if (false === $response) {
    var_dump($client->instance->getMessage());
}
var_dump($response);
```

- 移除实例

```
$client = \Workbunny\WebmanNacos\Client::channel();
$response = $client->instance->delete('猜猜我是谁', 'DEFAULT_GROUP', '127.0.0.1', 8848, []);
if (false === $response) {
    var_dump($client->config->getMessage());
}
```

- 实例列表

```
$client = \Workbunny\WebmanNacos\Client::channel();
$response = $client->instance->list('猜猜我是谁', []);
if (false === $response) {
    var_dump($client->config->getMessage());
}
print_r($response);
```

**注：实例与服务的区别请参看Nacos文档；**

#### 其他：

[](#其他)

- **具体使用参数都在源码内已标注，使用方法很简单，参考Nacos官方文档即可；**
- **后缀为Async的方法是Guzzle异步请求，在当前业务执行周期中阻塞，多个请求可并行执行；**
- **后缀为AsyncUseEventLoop的方法是workerman/http-client异步请求，在当前业务周期中非阻塞；**

```
$client = \Workbunny\WebmanNacos\Client::channel();

# 配置相关接口
$client->config;

# 鉴权相关接口
$client->auth;

# 实例相关接口
$client->instance;

# 系统相关接口
$client->operator;

# 服务相关接口
$client->service;
```

说明
--

[](#说明)

- 目前这套代码在我司生产环境运行，我会做及时的维护，**欢迎 issue 和 PR**；
- 对于不知道Nacos有什么用的/在什么时候用，可以参考这篇文章 [Nacos在我司的应用及SOA初尝](https://www.workerman.net/a/1339);
- nacos的配置监听项采用了服务端长轮询，有点类似于stream\_select，当配置没有改变的时候，会阻塞至请求结束；但当配置有变化时候，会立即返回其配置dataId；这里我的做法是开启一个Timer对配置进行监听，定时器间隔与长轮询最大阻塞时间一致:

    1. ConfigListenerProcess使用Guzzle的异步请求对配置监听器进行请求处理， onWorkerStart中的Guzzle客户端会阻塞请求，workerman status中会显示BUSY状态；
    2. AsyncConfigListenerProcess使用wokerman/http-client异步请求对配置监听 器进行请求，workerman/http-client使用了workerman的event-loop进行I/O处理， 不会阻塞当前进程，推荐使用；
- 所有的配置同步后会触发 **workerman reload** 对所有进程进行重载，保证了config的刷新，包括已经在内存中的各种单例，如 数据库连接、Redis连接等，保证即时将配置传达至需要的业务点；
- 使用配置方式不必改变，使用webman的`config()`即可，降低封装组件的心智负担;

其他
--

[](#其他-1)

- **[趣谈程序的演变过程](https://www.workerman.net/a/1341)**
- **[Nacos在我司的应用及SOA初尝](https://www.workerman.net/a/1339)**
- **[Nacos Open-API文档](https://nacos.io/zh-cn/docs/open-api.html)**
- **[workbunny/webman-nacos 项目地址](https://github.com/workbunny/webman-nacos)**

###  Health Score

54

—

FairBetter than 97% of packages

Maintenance83

Actively maintained with recent releases

Popularity35

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 74.5% 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 ~46 days

Recently: every ~127 days

Total

30

Last Release

116d ago

Major Versions

0.1.2 → 1.0.02022-05-12

1.x-dev → 2.0.02026-01-13

PHP version history (2 changes)0.0.1PHP &gt;=7.4

2.0.0PHP &gt;=8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/d1b317277226c93c11984f73dfad75533893466b02d06611126aa1687209c6ab?d=identicon)[Chaz6Chez](/maintainers/Chaz6Chez)

---

Top Contributors

[![chaz6chez](https://avatars.githubusercontent.com/u/22535862?v=4)](https://github.com/chaz6chez "chaz6chez (70 commits)")[![Tinywan](https://avatars.githubusercontent.com/u/14959876?v=4)](https://github.com/Tinywan "Tinywan (8 commits)")[![krissss](https://avatars.githubusercontent.com/u/10680903?v=4)](https://github.com/krissss "krissss (6 commits)")[![sunsgneayo](https://avatars.githubusercontent.com/u/51745500?v=4)](https://github.com/sunsgneayo "sunsgneayo (3 commits)")[![kylin987](https://avatars.githubusercontent.com/u/26080774?v=4)](https://github.com/kylin987 "kylin987 (3 commits)")[![srako](https://avatars.githubusercontent.com/u/18043224?v=4)](https://github.com/srako "srako (2 commits)")[![luoyue712](https://avatars.githubusercontent.com/u/102944161?v=4)](https://github.com/luoyue712 "luoyue712 (1 commits)")[![yuan-dian](https://avatars.githubusercontent.com/u/33216505?v=4)](https://github.com/yuan-dian "yuan-dian (1 commits)")

---

Tags

nacosphpwebmanworkbunnyworkerman

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/workbunny-webman-nacos/health.svg)

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

###  Alternatives

[google/cloud-core

Google Cloud PHP shared dependency, providing functionality useful to all components.

343121.4M78](/packages/google-cloud-core)[huaweicloud/huaweicloud-sdk-php

Huawei Cloud SDK for PHP

1829.2k2](/packages/huaweicloud-huaweicloud-sdk-php)

PHPackages © 2026

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