dev-master
9999999-devPHP library to interact with the consul.io service
MIT
The Requires
- php >= 5.5
- guzzle/guzzle ~3.9
PHP library to interact with the consul.io service
This is a PHP library for the consul.io application. Note that this is in development right now. Feel free to open PRs., (*2)
$ composer require "br/consul-php"
Depending on which part of the library you want to use, instantiate the correct Client:, (*3)
$client = new \BR\Consul\Agent('http://localhost:8500'); // to issue commands to the local agent $client = new \BR\Consul\KeyValueStore('http://localhost:8500'); // to access the Key Value Store
Use the getValue()
, setValue()
and deleteValue()
functions of the client. Example:, (*4)
$client->setValue('myKey', 'myValue'); $client->setValue('myKey', 'myValue', 'datacenter-east'); // Set the value in the datacenter-east datacenter $client->getValue('myKey'); $client->getValue('myKey', 'datacenter-east'); // Get the value from the datacenter-east datacenter $client->deleteValue('myKey'); $client->deleteValue('myKey', 'datacenter-east'); // Delete the value from the datacenter-east datacenter
In order to register a service, you have to create a BR\Consul\Model\Service
object, and pass it to the
Client::registerService()
function. Example:, (*5)
$service = new \BR\Consul\Model\Service(); $service->setName('postgres'); $service->setId('postgres-1'); $service->setTags(['master']); $service->setPort(5432); $success = $client->registerService($service);
You can retrieve a list of services that are registered with the agent. Example:, (*6)
$services = $service->getServices(); var_dump(count($services)); /** $srv \BR\Consul\Model\Service */ foreach ($services as $srv) { echo $srv->getName() }
$client->deregisterService('postgres-1'); // alternatively $client->removeService($service);
$datacenters = $client->getDatacenters(); var_dump(count($datacenters)); /** $datacenter \BR\Consul\Model\Datacenter */ foreach ($datacenters as $datacenter) { echo $datacenter->getName(); }
Run the unit and functional tests by running phpunit
in the root of the repository., (*7)
To run consul, use:, (*8)
$ ./consul agent -data-dir . --bootstrap -server
PHP library to interact with the consul.io service
MIT