Grpc使用教程
Grpc 是Google发起的一个开源远程过程调用 (Remote procedure call)系统。 该系统基于 HTTP/2 协议传输,使用 Protocol Buffers 作为接口描述语言
安装
安装依赖
sudo apt-get install pkg-config sudo apt-get install autoconf automake libtool make g++ unzip sudo apt-get install libgfalgs-dev libgtest-dev sudo apt-get install clang libc++-dev
下载gRPC
git clone https://github.com/grpc/grpc.git cd grpc git checkout v1.47.1 // 切换到需要的分支 git submodule update --init // 更新第三方源码
安装protobuf源码
cd third_party/protobuf/ git submodule update --init --recursive ./autogen.sh // 生成配置脚本 ./configure --disable-shared --prefix=/usr/local CFLAGS="-fPIC" CXXFLAGS="-fPIC" // 生成静态protobuf库 make -j8 make check sudo make install sudo ldconfig // 更新共享库缓存 which protoc // 查看软件是否安装成功 protoc --version // 检查是否安装成功
直接编译安装压缩包(v1.47.1): 链接: https://pan.baidu.com/s/11SRL9c19dd5alwWwSdUn0Q?pwd=gk7y 提取码: gk7y
安装gRPC
cd ../.. mkdir build && cd build cmake .. make -j8 sudo make install
测试
运行grpc/example中的示例代码
cd example/cpp/helloworld make -j4 ./greeter_server # 开启服务 # 新起一个终端 ./greeter_client # 打印出调用的结果:Hello world
使用
Proto文件定义
CMakeLists写法
cmake_minimum_required(VERSION 3.10.0)
project(master_control)
set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
add_definitions(-DBOOST_BIND_GLOBAL_PLACEHOLDERS)
#查找本地Protobuf模块的库信息
find_package(Protobuf REQUIRED)
set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf)
message(STATUS "Using protobuf ${Protobuf_VERSION}")
#查找本地gRPC模块的库信息
find_package(gRPC CONFIG REQUIRED)
message(STATUS "Using gRPC ${gRPC_VERSION}")
set(GRPCPP_INCLUDE_DIRS $<TARGET_PROPERTY:gRPC::grpc++,INTERFACE_INCLUDE_DIRECTORIES>)
set(GRPCPP_LIBRARIES $<TARGET_PROPERTY:gRPC::grpc++_unsecure,INTERFACE_LINK_LIBRARIES>)
set(GRPCPP_PLUGIN_EXEC $<TARGET_FILE:gRPC::grpc_cpp_plugin>)
set(_GRPC_GRPCPP gRPC::grpc++)
set(_REFLECTION gRPC::grpc++_reflection)
# 将proto文件列出
file(GLOB_RECURSE PROTO_LISTS ${CMAKE_CURRENT_SOURCE_DIR}/proto/*.proto)
set(PROTO_ALL_SRCS)
set(PROTO_ALL_HDRS)
set(GRPC_ALL_SRCS)
set(GRPC_ALL_HDRS)
foreach (PROTO_TMP_FILE ${PROTO_LISTS})
get_filename_component(PROTO_FILE "${PROTO_TMP_FILE}" ABSOLUTE)
get_filename_component(PROTO_PATH "${PROTO_FILE}" PATH)
get_filename_component(PROTO_NAME "${PROTO_FILE}" NAME_WE)
file(RELATIVE_PATH REL_FIL ${PROJECT_SOURCE_DIR} ${PROTO_TMP_FILE})
#message(STATUS "-----------------------------")
#message(STATUS "REL_FIL: ${REL_FIL}")
get_filename_component(DIR ${REL_FIL} DIRECTORY)
#message(STATUS "DIR: ${DIR}")
# generated sources
#代码生成器的目标文件位置
set(PROTO_SRCS "${CMAKE_CURRENT_BINARY_DIR}/${DIR}/${PROTO_NAME}.pb.cc")
set(PROTO_HDRS "${CMAKE_CURRENT_BINARY_DIR}/${DIR}/${PROTO_NAME}.pb.h")
set(GRPC_SRCS "${CMAKE_CURRENT_BINARY_DIR}/${DIR}/${PROTO_NAME}.grpc.pb.cc")
set(GRPC_HDRS "${CMAKE_CURRENT_BINARY_DIR}/${DIR}/${PROTO_NAME}.grpc.pb.h")
list(APPEND PROTO_ALL_SRCS ${PROTO_SRCS})
list(APPEND PROTO_ALL_HDRS ${PROTO_HDRS})
list(APPEND GRPC_ALL_SRCS ${GRPC_SRCS})
list(APPEND GRPC_ALL_HDRS ${GRPC_HDRS})
#代码生成器脚本,根据proto文件生成C++的四个文件
add_custom_command(
OUTPUT "${PROTO_SRCS}" "${PROTO_HDRS}" "${GRPC_SRCS}" "${GRPC_HDRS}"
COMMAND ${Protobuf_PROTOC_EXECUTABLE}
ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}"
--cpp_out "${CMAKE_CURRENT_BINARY_DIR}"
-I "${CMAKE_CURRENT_SOURCE_DIR}"
--plugin=protoc-gen-grpc="${GRPCPP_PLUGIN_EXEC}"
"${PROTO_FILE}"
DEPENDS "${PROTO_FILE}")
endforeach ()
# 将grpc编译成静态库
set(GRPC_LIBS "${PROJECT_NAME}_proto")
add_library(${GRPC_LIBS} STATIC
${PROTO_ALL_SRCS}
${PROTO_ALL_HDRS}
${GRPC_ALL_SRCS}
${GRPC_ALL_HDRS}
)
target_link_libraries(${GRPC_LIBS}
${_REFLECTION}
${_GRPC_GRPCPP}
${_PROTOBUF_LIBPROTOBUF})
include_directories("${CMAKE_CURRENT_BINARY_DIR}")