tinywan / nacos
This package is abandoned and no longer maintained.
No replacement package was suggested.
webman nacos plugin
v0.1.0
2022-03-19 11:48 UTC
Requires
- php: >=7.4
- ext-json: *
- guzzlehttp/guzzle: ^6.5|^7.0
- workerman/webman-framework: ^1.2.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.6
- phpstan/phpstan: ^1.4
- phpunit/phpunit: ^9.5
- workerman/webman: ^1.0
README
安装
composer require tinywan/nacos
基本用法
认证
use Tinywan\Nacos\Nacos; $nacos = new Nacos(); $login = $nacos->auth->login('nacos', 'nacos'); var_dump($login->getBody()->getContents());
Docker环境,添加环境变量
NACOS_AUTH_ENABLE=true
(默认该值为false)
认证成功
若用户名和密码正确,返回信息如下
{ "accessToken": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJuYWNvcyIsImV4cCI6MTY0NzUwMzcxMX0.m1p2WKSz1TNj3ftUgKSJuj7Yu4SdoQiV-k0rm6IbvFg", "tokenTtl": 18000, "globalAdmin": true, "username": "nacos" }
认证失败
抛出 NacosAuthException
异常
配置
获取配置
use Tinywan\Nacos\Nacos; $nacos = new Nacos(); $response = $nacos->config->get('database', 'DEFAULT_GROUP'); if (false === $response) { var_dump($nacos->config->getMessage()); }
发布配置
use Tinywan\Nacos\Nacos; $nacos = new Nacos(); $content = [ 'listen' => 'http://0.0.0.0:8888', 'transport' => 'tcp', 'context' => [], 'name' => 'webman-admin', ]; $response = $nacos->config->publish('aliyun', 'DEFAULT_GROUP',json_encode($content)); if (false === $response) { var_dump($nacos->config->getMessage()); }
实例
注册实例
use Tinywan\Nacos\Nacos; $nacos = new Nacos(); $optional = [ 'groupName' => 'webman-group', 'clusterName' => 'webman', 'namespaceId' => 'webman', 'weight' => 99.0, 'metadata' => '', 'enabled' => true, 'ephemeral' => false ]; $response = $nacos->instance->register('127.0.0.1', 8888,'webman-admin',$optional);
鉴权
https://nacos.io/zh-cn/docs/auth.html
OpenAPI
- 配置管理
- 服务发现
- 命名空间
Other
composer
composer dumpautoload
phpstan
vendor/bin/phpstan analyse src
php-cs-fixer
vendor/bin/php-cs-fixer fix src
Basic
__toString()
方法用于一个类被当成字符串时应怎样回应。例如 echo $obj; 应该显示些什么。警告:在 PHP 7.4.0 之前不能在 __toString() 方法中抛出异常。这么做会导致致命错误。
__invoke()
当尝试以调用函数的方式调用一个对象时,__invoke() 方法会被自动调用。__callStatic()
在静态上下文中调用一个不可访问方法时,__callStatic() 会被调用。
观察者模式
- 主题
- 观察者(订阅、取消主题)