etcd-cpp-apiv3编译笔记
etcd-cpp-apiv3编译笔记
etcd-cpp-apiv3是etcd的c++版本客户端api,项目地址为
https://github.com/etcd-cpp-apiv3/etcd-cpp-apiv3
由于readme文档中没详细对此库的编译进行说明,踩了一些坑,遂记录一下。
1.依赖
boost
protobuf
gRPC
cpprestsdk
2.编译
2.1安装相关依赖
此次仅介绍Ubuntu的方法,如下
sudo apt install -y ca-certificates ccache cmake libboost-all-dev libcurl4-openssl-dev libgrpc-dev libgrpc++-dev libprotobuf-dev libssl-dev libz-dev lsb-release protobuf-compiler-grpc screenfetch wget
2.2安装编译cpprestsdk
$ git clone https://github.com/microsoft/cpprestsdk.git
$ cd cpprestsdk
$ mkdir build && cd build
$ cmake .. -DBUILD_TESTS=ON -DBUILD_SAMPLES=ON -DCPPREST_EXCLUDE_WEBSOCKETS=ON -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
$ make -j
$ sudo make install
2.3安装编译etcd-cpp-apiv3
$ git clone https://github.com/etcd-cpp-apiv3/etcd-cpp-apiv3.git
$ cd etcd-cpp-apiv3
$ mkdir build && cd build
$ cmake .. -DBUILD_ETCD_TESTS=ON -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
$ make -j
$ sudo make install
3.测试
编写一个demo
#include <etcd/Watcher.hpp>
#include <string>
#include <iostream>
std::string str_url = "http://192.168.1.104:12379";
std::string str_key = "/server";
void watcher_cb(etcd::Response const & resp)
{
if (resp.error_code())
{
//error
std::cout<<"watcher_cb error_code:"<<resp.error_code()<<" and error";
std::cout <<" msg:"<<resp.error_message()<<std::endl;
}
else
{
//success
if(resp.action() == "set")
{
std::cout<<"set key:"<<resp.key(resp.index())<<" value:"<<resp.value(resp.index()).as_string();
}
else if(resp.action() == "delete")
{
std::cout<<"set key:"<<resp.key(resp.index())<<" value:"<<resp.value(resp.index()).as_string();
}
}
}
int main()
{
etcd::Watcher watcher(str_url,str_key,watcher_cb,true);
getchar();
watcher.Cancel();
return 0;
}
编译
$ g++ main.cpp -o main -lpthread -letcd-cpp-api -lprotobuf -lgrpc++ -lgrpc -lz -lcpprest -lssl -lcrypto -lboost_system
$ ./main