sensiolabs/consul-php-sdk
最新稳定版本:v5.3.0
Composer 安装命令:
composer require sensiolabs/consul-php-sdk
包简介
SDK to talk with consul.io API
关键字:
README 文档
README
Consul PHP SDK is a thin wrapper around the Consul HTTP API.
Compatibility
See previous version of README.md to find some version compatible with older version of symfony/http-client or guzzle
Installation
This library can be installed with composer:
composer require friendsofphp/consul-php-sdk
Supported services
- agent
- catalog
- health
- kv
- session
- txn
Usage
Instantiate a services, and start using it:
$kv = new Consul\Services\KV(); $kv->put('test/foo/bar', 'bazinga'); $kv->get('test/foo/bar', ['raw' => true]); $kv->delete('test/foo/bar');
A service exposes few methods mapped from the consul API:
All services methods follow the same convention:
$response = $service->method($mandatoryArgument, $someOptions);
- All API mandatory arguments are placed as first;
- All API optional arguments are directly mapped from
$someOptions; - All methods return a
Consul\ConsulResponse; - If the API responds with a 4xx response, a
Consul\Exception\ClientExceptionis thrown; - If the API responds with a 5xx response, a
Consul\Exception\ServeExceptionis thrown.
Cookbook
How to acquire an exclusive lock?
$session = new Consul\Services\Session(); $sessionId = $session->create()->json()['ID']; // Lock a key / value with the current session $lockAcquired = $kv->put('tests/session/a-lock', 'a value', ['acquire' => $sessionId])->json(); if (false === $lockAcquired) { $session->destroy($sessionId); echo "The lock is already acquire by another node.\n"; exit(1); } echo "Do you jobs here...."; sleep(5); echo "End\n"; $kv->delete('tests/session/a-lock'); $session->destroy($sessionId);
How to use MultiLockHandler?
$resources = ['resource1', 'resource2']; $multiLockHandler = new MultiLockHandler($resources, 60, new Session(), new KV(), 'my/lock/'); if ($multiLockHandler->lock()) { try { echo "Do you jobs here...."; } finally { $multiLockHandler->release(); } }
How to use MultiSemaphore?
$resources = [ new Resource('resource1', 2, 7), new Resource('resource2', 3, 6), new Resource('resource3', 1, 1), ]; $semaphore = new MultiSemaphore($resources, 60, new Session(), new KV(), 'my/semaphore'); if ($semaphore->acquire()) { try { echo "Do you jobs here...."; } finally { $semaphore->release(); } }
Some utilities
Consul\Helper\LockHandler: Simple class that implement a distributed lockConsul\Helper\MultiLockHandler: Simple class that implements a distributed lock for many resourcesConsul\Helper\MultiSemaphore: Simple class that implements a distributed semaphore for many resources
Run the test suite
You need a consul agent running on localhost:8500.
But you ca override this address:
export CONSUL_HTTP_ADDR=172.17.0.2:8500
If you don't want to install Consul locally you can use a docker container:
docker run -d --name=dev-consul -e CONSUL_BIND_INTERFACE=eth0 consul
Then, run the test suite
vendor/bin/simple-phpunit
sensiolabs/consul-php-sdk 适用场景与选型建议
sensiolabs/consul-php-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.83M 次下载、GitHub Stars 达 317, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「sdk」 「consul」 「hashicorp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sensiolabs/consul-php-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sensiolabs/consul-php-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sensiolabs/consul-php-sdk 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Consul
A PHP parser for Hashicorp's Terraform plans
Library to communicate with Hashicorp Vault
An easy Laravel integration for HashiCorp Vault
Consul SDK
consul service factory
统计信息
- 总下载量: 2.83M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 319
- 点击次数: 24
- 依赖项目数: 27
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04