dcarbone/php-consul-api
Composer 安装命令:
composer require dcarbone/php-consul-api
包简介
PHP client for the Consul HTTP API
README 文档
README
PHP client for the Consul HTTP API
This library is loosely based upon the official GO client.
PHP Compatibility
v3.xrequires PHP 8.2+.
Version Compatibility
| PHPConsulAPI Version | Consul Version |
|---|---|
| 0.3.x | 0.6.4 |
| 0.6.x | 0.7-0.8 |
| v1.x | 0.9-current |
| v2.x | 0.9-current |
| v3.x | 0.9-current |
| dev-main | current |
Newer versions of the api lib will probably work in a limited capacity with older versions of Consul, but no guarantee is made and backwards compatibility issues will not be addressed.
V3 Breaking Changes
There are a couple breaking changes between v2 and v3:
- The
FakeMapclass has been removed. - The
FakeSliceclass has been removed. - The
ReadableDurationclass has been removed. - All models now have parameterized constructors.
- For the life of V3 I will continue to support construction from associative arrays, but the parameterized constructors are the preferred method of construction.
- Construction via associative array will be removed entirely in V4 (whenever I get around to that).
- All of that
Transcodingnonsense has been removed. - The root
Configclass may no longer be constructed with a map. You must use constructor parameters. - Map-like fields are represented in PHP as associative arrays (for example,
array<string,mixed>); some JSON object inputs may still be accepted as\stdClassat setter boundaries.
Composer
This lib is designed to be used with Composer
Require Entry:
{
"require": {
"dcarbone/php-consul-api": "^v3.0"
}
}
Configuration
First, construct a Config. This class is modeled quite closely after the Config Struct present in the Consul API Subpackage.
Default Configuration
If you have defined some of the Consul Environment Variables on your hosts then it would probably be easiest to simply execute the following:
$config = \DCarbone\PHPConsulAPI\Config::newDefaultConfig();
Advanced Configuration
You may alternatively define values yourself:
$config = new \DCarbone\PHPConsulAPI\Config( // required fields HttpClient: $client, // [required] Client conforming to GuzzleHttp\ClientInterface Address: 'address of server', // [required] // optional fields Scheme: 'http or https', // [optional] defaults to "http" Datacenter: 'name of datacenter', // [optional] HttpAuth: 'user:pass', // [optional] WaitTime: '0s', // [optional] amount of time to wait on certain blockable endpoints. go time duration string format. Token: 'auth token', // [optional] default auth token to use TokenFile: 'file with auth token', // [optional] file containing auth token string InsecureSkipVerify: false, // [optional] if set to true, ignores all SSL validation CAFile: '', // [optional] path to ca cert file, see http://docs.guzzlephp.org/en/latest/request-options.html#verify CertFile: '', // [optional] path to client public key. if set, requires KeyFile also be set KeyFile: '', // [optional] path to client private key. if set, requires CertFile also be set // php specific options JSONEncodeOpts: JSON_UNESCAPED_SLASHES, JSONDecodeMaxDepth: 512, JSONDecodeOpts: 0, );
Configuration Note:
By default, this client will attempt to locate a series of environment variables to describe much of the above configuration properties. See here for that list, and see here for a list of the env var names.
For more advanced client configuration, such as proxy configuration, you must construct your own GuzzleHttp client prior to constructing a PHPConsulAPI Config object.
As an example:
$proxyClient = new \GuzzleHttp\Client(['proxy' => 'whatever proxy you want']); $config = new \DCarbone\PHPConsulAPI\Config( HttpClient: $proxyClient, Address: 'address of server', );
When constructing your client, if you are using the GuzzleHttp\Client object directly or derivative thereof, you may
pass any option listed in the Guzzle Request Options.
Consul
Next, construct a Consul object:
$consul = new \DCarbone\PHPConsulAPI\Consul($config);
NOTE: If you do not create your own config object, Consul will create it's own using Config::newDefaultConfig() and attempt to locate a suitable HTTP Client.
Once constructed, you interact with each Consul API via it's corresponding Client class:
$kvResp = $consul->KV->Keys(); if (null !== $kvResp->Err) { die($kvResp->Err); } var_dump($kvResp->Value);
...as an example.
Current Clients
More will be added as time goes on!
Tests
The testing suite is still in it's infancy, however it is being tested directly against an actual Consul agent. They will be back-filled as time allows. Future plans are to set up a simple cluster to provide a more real-world testing scenario.
dcarbone/php-consul-api 适用场景与选型建议
dcarbone/php-consul-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 407.33k 次下载、GitHub Stars 达 79, 最近一次更新时间为 2016 年 07 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「consul」 「consul-api」 「php-consul-api」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dcarbone/php-consul-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dcarbone/php-consul-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dcarbone/php-consul-api 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Consul
Consul SDK
consul service factory
A SimpleSAMLphp module adding support for Consul KV.
A Consul Client.
PHP SDK to communicate with Consul API for Laravel
统计信息
- 总下载量: 407.33k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 81
- 点击次数: 9
- 依赖项目数: 10
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2016-07-08