1. Go to this page and download the library: Download kriss/php-nacos library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kriss / php-nacos example snippets
use Kriss\Nacos\Contract\ConfigRepositoryInterface;
use Kriss\Nacos\Contract\HttpClientInterface;
use Kriss\Nacos\Contract\LoadBalancerManagerInterface;
use Kriss\Nacos\NacosContainer;
use Kriss\Nacos\Support\HttpClient;
use Kriss\Nacos\Support\LoadBalancerManager;
use Psr\SimpleCache\CacheInterface;
$container = new NacosContainer();
// 构造一个 ConfigRepository 实现 ConfigRepositoryInterface,参考: Kriss\Nacos\Support\MemoryConfigRepository
// 配置的 nacos 参数参考:config/nacos.php
/** @var ConfigRepositoryInterface $configRepository */
$container->add(ConfigRepositoryInterface::class, $configRepository);
// 构造一个 Httpclient 实现 HttpClientInterface,一般可以直接使用 Kriss\Nacos\Support\HttpClient
$httpClient = new HttpClient($log = new \Psr\Log\NullLogger());
$container->add(HttpClientInterface::class, $httpClient);
// 建议配置 log,记录完整的请求或异常的请求
// 例如使用 monolog:
/*$log = new Logger('nacos');
$log->pushHandler(new StreamHandler(__DIR__ . '/../runtime/mock_log.log'));*/
// 在使用 nacos 授权访问时,需要注入 Psr16 的 Cache 组件,用于缓存 nacos 的令牌
// 以下为例子
/*$filesystemAdapter = new Local(__DIR__ . '/../runtime');
$filesystem = new Filesystem($filesystemAdapter);
$cachePool = new FilesystemCachePool($filesystem);
$simpleCache = new SimpleCacheBridge($cachePool);*/
/** @var CacheInterface $simpleCache */
$container->add(CacheInterface::class, $simpleCache);
// 在使用 InstanceService::getOptimal 时需要 loadBalancer
$loadBalancer = new LoadBalancerManager();
$container->add(LoadBalancerManagerInterface::class, $loadBalancer);
/** @var \Kriss\Nacos\NacosContainer $container */
// 授权 API
$authApi = $container->get(\Kriss\Nacos\OpenApi\AuthApi::class);
// 配置管理 API
$configApi = $container->get(\Kriss\Nacos\OpenApi\ConfigApi::class);
// 服务实例 API
$instanceApi = $container->get(\Kriss\Nacos\OpenApi\InstanceApi::class);
// 命名空间 API
$namespaceApi = $container->get(\Kriss\Nacos\OpenApi\NamespaceApi::class);
// 操作系统 API
$operatorApi = $container->get(\Kriss\Nacos\OpenApi\OperatorApi::class);
// 服务 API
$serviceApi = $container->get(\Kriss\Nacos\OpenApi\ServiceApi::class);