All Projects → oliora → Ppconsul

oliora / Ppconsul

Licence: bsl-1.0
C++ client for Consul (http://consul.io)

Programming Languages

cpp
1120 projects
cpp11
221 projects

Labels

Projects that are alternatives of or similar to Ppconsul

Consul
Consul - Open Government and E-Participation Web Software
Stars: ✭ 1,088 (+1069.89%)
Mutual labels:  consul
Envoy Control
Envoy Control is a platform-agnostic, production-ready Control Plane for Service Mesh based on Envoy Proxy.
Stars: ✭ 66 (-29.03%)
Mutual labels:  consul
Gomplate
A flexible commandline tool for template rendering. Supports lots of local and remote datasources.
Stars: ✭ 1,270 (+1265.59%)
Mutual labels:  consul
Consul With Docker
The environment of Consul with Docker include Consul Template and Registrator
Stars: ✭ 59 (-36.56%)
Mutual labels:  consul
Terraform Modules
Reusable Terraform modules
Stars: ✭ 63 (-32.26%)
Mutual labels:  consul
Vip Manager
Manages a virtual IP based on state kept in etcd or Consul
Stars: ✭ 75 (-19.35%)
Mutual labels:  consul
Consul Kv Dashboard
Consul KVS based dashboard web application.
Stars: ✭ 49 (-47.31%)
Mutual labels:  consul
Eureka Consul Adapter
This project contains a Spring Boot Starter that registers HTTP endpoints on a Spring Cloud Eureka server to support Prometheus's service discovery mechanism for Consul (<consul_sd_config>)
Stars: ✭ 93 (+0%)
Mutual labels:  consul
Bookstoreapp Distributed Application
Ecommerce project is being developed using Spring Boot Microservices and Spring Cloud (Backend) and React (Frontend). Splitting the Ecommerce functionality into various individual microservices so that they can be distributed, scale really well and make use of resources efficiently.
Stars: ✭ 63 (-32.26%)
Mutual labels:  consul
Mechanist
[Docker] - The Mechanist is a collection of Dockerfiles and Composefiles for various tools and purposes.
Stars: ✭ 86 (-7.53%)
Mutual labels:  consul
Docker Vault
Docker Container for Hashicorp's Vault
Stars: ✭ 60 (-35.48%)
Mutual labels:  consul
Hashi Ui
A modern user interface for @hashicorp Consul & Nomad
Stars: ✭ 1,119 (+1103.23%)
Mutual labels:  consul
Helm
A native Scala client for interacting with Consul
Stars: ✭ 76 (-18.28%)
Mutual labels:  consul
Aspnetcore.services
Examples for ASP.NET Core webservices
Stars: ✭ 59 (-36.56%)
Mutual labels:  consul
Learning Tools
A collection of tools and files for learning new technologies
Stars: ✭ 1,287 (+1283.87%)
Mutual labels:  consul
Containerpilot
A service for autodiscovery and configuration of applications running in containers
Stars: ✭ 1,078 (+1059.14%)
Mutual labels:  consul
Kardia
A humane service status API module to expose any operational/internals of any Node.js based microservice. JSON format over HTTP protocol.
Stars: ✭ 70 (-24.73%)
Mutual labels:  consul
My Cheat Sheets
A place to keep all my cheat sheets for the complete development of ASIC/FPGA hardware or a software app/service.
Stars: ✭ 94 (+1.08%)
Mutual labels:  consul
Nsq Go
Go package providing tools for building NSQ clients, servers and middleware.
Stars: ✭ 90 (-3.23%)
Mutual labels:  consul
Python Consul2
Python client for the Consul HTTP API ,Continue develop on cablehead/python-consul
Stars: ✭ 85 (-8.6%)
Mutual labels:  consul

Ppconsul

Version 0.2

A C++ client library for Consul. Consul is a distributed tool for discovering and configuring services in your infrastructure.

The goal of Ppconsul is to:

  • Fully cover version 1 of Consul HTTP API. Please check the current implementation status.
  • Provide simple, modular and effective API based on C++11.
  • Support different platforms. At the moment, Linux, Windows and macOS platforms supported.
  • Cover all the code with automated tests.

Note that this project is under development and doesn't promise a stable interface.

Library tests are currently running against Consul v1.4.2. Library is known to work with Consul starting from version 0.4 (earlier versions might work as well but has never been tested) although some tests fail for older versions because of backward incompatible changes in Consul.

The library is written in C++11 and requires a quite modern compiler. Currently it's compiled with:

  • macOS: Clang 11 (Xcode 11.3.1)
  • Ubuntu Linux: GCC 7.4 with stdlibc++
  • Windows: n/a

Oldest versions of compilers that should work (all with using C++11 standard)

  • Clang 5
  • GCC 4.8
  • Visual Studio 2013

I try to support all modern compilers and platforms but I don't have resources to do extensive testing so from time to time something got broken on some platforms (mostly old GCC or Windows issues). Please create an issue if you discover a compilation error on your platform.

The library depends on:

The library includes code of the following 3rd party libraries (check ext directory):

  • json11 library to deal with JSON.
  • libb64 library for base64 decoding.

For unit tests, the library uses Catch2 framework. Many thanks to Phil Nash for this great product.

Warm Up Examples

Register, deregister and report the state of your service in Consul:

#include "ppconsul/agent.h"

using ppconsul::Consul;
using namespace ppconsul::agent;

// Create a consul client that uses default local endpoint `http://127.0.0.1:8500` and default data center
Consul consul;
// We need the 'agent' endpoint for a service registration
Agent agent(consul);

// Register a service with associated HTTP check:
agent.registerService(
    kw::name = "my-service",
    kw::port = 9876,
    kw::tags = {"tcp", "super_server"},
    kw::check = HttpCheck{"http://localhost:80/", std::chrono::seconds(2)}
);

...

// Unregister service
agent.deregisterService("my-service");

...

// Register a service with TTL
agent.registerService(
    kw::name = "my-service",
    kw::port = 9876,
    kw::id = "my-service-1",
    kw::check = TtlCheck{std::chrono::seconds(5)}
);

// Report service is OK
agent.servicePass("my-service-1");

// Report service is failed
agent.serviceFail("my-service-1", "Disk is full");

Determine raft leader (or lack thereof) and raft peers:

#include "ppconsul/status.h"

using ppconsul::Consul;
using namespace ppconsul::status;

// Create a consul client that uses default local endpoint `http://127.0.0.1:8500` and default data center
Consul consul;

// We need the status endpoint
Status status(consul);

// Determine whether a leader has been elected
bool isLeaderElected = status.isLeaderElected();

// Determine the actual raft leader
auto leader = status.leader();

// Determine the raft peers
auto peers = status.peers();

Use Key-Value storage:

#include "ppconsul/kv.h"

using ppconsul::Consul;
using ppconsul::Consistency;
using namespace ppconsul::kv;

Consul consul;

// We need the 'kv' endpoint
Kv kv(consul);

// Read the value of a key from the storage
std::string something = kv.get("settings.something", "default-value");

// Read the value of a key from the storage with consistency mode specified
something = kv.get("settings.something", "default-value", kw::consistency = Consistency::Consistent);

// Erase a key from the storage
kv.erase("settings.something-else");

// Set the value of a key
kv.set("settings.something", "new-value");

Blocking query to Key-Value storage:

// Get key+value+metadata item
KeyValue item = kv.item("status.last-event-id");

// Wait for the item change for no more than 1 minute:
item = kv.item("status.last-event-id", kw::block_for = {std::chrono::minutes(1), item.modifyIndex});

// If key exists, print it:
if (item)
    std::cout << item.key << "=" << item.value << "\n";

Abort all [blocking] queries:

Consul consul(kw::enable_stop = true); // Must be enabled at construction time
Kv kv(consul);

// Issue blocking queries, similarly to example above, on background threads etc.

// Stop all pending requests, e.g. at shutdown. No further requests can be done after this call.
consul.stop();

Call to Consul::stop() is irreversible: once it's done the Consul object is switched to the stopped state forever. This whole feature was made solery for the purpose of timely application shutdown in case when blocking queries are used and should be used accordingly.

Connect to Consul via HTTPS (TLS/SSL, whatever you call it):

#include "ppconsul/consul.h"

using namespace ppconsul;

Consul consul("https://localhost:8080",
              kw::tls::cert="path/to/cert",
              kw::tls::key="path/to/private/key",
              kw::tls::ca_info="path/to/ca/cert");

// Use consul ...

Multi-Threaded Usage of Ppconsul

Each Consul object has a pool of HTTP(S) clients to perform network requests. It is safe to call any endpoint (e.g. Kv, Agent etc) object or Consul object from multiple threads in the same time.

Call to Consul::stop() method stops all ongoing requests on that particular Consul object.

Custom http::HttpClient

If needed, user can implement http::HttpClient interface and pass custom HttpClientFactory to Consul's constructor.

Documentation

TBD

How To Build

You need C++11 compatible compiler (see above for the list of supported compilers) and CMake 3.1 or above.

TLDR

# Install dependencies
conan install .

# Make workspace directory
mkdir workspace
cd workspace

# Configure:
cmake ..

#Build
cmake --build . --config Release

# Install
cmake --build . --config Release --target install

Get dependencies

If you use Conan then simply run conan install . to install dependencies.

Otherwise:

  • Install Boost 1.55 or later. You need compiled Boost.Regex library if you use GCC 4.8, otherwise you need headers only.
  • Install libCURL (any version should be fine).

Build

Configure project:

mkdir workspace
cd workspace
cmake ..

You may want to set the following CMake variables on the command line:

To change where CMake looks for Boost, pass -DBOOST_ROOT=<path_to_boost> parameter to CMake or set BOOST_ROOT environment variable.

To change where CMake looks for libCURL, pass -DCURL_ROOT=<path_to_curl> parameter to CMake or set CURL_ROOT environment variable.

To change default install location, pass -DCMAKE_INSTALL_PREFIX=<prefix> parameter.

To build Ppconsul as static library, pass -DBUILD_STATIC_LIB=ON parameter. Note that in this case you have to link with json11 static library as well (json11 library is build as part of Ppconsul build.)

Note that on Windows Ppconsul can only be built as static library (that's default mode on Windows), see issue Allow to build Ppconsul as dynamic library on Windows.

Note about -G option of CMake to choose you favourite IDE to generate project files for.

Build:

cmake --build . --config Release

If Makefile generator was used then you can also do:

make

How to Install

Build it first as described above then run

cmake --build . --config Release --target install

If Makefile generator was used then you can also do:

make install

How To Run Tests

Install Consul 0.4 or newer. I recommend 0.7 or newer since it's easier to run it in development mode.

Run Consul

For Consul 0.9 and above:

consul agent -dev -datacenter=ppconsul_test -enable-script-checks=true

For Consul 0.7 and 0.8:

consul agent -dev -datacenter=ppconsul_test

For earlier version of Consul follow its documentation on how to run it with ppconsul_test datacenter.

Run Tests

ctest -C Release

If Makefile generator was used then you can also do:

make test

There are the following environment variable to configure tests:

Name Default Value Description
PPCONSUL_TEST_ADDR "127.0.0.1:8500" The Consul network address
PPCONSUL_TEST_DC "ppconsul_test" The Consul datacenter
PPCONSUL_TEST_LEADER_ADDR "127.0.0.1:8300" The Consul raft leader address

Never set PPCONSUL_TEST_DC into a datacenter that you can't throw away because Ppconsul tests will screw it up in many different ways.

Known Problems

Sometimes catalog tests failed on assertion REQUIRE(index1 == resp1.headers().index());. In this case, just rerun the tests. The reason for the failure is Consul's internal idempotent write which cause a spurious wakeup of waiting blocking query. Check the critical note under the blocking queries documentation at https://www.consul.io/docs/agent/http.html.

How to Use Ppconsul in Your Project

Build and install it first as described above.

When installed, the library can be simply used in any CMake-based project as following:

find_package(ppconsul)
add_executable(<your_app> ...)
target_link_libraries(<your_app> ppconsul)

As an alternative you can clone ppconsul into your project as submodule:

git submodule add https://github.com/oliora/ppconsul.git

And then include it into your CMake-based project as subdirectory:

set(BUILD_TESTS OFF)
set(BUILD_STATIC_LIB ON)
add_subdirectory(ppconsul)
...
target_link_libraries(<your_app> ppconsul)

Found a bug? Got a feature request? Need help with Ppconsul?

Use issue tracker or/and drop an email to oliora.

Contribute

First of all, welcome on board!

To contribute, please fork this repo, make your changes and create a pull request.

License

The library released under Boost Software License v1.0.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].