Giter VIP home page Giter VIP logo

consul-php-sdk's Introduction

Consul SDK

Compatibility

This table shows this SDK compatibility regarding Guzzle version:

SDK Version Guzzle Version
1.x >=4, <6
2.x 6

Installation

This library can be installed with composer:

composer require sensiolabs/consul-php-sdk

Usage

The simple way to use this SDK, is to instantiate the service factory:

$sf = new SensioLabs\Consul\ServiceFactory();

Then, a service could be retrieve from this factory:

$kv = $sf->get('kv');

Then, a service expose few methods mapped from the consul API:

$kv->put('test/foo/bar', 'bazinga');
$kv->get('test/foo/bar', ['raw' => true]);
$kv->delete('test/foo/bar');

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 raw guzzle response.

So if you want to acquire an exclusive lock:

// Start a 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);

Available services

  • agent
  • catalog
  • health
  • kv
  • session

Some utilities

  • Lock handler: Simple class that implement a distributed lock

consul-php-sdk's People

Contributors

jippi avatar lyrixx avatar romainneutron avatar tusharkant avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.