rstgroup/php-consul-kv-array-getter

这个库允许你以结构化数组的形式接收Consul的KV存储的键。

1.0 2017-07-26 09:59 UTC

This package is not auto-updated.

Last update: 2024-09-15 05:02:49 UTC


README

Build Status

这个库做什么?

该库允许你从Consul的KV存储中检索整个属性树。检索到的数据被分组到嵌套数组中。

如何安装它?

使用Composer要求包

composer require rstgroup/php-consul-kv-array-getter

如何使用这个库?

你只需要一个 SensioLabs\Consul\Services\KVInterface 的实例,并将其传递给 RstGroup\PhpConsulKVArrayGetter\Consul\ConsulArrayGetter 构造函数

use SensioLabs\Consul\ServiceFactory;
use SensioLabs\Consul\Services\KVInterface;
use RstGroup\PhpConsulKVArrayGetter\Consul\ConsulArrayGetter;

// your consul options:
$consulParams = [
    'base_uri' => 'http://consul-domain:8500'
];

// prepare service that talks to Consul KV
$consulServicesFactory = new ServiceFactory($consulParams);
$kvService = $consulServicesFactory->get(KVInterface::class);

// create getter instance
$consulArrayGetter = new ConsulArrayGetter(
    $kvService
);

// get the keys as structure
$result = $consulArrayGetter->getByPrefix('prefix');

键如何映射到返回结构?

假设我们在KV存储中有一系列的键

application/db/host      => 'some host'
application/cache/prefix => 'abcd.prefix'
application/name         => 'app'

如果我们使用前缀 'application' 获取配置,我们将收到

$consulArrayGetter->getByPrefix('application') == [
    'application' => [
        'db' => [ 'host' => 'some host' ],
        'cache' => [ 'prefix' => 'abcd.prefix' ],
        'name' => 'app'
    ]
]

如果你在末尾添加斜杠,结果数组将相对于 application 键创建

$consulArrayGetter->getByPrefix('application/') == [
    'db' => [ 'host' => 'some host' ],
    'cache' => [ 'prefix' => 'abcd.prefix' ],
    'name' => 'app'
]

在斜杠后添加另一个键部分将只返回匹配给定前缀的键

$consulArrayGetter->getByPrefix('application/db') == [
    'db' => [ 'host' => 'some host' ]
]