A C++ library that implements the Consul API.
A this moment only some endpoints and actions are supported:
- Register and deregister services with HTTP or gPRC health checks
- Set, get and delete key/values
- Create and delete sessions
- Leader election using sessions
Example:
#include <consulcpp/ConsulCpp>
int main( int argc, char * argv[] )
{
consulcpp::Consul consul;
if( consulcpp::Consul consul; consul.connect() ) {
consulcpp::Service service;
consulcpp::ServiceCheck check;
service.mName = "demo";
service.mAddress = consul.address();
service.mPort = 9990;
check.mInterval = "10s";
check.mGRPC = fmt::format( "{}:{}/Health", service.mAddress, service.mPort );
service.mChecks = { check };
consul.services().create( service );
consulcpp::Session session = consul.sessions().create();
if( consulcpp::Leader::Status leader = consul.leader().acquire( service, session ); leader == consulcpp::Leader::Status::Yes ){
// I'm the leader
}else{
// I'm a follower
}
...
consul.leader().release( service, session );
consul.sessions().destroy( session );
consul.services().destroy( service );
}
return 0;
}
The library uses beast internally to access the Consul API. For now the use is inefficient.
Use cmake to build it on Windows, macOS and Linux.
./vcpkg install boost fmt uriparser nlohmann-json spdlog protobuf grpc ms-gsl tl-expected catch2
find_package(consulcpp CONFIG REQUIRED)
target_link_libraries(main PRIVATE consulcpp)
- Ppconsul: C++ client for Consul. You should probably use this instead :)
- oatpp-consul: C++ Consul integration for oatpp applications
- Install and add to the path both build-wrapper and sonar-scanner
- Set the SONAR_TOKEN environment variable
- Use the sonarqube target:
cmake --build build --target sonarqube