etcd-cpp-apiv3是etcd的c++版本客戶端api,項目地址爲ios
https://github.com/etcd-cpp-apiv3/etcd-cpp-apiv3
因爲readme文檔中沒詳細對此庫的編譯進行說明,踩了一些坑,遂記錄一下。c++
boost protobuf gRPC cpprestsdk
這次僅介紹Ubuntu的方法,以下git
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
$ 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
$ 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
編寫一個demogithub
#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; }
編譯shell
$ g++ main.cpp -o main -lpthread -letcd-cpp-api -lprotobuf -lgrpc++ -lgrpc -lz -lcpprest -lssl -lcrypto -lboost_system $ ./main