elemenx/apollo-client

PHP版本的Apollo客户端,基于multilinguals/apollo-php-client进行分支开发

0.1.7 2021-01-21 12:25 UTC

This package is auto-updated.

Last update: 2024-09-21 20:56:27 UTC


README

安装

PHP版本 >= 7.0

$ composer require elemenx/apollo-client

PHP版本 >= 5.4 , <7.0

$ composer require elemenx/apollo-client --ignore-platform-reqs

特性

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

使用方法

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

客户端示例代码

#!/usr/bin/env php
<?php
require 'vender/autoload.php'; // autoload
use ElemenX\ApolloClient\ApolloClient;

//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

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