定制 tinywan/nacos-sdk-php 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tinywan/nacos-sdk-php

最新稳定版本:v0.1.0

Composer 安装命令:

composer require tinywan/nacos-sdk-php

包简介

PHP SDK For Nacos

README 文档

README

license 996.icu Build status Latest Stable Version Total Downloads License PHP Version Require

Nacos-Sdk-PHP

Nacos-Sdk-PHP for PHP client allows you to access Nacos OpenAPI. Open API Guide

Requirements

  • PHP ^7.0

Installation

composer require tinywan/nacos-sdk-php

Getting Started

use Nacos\NacosClient; $client = new NacosClient('localhost', 8848); $dataId = 'database.php'; $group = 'DEFAULT_GROUP'; $result = $client->getConfig($dataId, $group);

if nacos.core.auth.enabled=false

use Nacos\NacosClient; use Nacos\NacosAuth; $client = new NacosClient('localhost', 8848); $auth = new NacosAuth($client); $auth->login('nacos','nacos'); $dataId = 'database.php'; $group = 'DEFAULT_GROUP'; $result = $client->getConfig($dataId, $group);

Use Namespace

use Nacos\NacosClient; $dataId = 'database.php'; $group = 'DEFAULT_GROUP'; $namespace = 'c78ce19d-82d1-456a-8552-9a0db6c11d01'; $client = new NacosClient('localhost', 8848); $client->setNamespace($namespace); $result = $client->getConfig($dataId, $group);

Listener Config

use Nacos\NacosClient; use Nacos\Models\Config; $dataId = 'database.php'; $group = 'DEFAULT_GROUP'; $namespace = 'c78ce19d-82d1-456a-8552-9a0db6c11d01'; $client = new NacosClient('localhost', 8848); $client->setNamespace($namespace); $client->setTimeout(3); $content = $client->getConfig($dataId, $group); $contentMd5 = md5($content); $cache = new Config(); $cache->dataId = $dataId; $cache->group = $group; $cache->namespace = $namespace; $cache->contentMd5 = $contentMd5; $result = $client->listenConfig([$cache]); if(!empty($result)) { $updateContent = $client->getConfig($dataId, $group); echo '[x] update content : ' . $updateContent, "\n"; } else { echo '[x] this is not update ', "\n"; }

Register an instance to the service

use Nacos\NacosClient; use Nacos\Models\Config; $client = new NacosClient('localhost', 8848); $serviceName = 'NacosTaskService'; $instance = new ServiceInstance(); $instance->serviceName = $serviceName; $instance->ip = '127.0.0.1'; $instance->port = 80; $instance->healthy = true; $instance->ephemeral = false; $isSuccess = $client->createInstance($instance); if(true === $isSuccess) { echo '[x] create service instance success ', "\n"; } else { echo '[x] create service instance fail ', "\n"; }

nacaos-regiter-service.png

API

Request Options

  • setNamespace
    • string $namespace
  • setTimeout
    • int $timeout

Config API

  • getConfig
    • string $dataId
    • string $group = NacosClient::DEFAULT_GROUP
  • publishConfig
    • string $dataId
    • string $group
    • $content
  • removeConfig
    • string $dataId
    • string $group = NacosClient::DEFAULT_GROUP
  • listenConfig
    • array $configs
    • int $timeout = 30

Naming API

  • createInstance
    • ServiceInstance $instance
  • deleteInstance
    • string $serviceName
    • string $ip
    • int $port
    • string $clusterName = null
    • string $namespaceId = null
  • updateInstance
    • ServiceInstance $instance
  • getInstanceList
    • string $serviceName
    • string $namespaceId = null
    • array $clusters = []
    • bool $healthyOnly = false
  • getInstance
    • string $serviceName
    • string $ip
    • int $port
    • string $namespaceId = null
    • string $cluster = null
    • bool $healthyOnly = false
  • sendInstanceBeat
    • string $serviceName
    • BeatInfo $beat

PHPUnit Test

NacosClientTest

./vendor/bin/phpunit --bootstrap src/Nacos/NacosClient.php tests/NacosClientTest.php 
  • phpunit 调用命令行测试PHPUnit
  • --bootstrap src/Nacos/NacosClient.php 指示PHPUnit命令行测试在测试之前执行 include src/Nacos/NacosClient.php
  • tests/NacosClientTest.php 指示PHPUnit命令行测试要执行的测试 NacosClientTest 类声明在 tests/NacosClientTest.php
  • http://www.phpunit.cn/getting-started.html

NacosConfig

./vendor/bin/phpunit --bootstrap src/Nacos/NacosConfig.php tests/NacosConfigTest.php 

NacosNaming

./vendor/bin/phpunit --bootstrap src/Nacos/NacosNaming.php tests/NacosNamingTest.php 

Other

Docker Composer

docker run --rm --interactive --tty -v e:/GitHub/nacos-sdk-php:/app composer:1.10.16 install --ignore-platform-reqs 

Git Tag

git push origin v0.0.42 // or push all git push origin --tags 

统计信息

  • 总下载量: 6.05k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 13
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 12
  • Watchers: 4
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固