Interaction component with etcd (A distributed, reliable key-value store for the most critical data of a distributed system) for Yii2 Framework.
- PHP: >= 8.5
composer require yii2-extensions/etcd "^2.0.0"or add
"yii2-extensions/etcd": "^2.0.0"
to the require section of your composer.json file.
- etcd >= 3.7 (API v3)
version
range(getKey,getRange)putdeleteRangetxncompact
authenticateenabledisablestatususerAdd,userGet,userList,userDelete,userChangePassworduserGrantRole,userRevokeRoleroleAdd,roleGet,roleList,roleDeleteroleGrantPermission,roleRevokePermission
memberAddmemberRemovememberUpdatememberListmemberPromote
alarmstatusdefragmenthashhashKvmoveLeaderdowngrade
grantrevoketimeToLiveleases
Configure
$config = [
'components' => [
'etcd' => [
'class' => \Yii2\Extensions\Etcd\Etcd::class,
'host' => 'etcd:2379',
'user' => 'username',
'password' => 'password',
],
],
];Get key value
Yii::$app->etcd->getKv()->getKey('hello')->firstKeyValue;
// HelloGet etcd version
Yii::$app->etcd->version;
// {"etcdserver":"3.7.4","etcdcluster":"3.7.0","storage":"3.7.0"}Since 2.0.0 all etcd API methods are grouped by domain and reached through a sub-service:
// KV
Yii::$app->etcd->getKv()->put('foo', 'bar');
Yii::$app->etcd->getKv()->getKey('foo')->firstKeyValue;
Yii::$app->etcd->getKv()->getRange('foo', 'fop');
Yii::$app->etcd->getKv()->deleteRange('foo', 'fop');
Yii::$app->etcd->getKv()->txn($compare, $success, $failure);
Yii::$app->etcd->getKv()->compact($revision);
// Auth
Yii::$app->etcd->getAuth()->authStatus();
Yii::$app->etcd->getAuth()->userAdd('alice', 'pw');
Yii::$app->etcd->getAuth()->roleGrantPermission('viewer', PermissionType::READ, '/foo', '/fop');
// Cluster
Yii::$app->etcd->getCluster()->memberList();
// Maintenance
Yii::$app->etcd->getMaintenance()->status();
Yii::$app->etcd->getMaintenance()->hash();
// Lease
Yii::$app->etcd->getLease()->leaseGrant(3600);
Yii::$app->etcd->getLease()->leaseRevoke($id);Since 2.0.0 the methods that return a non-scalar result return a typed response
object from \Yii2\Extensions\Etcd\Responses\ (e.g. DeleteRangeResponse,
TxnResponse, StatusResponse, LeaseGrantResponse) instead of a plain array.
Nested structures stay arrays, top-level fields are exposed as properties:
$result = Yii::$app->etcd->getKv()->deleteRange('foo', 'fop');
$result->deleted; // number of deleted keys
$result->prevKvs; // previous key-value pairs (when prev_kv is set)
$grant = Yii::$app->etcd->getLease()->leaseGrant(3600);
$grant->ID; // new lease IDetcd v3 uses gRPC for its messaging protocol. For languages with no gRPC support, etcd provides a JSON gRPC gateway. This gateway serves a RESTful proxy that translates HTTP/JSON requests into gRPC messages.
$config = [
'components' => [
'etcd' => [
'class' => \Yii2\Extensions\Etcd\Etcd::class,
...
'protocol' => \Yii2\Extensions\Etcd\EtcdProtocol::GRPC, // Default value \Yii2\Extensions\Etcd\EtcdProtocol::HTTP
],
],
];