转载: http://dreamlikes.cn/archives/555
gRPC 是一个高性能、开源和通用的 RPC 框架,面向移动和 HTTP/2 设计。目前提供 C、Java 和 Go 语言版本,分别是:grpc, grpc-java, grpc-go. 其中 C 版本支持 C, C++, Node.js, Python, Ruby, Objective-C, PHP 和 C#。
gRPC 基于 HTTP/2 标准设计,带来诸如双向流、流控、头部压缩、单 TCP 连接上的多复用请求等特。这些特性使得其在移动设备上表现更好,更省电和节省空间占用。
测试环境:
系统是新安装的CentOS 7.2
|
[root@localhost~]# cat /etc/redhat-release
CentOS Linux release7.2.1511(Core)
|
一、YUM安装相关工具
|
yum install-ygcc-c++autoconf libtool
yum groupinstall-y"Development Tools"
|
二、下载gRPC源码和相关子模块
这步可能出现失败或者网速慢的情况(原因你懂的),多执行几次直到完全下载完毕为止。
|
git clonehttps://github.com/grpc/grpc.git
cdgrpc
git submodule update--init
|
三、编译安装protobuf
这个步骤有个地方要下载googlecode的里面的代码,国内正常来说是下载不下来的(除非有梯子),所以要换个下载地址:
|
[root@localhost grpc]# cd third_party/protobuf/
[root@localhost protobuf]# vim autogen.sh
|
找到这行:
|
curl$curlopts-Ohttps://googlemock.googlecode.com/files/gmock-1.7.0.zip
|
修改为:
|
curl$curlopts -L-ogmock-1.7.0.ziphttps://github.com/peter-wangxu/gMock/archive/1.7.0.zip
|
然后编译安装即可:
|
[root@localhost protobuf]# ./autogen
[root@localhost protobuf]# ./configure
[root@localhost protobuf]# make
[root@localhost protobuf]# make install
[root@localhost protobuf]# ldconfig # refresh shared library cache.
[root@localhost protobuf]# which protoc
/usr/local/bin/protoc
[root@localhost protobuf]# protoc --version
libprotoc3.0.0
|
四、编译安装gRPC
|
[root@localhost protobuf]# cd ../..
[root@localhost grpc]# make
[root@localhost grpc]# make install
|
执行make install的时候,我这里有错误提示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
Warning:it looks like protoc3.0.0+isn't installed on your system,
which means that you won'tbe able tocompile.proto files foruse
with gRPC.
Ifyou are just using pre-compiled protocol buffers,oryou otherwise
have no need tocompile.proto files,you can ignore this.
Ifyou doneed protobuf forsome reason,you can download andinstall
it from:
https://github.com/google/protobuf/releases
Once you've done so,you can re-run thischeck by doing:
make verify-install
|
明明是有安装protoc的,但是没检测到,网上找了资料说这个问题好像是不影响的。
五、Demo测试
官方自带有个HelloWorld的Demo,可以编译这个Demo看是否我们已经安装成功了。
|
[root@localhost grpc]# cd examples/cpp/helloworld/
[root@localhost helloworld]# make
|
结果报错了:
|
Packagegrpc++was notfound inthe 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'
tothe PKG_CONFIG_PATH environment variable
|
意思是找不到环境变量PKG_CONFIG_PATH,解决方法是把环境变量加上去:
在gRPC目录下保存文件actviate.sh:
|
DIR="$( cd "$(dirname"${BASH_SOURCE[0]}")" && pwd )"
export PATH=$PATH:$DIR/bins/opt:$DIR/bins/opt/protobuf
export CPATH=$DIR/include:$DIR/third_party/protobuf/src
export LIBRARY_PATH=$DIR/libs/opt:$DIR/libs/opt/protobuf
export PKG_CONFIG_PATH=$DIR/libs/opt/pkgconfig:$DIR/third_party/protobuf
export LD_LIBRARY_PATH=$DIR/libs/opt
|
然后执行:
|
[root@localhost grpc]# source actviate.sh
[root@localhost grpc]# cd examples/cpp/helloworld/
[root@localhost helloworld]# make
|
编译成功。(注意,每次新开终端编译或运行程序,都要执行actviate.sh,除非把环境变量设成永久有效)
然后运行服务端,监听的是50051端口:
|
[root@localhost helloworld]# ./greeter_server
I071909:09:11.798702503 5076ev_epoll_linux.c:85] epoll engine will be using signal:36
D071909:09:11.798857929 5076ev_posix.c:106] Using polling engine:epoll
Server listening on0.0.0.0:50051
|
在另外一个终端执行客户端程序:
|
[root@localhost helloworld]# ./greeter_client
I071909:10:04.431843293 5142ev_epoll_linux.c:85] epoll engine will be using signal:36
D071909:10:04.432006262 5142ev_posix.c:106] Using polling engine:epoll
Greeter received:Hello world
|
成功!
分享到:
相关推荐
CentOS 7 下安装 gRPC for C++ 及其应用详解 本文主要介绍了在 CentOS 7 环境下安装 gRPC for C++ 的步骤,并详细解释了 gRPC 的概念、特性和应用场景。 一、gRPC 概述 gRPC 是一个高性能、开源和通用的 RPC 框架...
grpc-all-1.48.0.tar.gz 和 cpprestsdk-all-2.10.18.tar.gz 在 CentOS7上使用 g++9.3 编译后的头文件和库文件,其中 etcd-cpp-apiv3 依赖的 boost 和 openssl 库都拷贝到了压缩包中,可以直接移植到其他服务器上使用...
在本教程中,我们将详细介绍如何在您的系统上安装gRPC环境,编译gRPC项目,配置gRPC与Oracle数据库的连接,并通过OCCI进行测试。 首先,让我们了解gRPC的工作原理。gRPC基于RPC(Remote Procedure Call)模型,允许...
要求Linux:Ubuntu,Debian,CentOS等macOS(OS X):可能有效,但未经测试码头工人您还可以在docker中使用它,它基于gcc 7.5.0: docker pull chronolaw/cpp_studydocker run -it --rm chronolaw/cpp_study参考 ...
- **protobuf-cpp**:如果要使用gRPC或者相关协议,可能需要protobuf库。 - **OpenID Connect模块**:如果要支持OpenID Connect身份验证,需要相关的库和开发文件。 7. **操作系统兼容性**: - 在不同的Linux...