【热门云产品免费试用活动】| 【最新活动】| 【企业应用优惠】
简介
gRPC是一个高性能、通用的 开源 RPC框架,其由Google主要面向移动应用开发并基于HTTP/2协议标准而设计,基于ProtoBuf(ProtocolBuffers)序列化协议开发,且支持众多开发语言。 gRPC提供了一种简单的方法来精确地定义服务和为iOS、Android和后台支持服务自动生成可靠性很强的客户端功能库。
配置编译方法
1.获取源码
https://github.com/grpc/grpc使用git clone命令下载当前最新版本的gRPC源码。
2.编译和安装
1)安装gRPC所需的依赖。
yum install -y gcc-c++ autoconf libtool git
2) 获取gRPC的源代码。
cd
git clone https://github.com/grpc/grpc.git
3)进入gRPC文件夹,下载第三方库的软件源码。
cd grpc
git submodule update --init
4)进入gRPC源码的第三方库文件夹中,编译并安装protobuf。
cd /root/grpc/third_party/protobuf
./autogen.sh
./configure
make -j4
make install
5)查询protobuf的版本号。
protoc --version
回显内容如下,若能正常显示protobuf的版本号,则表示protobuf安装成功。
libprotoc 3.11.4
6)接着,进入gRPC源码目录,编译并安装gRPC。
cd /root/grpc
make -j4
make install
3.运行和验证
官方自带 "HelloWorld" 的Demo,通过编译该Demo来验证gRPC是否安装成功。
1) 设置环境变量。
a. 编译“/etc/profile”文件,添加环境变量。
vi /etc /profile
I. 文件末尾添加 “PKG_CONFIG_PATH” 和 “LD_LIBRARY_PATH” 环境变量。
修改内容如下:
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
export LD_LIBRARY_PATH=/usr/local/lib
II. 是环境变量生效。
source /etc/profile
b. 分别查看“PKG_CONFIG_PATH”和“LD_LIBRARY_PATH”环境变量是否修改生效。
echo $PKG_CONFIG_PATH
回显内容如下:
/usr/local/lib/pkgconfig
echo $LD_LIBRARY_PATH
回显内容如下:
/usr/local/lib
2) 验证Demo是否编译成功,且运行正常。
a. 编译源码。
在修改环境变量的同一个终端中,进入Demo文件夹,编译源码。
cd /root/grpc/examples/cpp/helloworld
make
b. 运行 greeter_server。
编译成功之后,在该终端运行服务器端程序greeter_server。
./greeter_server
回显内容如下,监听的是50051端口:
Server listening on 0.0.0.0:50051
c. 运行greeter_client。
打开另一个终端,先使环境变量生效,在执行客户端程序greeter_client。
source /etc/profile
cd /root/grpc/examples/cpp/helloworld
./greeter_client
回显内容如下,则表示gRPC安装成功且能正常使用:
Greeter received: Hello world
问题一:编译过程中提示找不到依赖包。
问题描述:
1 移植指导中编译Demo,执行make命令后,提示找不到相关依赖包的错误,如下:
Package protobuf was not found in the pkg-config search path.
Perhaps you should add the directory containing `protobuf.pc'
to the PKG_CONFIG_PATH environment variable
No package 'protobuf' found
Package grpc++ was not found in the pkg-config search path.
Perhaps you should add the directory containing `grpc++.pc'
to the PKG_CONFIG_PATH environment variable
No package 'grpc++' found
Package grpc was not found in the pkg-config search path.
Perhaps you should add the directory containing `grpc.pc'
to the PKG_CONFIG_PATH environment variable
No package 'grpc' found
/usr/bin/ld: greeter_client.o: undefined reference to symbol '_ZN9grpc_impl26InsecureChannelCredentialsEv'
//usr/local/lib/libgrpc++.so.1: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make: *** [greeter_client] Error 1
问题原因:缺少 “PKG_CONFIG_PATH” 环境变量配置。
解决方法:
请参见1 移植指导设置 “PKG_CONFIG_PATH” 环境变量,确保环境变量生效后,先执行make clean清除上次未编译成功的Demo之后,在重新参见1 移植指导验证Demo是否编译成功,且运行正常。
问题二:编译过程中提示无法加载“libgrpc++.so.” 1的错误。
问题描述:
1 移植指导编译Demo,make编译成功后,执行服务器端程序 .greeter_server时,时无法加载 “libgrpc++.so.1” 的错误,如下:
./greeter_server: error while loading shared libraries: libgrpc++.so.1: cannot open shared object file: No such file or directory
问题原因:缺少 “LD_LIBRARY_PATH” 环境变量配置。
解决方法:请参见1 移植指导设置 “LD_LIBRARY_PATH” 环境变量,,确保环境变量生效后,先执行 make clean 清除上次未编译成功的Demo之后,再重新参见1 移植指导验证Demo是否编译成功,且运行正常。
更多相关文章: