Download the PHP package easyswoole/etcd-client without Composer

On this page you can find all versions of the php package easyswoole/etcd-client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package etcd-client

etcd-client

一个面向 PHP 8.1+ 的 etcd v3 JSON Gateway 客户端。默认使用 easyswoole/http-client 发送请求,提供 KV、租约、认证、用户和角色权限等常用能力。

本项目采用强类型 API,并支持注入自定义 HTTP Transport,便于单元测试和替换底层通信实现。

环境要求

安装

快速开始

Config 参数说明:

参数 类型 默认值 说明
endpoint string http://127.0.0.1:2379 etcd 服务地址,必须使用 HTTP 或 HTTPS
apiVersion string v3 JSON Gateway API 版本
timeout float 10.0 HTTP 请求超时秒数
tls ?TlsConfig HTTPS 时自动创建 TLS 与 mTLS 配置;只能用于 HTTPS 地址

HTTPS 地址即使没有显式传入 TlsConfig,也会自动使用严格证书校验配置。

KV 操作

写入键值

可以通过第三个参数传递 etcd Put 请求选项:

将键绑定到租约:

获取单个键

获取指定范围

获取指定前缀

客户端会根据前缀自动计算 etcd 所需的 range_end:

获取全部键

可以传入 etcd Range 请求支持的附加选项:

删除键或前缀

删除指定范围:

压缩历史版本

租约操作

leaseTimeToLive() 返回的 keys 会自动进行 Base64 解码。

认证

获取并使用 Token

withToken() 不会修改原客户端,而是返回一个携带 Token 的新客户端实例,可避免认证信息意外影响其他请求。

清除 Token 时同样创建新实例:

启用或关闭认证

启用认证前,请先按照 etcd 的要求创建 root 用户和 root 角色,避免服务进入无法管理的状态。

用户管理

角色与权限管理

可用权限枚举:

枚举 etcd 值 说明
Permission::Read 0 只读
Permission::Write 1 只写
Permission::ReadWrite 2 读写

HTTPS 与双向 TLS

使用 HTTPS 时,客户端会显式开启 TLS,即使 etcd 使用的是常见的 2379 而不是 443 端口。默认安全策略如下:

使用私有 CA 和 mTLS

如果通过 IP 地址连接,但证书签发给域名,可以单独指定证书主机名:

也可以指定经哈希处理的 CA 目录:

开发环境中的自签名证书

建议仍然保持证书校验,只允许自签名证书:

只有在临时诊断环境中才应完全关闭服务端证书校验:

禁用 verifyPeer 会使客户端无法确认服务端身份,可能遭受中间人攻击,不应在生产环境使用。

TlsConfig 可用配置项:

配置 默认值 说明
verifyPeer true 验证服务端证书
allowSelfSigned false 是否允许自签名证书;建议优先通过 caFile 信任私有 CA
caFile null CA 证书文件绝对路径
caPath null 已进行哈希处理的 CA 证书目录
clientCertificate null mTLS 客户端证书文件
clientPrivateKey null mTLS 客户端私钥文件,必须与证书同时配置
passphrase null 客户端私钥密码
serverName null TLS 主机名/SNI,例如 etcd.internal

返回值与 Base64 处理

etcd v3 JSON Gateway 要求键和值使用 Base64。客户端会自动处理以下字段:

方法返回 etcd 原始响应结构,只对上述二进制字段做解码,不会移除 header、revision、count 等元数据。例如:

异常处理

网络错误、无效 JSON 和 etcd 非 2xx 响应会转换为客户端异常:

TransportException 继承自 EtcdException。如果不需要分别处理,捕获 EtcdException 即可。

自定义 Transport

可以实现 TransportInterface,用于接入其他 HTTP 客户端、链路追踪或测试替身:

Transport 需要返回包含 HTTP 状态码和响应正文的 TransportResponse。

测试

安装开发依赖:

运行全部测试:

单元测试使用内存 Transport,不依赖网络或真实 etcd 服务。

本机 mock etcd 集成测试

项目提供了一个轻量的本机 mock etcd JSON Gateway,用于验证生产环境使用的 EasySwooleTransport、HTTP 请求、Base64 编解码和响应处理:

集成测试会自动完成以下工作:

  1. 分配一个空闲的本机端口;
  2. 启动 PHP 内置 HTTP 服务;
  3. 模拟 KV 写入、前缀查询、范围删除和认证接口;
  4. 使用真实 EasySwoole HTTP Transport 发起请求;
  5. 测试结束后关闭服务并清理临时状态文件。

因此不需要手动启动 mock 服务,也不需要安装真正的 etcd。

License

Apache-2.0


All versions of etcd-client with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1.0
ext-json Version *
easyswoole/http-client Version ^1.6
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package easyswoole/etcd-client contains the following files

Loading the files please wait ...