PHP code example of yurunsoft / nacos-php

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 

// Get provider from client

$auth = $client->auth;
$config = $client->config;
$namespace = $client->namespace;
$instance = $client->instance;
$service = $client->service;
$operator = $client->operator;

$value = $client->config->get('dataId', 'group');

$client->config->set('dataId', 'group', json_encode(['id' => 19260817]), 'json');
$value = $client->config->getParsedConfig('dataId', 'group', '', $type);

// output:
// array(1) {
//   ["id"]=>
//   int(19260817)
// }
var_dump($value);

var_dump($type); // json

$client->config->set('dataId', 'group', 'value');

$client->config->delete('dataId', 'group', 'value');

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);
        }
    }
}

$client->instance->register('192.168.1.123', 8080, 'Service1');
// Complete Parameters
$client->instance->register('192.168.1.123', 8080, 'Service1', $namespaceId = '', $weight = 1, $enabled = true, $healthy = true, $metadata = '', $clusterName = '', $groupName = '', $ephemeral = false);

$client->instance->deregister('192.168.1.123', 8080, 'Service1');
// Complete Parameters
$client->instance->deregister('192.168.1.123', 8080, 'Service1', $namespaceId = '', $clusterName = '', $groupName = '', $ephemeral = false);

$client->instance->update('192.168.1.123', 8080, 'Service1');
// Complete Parameters
$client->instance->update('192.168.1.123', 8080, 'Service1', $namespaceId = '', $weight = 1, $enabled = true, $healthy = true, $metadata = '', $clusterName = '', $groupName = '', $ephemeral = false);

use Yurun\Nacos\Provider\Instance\Model\RsInfo;

$beat = new RsInfo();
$beat->setIp('192.168.1.123');
$beat->setPort(8080);
$client->instance->beat('Service1', $beat);

$response = $client->instance->list('Service1');
$response = $client->instance->list('Service1', $groupName = '', $namespaceId = '', $clusters = '', $healthyOnly = false);

$response = $client->instance->detail('192.168.1.123', 8080, 'Service1');
// Complete Parameters
$response = $client->instance->detail('192.168.1.123', 8080, 'Service1', $groupName = '', $namespaceId = '', $clusters = '', $healthyOnly = false, $ephemeral = false);