apollo-sdk-config icon indicating copy to clipboard operation
apollo-sdk-config copied to clipboard

按照代码无法获取相关配置,输出为空

Open hywaljp opened this issue 3 years ago • 3 comments
trafficstars

$client = new Client([ 'config_server_url' => 'http://portalIp:8070/',//Apollo配置服务的地址,必须传入这个参数 //'secret' => 'xxxxxxxxxx',//密钥,如果配置了密钥可以通过 ]);

    $appId = 'T-RNG';
    $namespaceName = 'application';
    $useCacheApi = true;//设置为false可以通过不带缓存的Http接口从Apollo读取配置

    echo '<pre>';
    $config = $client->getConfig($appId, $namespaceName, $useCacheApi);
    if($config === false) {//获取配置失败
        print_r($client->getErrorInfo());//当产生curl错误时此处有值
        print_r($client->getHttpInfo());//可能不是产生curl错误,而是阿波罗接口返回的http状态码不是200或304
    } else {//获取配置成功
        print_r($config);//返回数组结构
    }

hywaljp avatar Jun 09 '22 16:06 hywaljp

试试把这个http://portalIp:8070/改成http://portalIp:8070看看

flylan avatar Jun 13 '22 00:06 flylan

输出一个空数组,多一个斜杠和少一个是一样的。

hywaljp avatar Jun 13 '22 01:06 hywaljp

端口8070 改成8080 就好了

<?php
require 'vendor/autoload.php';
use ApolloSdk\Config\Client;

$client = new Client([
    'config_server_url' => 'http://localhost:8080/',//Apollo配置服务的地址,必须传入这个参数
    //'secret' => 'xxxxxxxxxx',//密钥,如果配置了密钥可以通过
]);

$appId = 'SampleApp'; // 通过docker安装服务后,自带的一个appId
$namespaceName = 'application'; // 默认的namespace
$useCacheApi = true;//设置为false可以通过不带缓存的Http接口从Apollo读取配置

echo '<pre>';
$config = $client->getConfig($appId, $namespaceName, $useCacheApi);
if($config === false) {//获取配置失败
    print_r($client->getErrorInfo());//当产生curl错误时此处有值
    print_r($client->getHttpInfo());//可能不是产生curl错误,而是阿波罗接口返回的http状态码不是200或304
} else {//获取配置成功
    print_r($config);//返回数组结构
}

xuepeng2 avatar Jul 10 '23 09:07 xuepeng2