Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/FriendsOfPHP/consul-php-sdk
PHP Consul SDK
https://github.com/FriendsOfPHP/consul-php-sdk
consul consul-sdk php sdk
Last synced: about 1 month ago
JSON representation
PHP Consul SDK
- Host: GitHub
- URL: https://github.com/FriendsOfPHP/consul-php-sdk
- Owner: FriendsOfPHP
- License: mit
- Created: 2014-11-17T13:10:57.000Z (about 10 years ago)
- Default Branch: main
- Last Pushed: 2024-03-04T08:05:08.000Z (9 months ago)
- Last Synced: 2024-10-30T02:31:15.339Z (about 1 month ago)
- Topics: consul, consul-sdk, php, sdk
- Language: PHP
- Homepage:
- Size: 29.9 MB
- Stars: 316
- Watchers: 18
- Forks: 56
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-consul - FriendsOfPHP/consul-php-sdk
README
# Consul PHP SDK
Consul PHP SDK is a thin wrapper around the [Consul](https://consul.io/) HTTP API.
## Compatibility
See previous version of
[README.md](https://github.com/FriendsOfPHP/consul-php-sdk/tree/404366acbce4285d08126c0a55ace84c10e361d1)
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:
```php
$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](https://consul.io/docs/agent/http.html):
**All services methods follow the same convention:**
```php
$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?
```php
$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?
```php
$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?
```php
$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
```