1. Go to this page and download the library: Download yurunsoft/nacos-php 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/ */
yurunsoft / nacos-php example snippets
use Yurun\Nacos\Client;
use Yurun\Nacos\ClientConfig;
// The parameters of ClientConfig are all optional, the ones shown below are the default values
// You can write only the configuration items you want to modify
$config = new ClientConfig([
'host' => '127.0.0.1',
'port' => 8848,
'prefix' => '/',
'username' => '',
'password' => '',
'timeout' => 60000, // Network request timeout time, in milliseconds
'ssl' => false, // Whether to use ssl(https) requests
'authorizationBearer' => false, // Whether to use the request header Authorization: Bearer {accessToken} to pass Token, older versions of Nacos need to be set to true
'maxConnections' => 16, // Connection pool max connections
'poolWaitTimeout' => 30, // Connection pool wait timeout when get connection, in seconds
// Custom configuration processor to convert string configuration values to arbitrary types (e.g. arrays), which can override the default processing
'configParser' => [
// type Name => callback(string value): mixed
'json' => static fn (string $value): array => json_decode($value, true),
],
]);
// Instantiating the client, Not
use Yurun\Nacos\Provider\Config\ConfigListener;
use Yurun\Nacos\Provider\Config\Model\ListenerConfig;
// Get config listener
$listenerConfig = new ListenerConfig([
'timeout' => 30000, // The config listener long polling timeout, in milliseconds. The result is returned immediately when the value is 0.
'failedWaitTime' => 3000, // Waiting time to retry after failure, in milliseconds
'savePath' => '', // Config save path, default is empty and not saved to file
'fileCacheTime' => 0, // The file cache time, defaulted to 0, is not affected by caching, and this configuration only affects pull operations.
]);
$listener = $client->config->getConfigListener($listenerConfig);
$dataId = 'dataId';
$groupId = 'groupId';
$tenant = '';
// Add listening item
$listener->addListener($dataId, $groupId, $tenant);
// Add listening item with callback
$listener->addListener($dataId, $groupId, $tenant, function (\ConfigListener $listener, string $dataId, string $group, string $tenant) {
// $listener->stop();
});
// Pull configuration for all listeners (not
use Yurun\Nacos\Provider\Config\Model\ListenerRequest;
$md5 = '';
while (true) {
$request = new ListenerRequest();
$request->addListener('dataId', 'group', $md5);
$items = $client->listen($request);
foreach ($items as $item) {
if ($item->getChanged()) {
$value = $config->get($item->getDataId(), $item->getGroup(), $item->getTenant());
var_dump('newValue:', $value);
$md5 = md5($value);
}
}
}
use Yurun\Nacos\Provider\Instance\Model\RsInfo;
$beat = new RsInfo();
$beat->setIp('192.168.1.123');
$beat->setPort(8080);
$client->instance->beat('Service1', $beat);