Download the PHP package sensiolabs/consul-php-sdk without Composer
On this page you can find all versions of the php package sensiolabs/consul-php-sdk. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sensiolabs/consul-php-sdk
More information about sensiolabs/consul-php-sdk
Files in sensiolabs/consul-php-sdk
Package consul-php-sdk
Short Description SDK to talk with consul.io API
License MIT
Informations about the package consul-php-sdk
Consul PHP SDK
A thin, no-magic PHP wrapper around the Consul HTTP API, built on top of Symfony HttpClient.
โจ Features
- ๐งญ The whole Consul CE HTTP API: 17 services, from KV to ACL, service mesh and operator endpoints
- ๐๏ธ Key/Value store, sessions and transactions
- ๐ฉบ Service discovery: agent, catalog, health and prepared queries
- ๐ Distributed locks and semaphores, ready to use
- ๐ชถ Lightweight: only depends on
symfony/http-clientandpsr/log - ๐ Pluggable: bring your own HTTP client and PSR-3 logger
๐ฆ Installation
๐ Usage
Configuration
By default, services talk to the agent on http://127.0.0.1:8500. The address
can be changed with the CONSUL_HTTP_ADDR environment variable (the same one
the Consul CLI uses), or with the base_uri option:
The first argument of Client accepts any
Symfony HttpClient option,
which comes handy to send an ACL token:
The token can also be set with the CONSUL_HTTP_TOKEN environment variable.
You can also pass a PSR-3 logger, and your own HttpClientInterface instance:
Available services
| Service | Consul API |
|---|---|
Consul\Services\ACL |
/v1/acl |
Consul\Services\Agent |
/v1/agent |
Consul\Services\Catalog |
/v1/catalog |
Consul\Services\Config |
/v1/config |
Consul\Services\Connect |
/v1/connect |
Consul\Services\Coordinate |
/v1/coordinate |
Consul\Services\DiscoveryChain |
/v1/discovery-chain |
Consul\Services\Event |
/v1/event |
Consul\Services\Health |
/v1/health |
Consul\Services\KV |
/v1/kv |
Consul\Services\Operator |
/v1/operator |
Consul\Services\Peering |
/v1/peering |
Consul\Services\PreparedQuery |
/v1/query |
Consul\Services\Session |
/v1/session |
Consul\Services\Snapshot |
/v1/snapshot |
Consul\Services\Status |
/v1/status |
Consul\Services\TXN |
/v1/txn |
Conventions
All services follow the same convention:
- Mandatory API arguments come first;
- Optional API arguments are passed in the
$optionsarray, with the same name as in the Consul documentation. Use an array for multi-valued arguments, e.g.['tag' => ['v1', 'primary']]; - Every method returns a
Consul\ConsulResponse, which exposesgetBody(),json(),getHeaders(),getStatusCode()andisSuccessful(); - A
4xxresponse throws aConsul\Exception\ClientException; - A
5xxresponse, or a network error, throws aConsul\Exception\ServerException.
Both exceptions implement Consul\Exception\ConsulExceptionInterface:
๐ณ Cookbook
Register a service with a health check
Read many keys at once
Run a transaction
Back up the cluster
Acquire an exclusive lock
LockHandler takes a lock on a key, and releases it automatically at the end
of the script:
Lock many resources at once
MultiLockHandler locks all resources, or none of them:
Use a distributed semaphore
MultiSemaphore allows up to limit concurrent holders per resource. Each
Resource is defined by a name, the number of slots to acquire, and a limit:
๐งฉ Compatibility
| Version | PHP | symfony/http-client |
|---|---|---|
| 5.4 | โฅ 8.2 | 6.4, 7.4, 8.1+ |
| 5.3 | โฅ 8.1 | 5.4, 6.4, 7.x, 8.x |
Looking for Guzzle support, or older versions of PHP? Check the CHANGELOG and this older README.
๐งช Running the test suite
The test suite needs a Consul agent listening on localhost:8500 (or on
CONSUL_HTTP_ADDR), with ACLs enabled and root as management token. The
easiest way is to use Docker:
Then run:
๐ License
This library is released under the MIT license.