aternos/etcd

etcd v3 的 PHP gRPC 客户端

v1.5.0 2022-06-07 11:45 UTC

This package is auto-updated.

Last update: 2024-08-29 12:31:39 UTC


README

使用 gRPC 为 etcd v3 提供完整和/或简化客户端。

关于

此库包括从 etcd 存储库中的 .proto 文件生成的 gRPC 类,位于 grpc 目录中。您可以使用(更复杂的)gRPC 类或更简单的 Client 类来实现基本功能。目前,Client 类中只实现了少数几个基本功能,因此当您使用 gRPC 类实现并创建 pull request 时,请随意添加更多功能。

安装

要使用此库,必须安装 gRPC PHP 扩展。有关完整说明,请参阅此链接

sudo apt install php php-dev php-pear
sudo pecl install grpc

并将 extension=grpc.so 添加到 php.ini 文件中。

不需要 protobuf 扩展,因为它是此库的依赖项,您可以使用以下方法安装:

composer require aternos/etcd

使用方法

客户端类

<?php

$client = new Aternos\Etcd\Client();
$client = new Aternos\Etcd\Client("localhost:2379");
$client = new Aternos\Etcd\Client("localhost:2379", "username", "password");

// currently implemented functions
$client->put("key", "value");
$client->get("key");
$client->delete("key");
$client->putIf("key", "newValue", "valueToCompareWith");
$client->deleteIf("key", "valueToCompareWith");

// complex transaction example
$leaseId = $client->getLeaseID(10);
$putOp = $client->getPutOperation('key', 'someValueToPutOnSuccess', $leaseId);
$getOp = $client->getGetOperation('key');
// following compare checks for key existence
$compare = $client->getCompare('key', '0', \Etcdserverpb\Compare\CompareResult::EQUAL, \Etcdserverpb\Compare\CompareTarget::MOD);
// execute Put operation and return the key we stored, just return the key value if it already exists
$txnResponse = $client->txnRequest([$putOp, $getOp], [$getOp], [$compare]);
$result = $client->getResponses($txnResponse, 'response_range', true);
// $result[0] contains "someValueToPutOnSuccess"

分片客户端

<?php

$clients = [
    new Aternos\Etcd\Client("hostA:2379"),
    new Aternos\Etcd\Client("hostB:2379"),
    new Aternos\Etcd\Client("hostC:2379")
];
$shardedClient = new Aternos\Etcd\ShardedClient($clients);

$shardedClient->put("key", "value");
$shardedClient->get("key");

故障转移客户端

  • 在 etcd 主机失败时自动且透明地故障转移
<?php

$clients = [
    new Aternos\Etcd\Client("hostA:2379"),
    new Aternos\Etcd\Client("hostB:2379"),
    new Aternos\Etcd\Client("hostC:2379")
];
$failoverClient = new Aternos\Etcd\FailoverClient($clients);

// set 60 seconds as a hold-off period between another connection attempt to the failing host 
// default is 120 seconds
// failing host is being remembered within FailoverClient object instance 
$failoverClient->setHoldoffTime(60);
$failoverClient->put("key", "value");
$failoverClient->get("key");