PHP code example of zengonly / php-alibaba-nacos

1. Go to this page and download the library: Download zengonly/php-alibaba-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/ */

    

zengonly / php-alibaba-nacos example snippets


# cron.php
Nacos::init(
    "http://127.0.0.1:8848/",
    "dev",
    "LARAVEL",
    "DEFAULT_GROUP",
    ""
)->runOnce();

NacosConfig::setSnapshotPath("指定存放配置文件的目录路径");

Nacos::init(
    "http://127.0.0.1:8848/",
    "dev",
    "LARAVEL",
    "DEFAULT_GROUP",
    ""
)->listener();

GetConfigRequestErrorListener::add(function($config) {
    if (!$config->getConfig()) {
        echo "获取配置异常, 配置为空,下面进行自定义逻辑处理" . PHP_EOL;
        // 设置是否修改配置文件内容,如果修改成true,这里设置的配置文件内容将是最终获取到的配置文件
        $config->setChanged(true);
        $config->setConfig("hello");
    }
});
bash
*/1 */1 * * * php path/to/cron.php
shell
export NACOS_ENV=local