PHP code example of zxyfaxcn / nacos
1. Go to this page and download the library: Download zxyfaxcn/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/ */
zxyfaxcn / nacos example snippets
// config/autoload/server.php
return [
// ...other
'callbacks' => [
// ...other
SwooleEvent::ON_SHUTDOWN => [Hyperf\Framework\Bootstrap\ShutdownCallback::class, 'onShutdown']
]
];
$instance = new ThisInstance();
$service = new ThisService();
$instance = make(NacosInstance::class);
$service = new ServiceModel([
'serviceName' => 'hyperf',
'groupName' => 'api',
'namespaceId' => '5ce9d1c1-6732-4ccc-ae1f-5139af86a845'
]);
$optimal = $instance->getOptimal($service);
// config/autoload/nacos.php
return [
'enable' => true, // 是否开启自动注册 默认false
'deleteServiceWhenShutdown' => true, // 是否开启自动注销 默认false
'host' => '127.0.0.1',
'port' => '8848',
// 服务配置 serviceName, groupName, namespaceId
// protectThreshold, metadata, selector
'service' => [
'serviceName' => 'hyperf',
'groupName' => 'api',
'namespaceId' => 'namespace_id',
'protectThreshold' => 0.5,
],
// 节点配置 serviceName, groupName, weight, enabled,
// healthy, metadata, clusterName, namespaceId, ephemeral
'client' => [
'namespaceId' => 'namespace_id', // 注意此处必须和service保持一致
'serviceName' => 'hyperf',
'groupName' => 'DEFAULT',
'weight' => 80,
'enabled' => true,
'healthy' => true,
'cluster' => 'DEFAULT',
'ephemeral' => true,
'beatEnable' => true,// 心跳
'beatInterval' => 5,// s
],
// 配置刷新
'configReloadEnable' => false,
'configReloadInterval' => 3,// 间隔 s
// 远程配置合并节点, 默认 config 根节点
'configAppendNode' => 'nacos_conf',
'listenerConfig' => [
// 配置项 dataId, group, tenant, type, content
[
'dataId' => 'hyperf-service-config',
'group' => 'DEFAULT_GROUP',
],
[
'dataId' => 'hyperf-service-config-yml',
'group' => 'DEFAULT_GROUP',
'type' => 'yml',
],
],
];
shell
php bin/hyperf.php vendor:publish zxyfaxcn/nacos