Skip to content
Cetcd is a C client library for etcd with full features support
Branch: master
Clone or download
shafreeck Merge pull request #45 from thkukuk/ldflags
LDFLAGS needs to be at the end of the gcc command line, not at the be…
Latest commit ebf4d36 Feb 28, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples
sds Fix g++ errors Aug 7, 2015
.gitignore Add ignore entries Sep 8, 2015
LICENSE Initial commit Jul 22, 2015
Makefile Fix typo Jan 5, 2017
README.md Add examples to README.md May 5, 2016
cetcd.c Revert "Don't export SDS API and install header file." Feb 14, 2017
cetcd.h Revert "Don't export SDS API and install header file." Feb 14, 2017
cetcd_array.c Fix array.* typo Aug 23, 2015
cetcd_array.h Fix #27, fix the api signature May 15, 2016
cetcd_json_parser.h Add https support Oct 12, 2015
wercker.yml Use ubuntu as build box Aug 22, 2015

README.md

Cetcd is a C client for etcd

License Gitter Stories in Ready wercker status

Table of Contents generated with DocToc

Status

cetcd is on active development. It aims to be used in production environment and to supply full features of etcd. Any issues or pull requests are welcome!

Features

  • Round-robin load balance and failover
  • Full support for etcd keys space apis
  • Multiple concurrent watchers support

Deps

cetcd use sds as a dynamic string utility. It is licensed in sds/LICENSE. sds is interaged in cetcd's source code, so you don't have to install it before.

yajl is a powerful json stream parsing libaray. We use the stream apis to parse response from cetcd. It is already integrated as a third-party dependency, so you are not necessary to install it before.

curl is required to issue HTTP requests in cetcd

Install

Install curl if needed on Ubuntu

apt-get install libcurl4-openssl-dev

or on CentOS

yum install libcurl-devel

then

make
make install

It default installs to /usr/local.

Use

make install prefix=/path

to specify your custom path.

Link

Use -lcetcd to link the library

Examples

Usage

cetcd_array is an expandable dynamic array. It is used to pass etcd cluster addresses, and return cetcd response nodes

Create an array to store the etcd addresses

    cetcd_array addrs;

    cetcd_array_init(&addrs, 3);
    cetcd_array_append(&addrs, "127.0.0.1:2379");
    cetcd_array_append(&addrs, "127.0.0.1:2378");
    cetcd_array_append(&addrs, "127.0.0.1:2377");

cetcd_client is a context cetcd uses to issue requests, you should init it with etcd addresses

Init the cetcd_client

    cetcd_client cli;
    cetcd_client_init(&cli, &addrs);

Then you can issue an cetcd request which reply with an cetcd response

Set a key

    cetcd_response *resp;
    resp = cetcd_set(&cli, "/service/redis", "hello cetcd", 0);
    if(resp->err) {
        printf("error :%d, %s (%s)\n", resp->err->ecode, resp->err->message, resp->err->cause);
    }
    cetcd_response_release(resp);

Get a key

    cetcd_response *resp;
    resp = cetcd_get(&cli, "/service/redis");
    if(resp->err) {
        printf("error :%d, %s (%s)\n", resp->err->ecode, resp->err->message, resp->err->cause);
    }
    cetcd_response_release(resp);

List a directory

    cetcd_response *resp;
    resp = cetcd_lsdir(&cli, "/service", 1, 1);
    if(resp->err) {
        printf("error :%d, %s (%s)\n", resp->err->ecode, resp->err->message, resp->err->cause);
    }
    cetcd_response_print(resp);
    cetcd_response_release(resp);

Clean all resources

    cetcd_array_destory(&addrs);
    cetcd_client_destroy(&cli);

See examples/cetcdget.c for more detailes

You can’t perform that action at this time.