c++程序 引入etcd c++ API 编译安装步骤

etcd-cpp-api is a C++ API for etcd

1、Build Boost Libraryhttps://www.boost.org/doc/libs/1_61_0/more/getting_started/unix-variants.html

1
2
3
4
cd boost_1_61_0
./bootstrap.sh --with-libraries=system,thread,locale,random,chrono,regex,filesystem
./b2 install

要求boost在1.54以上,cmake要求3.0以上 ,gcc在4.8 以上

另外需要安装一些包:

1
yum install openssl-devel go

2、Build Casablanca: https://github.com/Microsoft/cpprestsdk

1
2
3
4
5
6
7
8
9
10
git clone https://github.com/Microsoft/cpprestsdk.git
cd casablanca/Release
mkdir build.debug
cd build.debug
cmake3 .. -DCMAKE_BUILD_TYPE=Debug #-DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=0
make
make install

*g++ -std=c++11 my_file.cpp -o my_file -lboost_system -lcrypto -lssl -lcpprest
./my_file*

因为cpprestsdk源码中中有些变量没有用到,会把警告当错误。 需要注释掉

1
2
cpprestsdk-2.10.1/Release/CMakeLists.txt:14:
#set(WERROR ON CACHE BOOL "Treat Warnings as Errors.")

如若手动指定boost库版本,需要在
vi cpprestsdk-2.10.1/Release/cmake/cpprest_find_boost.cmake
在20行下面添加

1
2
3
20   elseif(UNIX)
21 set(Boost_INCLUDE_DIR /usr/include/boost159)
22 set(Boost_LIBRARIES /usr/lib64/boost159)

3、Install grpc for C++https://github.com/grpc/grpc.git

1
2
3
4
cd grpc
git submodule update --init
make
make install

安装路径在Makefile中配置prefix

4、Install Protobuf :这里使用grpc下自带的第三方protobuf编译安装

1
2
3
4
5
cd grpc/third_party/protobuf/
./autogen.sh
./configure
make -j4
make install

5、Install etcd

源码安装(需要安装go环境)

1
2
3
4
git clone https://github.com/coreos/etcd.git
cd etcd
./build
export ETCDCTL_API=3 #要使用etcd的3版本api需要导入环境变量

或者下载 https://github.com/coreos/etcd/releases etcd-v3.3.8-linux-amd64的二进制包

1
2
3
unzip etcd-v3.3.8-linux-amd64
export PATH=/home/gjf/etcd/etcd-v3.3.8-linux-amd64/etcd:$PATH
export ETCDCTL_API=3

6、etcd-cpp-apiv3 安装https://github.com/nokia/etcd-cpp-apiv3.git

在 etcd-cpp-apiv3-master中如果是自定义头文件或库安装路径的话需要对
CMakeLists.txt 进行更改

1
2
3
4
5
6
7
8
9
vi etcd-cpp-apiv3-master/CMakeLists.txt

set(CPPREST_INCLUDE_DIR /home/jinri/tmp/include)
set(CPPREST_LIB /home/jinri/tmp/lib)
#find_library(CPPREST_LIB NAMES cpprest)
#find_path(CPPREST_INCLUDE_DIR NAMES cpprest/http_client.h)

set(Boost_INCLUDE_DIR /usr/include/boost159)
set(Boost_LIBRARIES /usr/lib64/boost159)
1
2
3
4
5
6
7
8
vi etcd-cpp-apiv3-master/src/CMakeLists.txt

find_library(CPPREST_LIBRARY cpprest /home/jinri/tmp/lib)
find_library(PROTOBUF_LIBRARY protobuf /home/jinri/tmp/lib)
find_library(GRPC++_LIBRARY grpc++ /home/jinri/tmp/lib)
MESSAGE(STATUS "Found third_lib: ${CPPREST_LIBRARY} ${PROTOBUF_LIBRARY} ${GRPC++_LIBRARY}")
#target_link_libraries(etcd-cpp-api boost_system ssl crypto cpprest protobuf grpc++)
target_link_libraries(etcd-cpp-api ${CPPREST_LIBRARY} boost_system ssl crypto ${PROTOBUF_LIBRARY} ${GRPC++_LIBRARY})

etcd-cpp-apiv3-master/tst/CMakeLists.txt一行设置有问题
需改为:

1
find_path(CATCH_INCLUDE_DIR NAMES catch.hpp ..)

把由第三步第四步生成protoc grpc_cpp_plugin 二进制程序(在install后的bin中)拷贝到proto目录,执行相应的命令生成protoc的接口文件

1
2
3
cd etcd-cpp-apiv3-master/proto
./protoc -I . --cpp_out=. ./*.proto
./protoc -I . --grpc_out=. --plugin=protoc-gen-grpc=./grpc_cpp_plugin ./rpc.proto

编译安装最终 libetcd-cpp-api.so Client.hpp Response.hpp SyncClient.hpp Value.hpp Watcher.hpp

1
2
3
4
5
6
cd etcd-cpp-apiv3-master
mkdir build
cd build
cmake3 ..
make
make install

注意事项:

1、grpc需使用git clone下来的master版本,而且还需要下载子模块

2、个别模块中的CMakeLists.txt需要更改,测试用例个别有问题

3、etcd v3的c++ client实现使用的是https://github.com/nokia/etcd-cpp-apiv3上的开源代码,其实现的功能有限且不稳定,增删改查、订阅功能都有,更新租约(保活)、事务操作等都没实现。

4、开源client代码中订阅功能会在grpc通信write阶段发生coredump,参考了https://github.com/nokia/etcd-cpp-apiv3/issues/5进行解决,但没解决根本原因,需要进一步研究grpc调用过程。

5、thrift 中协议有些字段与protobuf 中的字段冲突,引发难以查找的错误。 如文件中的version字段

6 还有就是cpprest中concurrency 与apache中apache::thrift::concurrency命名冲突 ,在cpprest前加上pplx域名空间

6、用c++11 版本编译,std::shared_ptr 与boost::shared_ptr 冲突,后改为用boost::shared——ptr ,本来打算用std::shared_ptr ,单如此的话还有要更改一些第三方问文件,且会与以前版本不兼容

需要更改的头文件 cpprest中出现concurrency的,/usr/include/thrift/config.h中的version或者/usr/local/include/proto/rpc.proto 中version改名字
monitor中所有的shared_ptr 都加上boost命名空间前缀