承接 sensiolabs/consul-php-sdk 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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\ClientException is thrown;
  • If the API responds with a 5xx response, a Consul\Exception\ServeException is 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 lock
  • Consul\Helper\MultiLockHandler: Simple class that implements a distributed lock for many resources
  • Consul\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 我们能提供哪些服务?
定制开发 / 二次开发

基于 sensiolabs/consul-php-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.83M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 319
  • 点击次数: 24
  • 依赖项目数: 27
  • 推荐数: 1

GitHub 信息

  • Stars: 317
  • Watchers: 14
  • Forks: 55
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04