apollo-php-client

by multilinguals

multilinguals / apollo-php-client

携程Apollo配置中心PHP客户端

134 Stars 40 Forks Last release: almost 2 years ago (v0.1.2) 46 Commits 3 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

携程Apollo的PHP客户端

install

php version >= 7.0

bash
$ composer require multilinguals/apollo-client
php version >= 5.4 , <7.0
bash
$ composer require multilinguals/apollo-client --ignore-platform-reqs

Features

  • 支持apollo配置变更的实时获取
  • 支持拉取配置后自定义的回调处理

Usage

客户端以cli的方式后台启动执行,支持apollo配置的适时获取,并将配置保存在指定的目录供应用程序读取解析

客户端示例代码

#!/usr/bin/env php
//specify address of apollo server
$server = getenv('CONFIG_SERVER'); // get server address from env

//specify your appid at apollo config server $appid = getenv('APPID'); // get appid from env

//specify namespaces of appid at apollo config server $namespaces = getenv('NAMESPACE'); // get namespaces from env $namespaces = explode(',', $namespaces);

$apollo = new ApolloClient($server, $appid, $namespaces);

if ($clientIp = getenv('CLIENTIP')) { $apollo->setClientIp($clientIp); }

ini_set('memory_limit','128M'); $pid = getmypid(); echo "start [$pid]\n"; $restart = true; //auto start if failed do { $error = $apollo->start(); if ($error) echo('error:'.$error."\n"); }while($error && $restart);

配置管理

拉取的配置默认保存在脚本所在目录,每个namespace的配置以

apolloConfig.{$namespaceName}.php
的方式命名保存

Docker环境客户端自启动

在docker的启动脚本中加入启动代码,一般的php容器启动脚本是docker-php-entrypoint

bash
if [ -f "/path/to/start.php" ]; then
    apollo_ps=$(ps -aux | grep -c "php /path/to/start.php")
    if [ $apollo_ps -eq 1 ]; then
        php /path/to/start.php &
    fi
fi

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.