Git Product home page Git Product logo

php-nacos's Introduction

阿里巴巴nacos配置中心-PHP客户端

Nacos配置中心的PHP客户端,更多关于Nacos配置中心的介绍,可以查看Nacos配置中心Wiki

特性

  1. 容错兜底
  2. 容易上手
  3. 技术支持,有问题可加作者微信: suxiaolinKing

开发计划

composer安装

composer require alibaba/nacos

使用crontab拉取配置文件

定时1分钟拉取一次

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

拉取到的配置文件路径:当前工作目录/nacos/config/dev_nacos/snapshot/LARAVEL

配置文件保存的工作目录可以通过下面命令修改

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

配置兜底方案

将兜底的配置文件放入下面的路径里

如果有给$tenant设置值,文件路径这样计算

工作目录/nacos/config/{$env}_nacos/config-data-{$tenant}/{$dataId}

否则

工作目录/nacos/config/{$env}_nacos/config-data/{$dataId}

nacos会在无法从配置中心查询配置文件时将读取上面的配置文件

Dummy模式(本地开发不走配置中心)

配置环境变量NACOS_ENV=local再启动项目

export NACOS_ENV=local

贡献者 ✨

Thanks goes to these wonderful people:


fatrbaby

📖

TELstatic

📖

wujunze

📖

ifcute

📖

RobberPhex

📖

Contributions of any kind are welcome!

感谢nacos团队赠送的纪念杯

php-nacos's People

Contributors

blueoo avatar fatrbaby avatar ifcute avatar neatlife avatar telstatic avatar wujunze avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

php-nacos's Issues

请教:本地写入文件的目的

请教一下,如若定时任务写入本地文件,也就是平时正常读取配置或者说单次请求的粒度内还是读取本地文件么

还有监听在php下会是怎样的一个使用场景

Model::decode()容错处理方案

此方法会遍历$instanceJson参数并将值渲染到调用此方法的子类属性,如果$instanceJson和子类的属性不匹配,则会报找不到方法的错误

原方法如下

    public static function decode($instanceJson)
    {
        $instance = new static();
        foreach (json_decode($instanceJson) as $propertyName => $propertyValue) {
            $instance->{"set" . ucfirst($propertyName)}($propertyValue);
        }
        return $instance;
    }

方法改进

    public static function decode($instanceJson)
    {
        $instance = new static();

        foreach (json_decode($instanceJson, true) as $propertyName => $propertyValue) {
            $method = "set" . ucfirst($propertyName);
            // 在此处增加判断方法是否存在判断,如$instanceJson返回多余参数,则不进行渲染
            if (method_exists($instance, $method)) {
                $instance->{$method}($propertyValue);
            }
        }

        return $instance;
    }

listInstances方法请求地址404

服务发现 官方demo:
curl -X GET 'http://127.0.0.1:8848/nacos/v1/ns/instances?serviceName=nacos.naming.serviceName'

问题:
// 服务注册
$naming = Naming::init(
"qyd-fe-api1",
"172.16.69.152",
"8000",
"84b71387-76e1-43ae-b3c3-e14776455e0a",
"",
false
);

$naming->register();

$instanceList = $naming->listInstances();

listInstances方法请求地址404。

nacos-client.ERROR: 查询实例列表异常,开始从本地获取配置, message: Client error: GET http://11.0.21.13:8040/nacos/v1/ns/instance/list?serviceName=qyd-fe-api1&namespaceId=&clusters=&healthyOnly=0 resulted in a 404 Not Found response: dom not found: qyd-fe-api1 [] []

获取服务下的健康的实例列表

NacosConfig::setHost("http://127.0.0.1:8848/"); // 配置中心地址
//$healthyOnly值需要传递字符串的true,否则会导致传参的时候被http_build_query转为1导致nacos服务不识别此参数
$instances = NamingClient::listInstances("test-service", "true");

nacos api地址:
https://nacos.io/zh-cn/docs/open-api.html

如果获取不到健康实例请注意下

拉取配置保存文件路径

作者你好,为什么从 Nacos 拉取下来的配置文件在源码中写死了最后保存文件的路径,不可以在用户使用 setSnapshotPath 设置保存路径时完全按照用户的来嘛,这样子直接保存到项目根目录的 .env 文件中就完成了配置更新

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.