打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
CentOS7下安装gRPC(C++环境搭建)

文章来源:点击打开链接

 

 

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 连接上的多复用请求等特。这些特性使得其在移动设备上表现更好,更省电和节省空间占用。

https://github.com/grpc/grpc

https://github.com/google/protobuf

http://doc.oschina.net/grpc

测试环境:

系统是新安装的CentOS 7.2

Shell

1

2

[root@localhost ~]# cat /etc/redhat-release

CentOS Linux release 7.2.1511 (Core)

 

一、YUM安装相关工具

 

Shell

1

2

yum install -y gcc-c++ autoconf libtool

yum groupinstall -y "Development Tools"

 

二、下载gRPC源码和相关子模块

这步可能出现失败或者网速慢的情况(原因你懂的),多执行几次直到完全下载完毕为止。

Shell

1

2

3

git clone https://github.com/grpc/grpc.git

cd grpc

git submodule update --init

firecat注:执行以上三个命令,会自动下载源码,也包含第三方third_party文件夹的全部源码。

也就是说,我们不需要手动去官网https://github.com/grpc/grpc下载源码。因为有第三方库不好操作,不好安装,搞错了会很麻烦。

三、编译安装protobuf

这个步骤有个地方要下载googlecode的里面的代码,国内正常来说是下载不下来的(除非有梯子),所以要换个下载地址:

Shell

1

2

[root@localhost grpc]# cd third_party/protobuf/

[root@localhost protobuf]# vim autogen.sh

找到这行:

Shell

1

curl $curlopts -O https://googlemock.googlecode.com/files/gmock-1.7.0.zip

修改为:

Shell

1

curl $curlopts  -L -o gmock-1.7.0.zip https://github.com/peter-wangxu/gMock/archive/1.7.0.zip

然后编译安装即可:

Shell

1

2

3

4

5

6

7

8

9

[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

libprotoc 3.0.0

firecat注:我的实践结果是不需要自行编译和安装protobuf这个步骤,因为第二步的三个命令已经实现下载了protobuf的源码。

四、编译安装gRPC

 

1

2

3

[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 protoc 3.0.0+ isn't installed on your system,

which means that you won't be able to compile .proto files for use

with gRPC.

 

If you are just using pre-compiled protocol buffers, or you otherwise

have no need to compile .proto files, you can ignore this.

 

If you do need protobuf for some reason, you can download and install

it from:

 

   https://github.com/google/protobuf/releases

 

Once you've done so, you can re-run this check by doing:

 

   make verify-install

明明是有安装protoc的,但是没检测到,网上找了资料说这个问题好像是不影响的。

firecat注:这个步骤顺利通过,没有出现错误提示。

五、Demo测试

官方自带有个HelloWorld的Demo,可以编译这个Demo看是否我们已经安装成功了。

1

2

[root@localhost grpc]# cd examples/cpp/helloworld/

[root@localhost helloworld]# make

结果报错了:

firecat注,我的也出现了这个错误。

1

2

3

4

5

6

7

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

意思是找不到环境变量PKG_CONFIG_PATH,解决方法是把环境变量加上去:

在gRPC目录下新建和保存脚本文件actviate.sh:

1

2

3

4

5

6

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

然后执行:(firecat注:要在同个终端,先执行脚本后执行编译)

Shell

1

2

3

[root@localhost grpc]# source actviate.sh

[root@localhost grpc]# cd examples/cpp/helloworld/

[root@localhost helloworld]# make

编译成功。(注意,每次新开终端编译或运行程序,都要执行actviate.sh,除非把环境变量设成永久有效)

然后运行服务端,监听的是50051端口:

Shell

1

2

3

4

[root@localhost helloworld]# ./greeter_server

I0719 09:09:11.798702503    5076 ev_epoll_linux.c:85]        epoll engine will be using signal: 36

D0719 09:09:11.798857929    5076 ev_posix.c:106]             Using polling engine: epoll

Server listening on 0.0.0.0:50051

在另外一个终端执行客户端程序:

1

2

3

4

[root@localhost helloworld]# ./greeter_client

I0719 09:10:04.431843293    5142 ev_epoll_linux.c:85]        epoll engine will be using signal: 36

D0719 09:10:04.432006262    5142 ev_posix.c:106]             Using polling engine: epoll

Greeter received: Hello world

成功!

另外如果编译时遇到如下错误:

error while loading shared libraries: libprotobuf.so.17: cannot open shared object file: No such file or directory

解决方法:

# find ./ -depth -name "libprotobuf.so.14" -print
一般情况会在/usr/local/lib中,然后:
# cat /etc/ld.so.conf
include ld.so.conf.d/*.conf

# echo "/usr/local/lib" >> /etc/ld.so.conf

# ldconfig

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
CentOS
【Linux笔记】CentOS&RHEL安装vsftp
Centos6安装FreeSWITCH 1.5时./configure问题解决记录
CentOS 6.0 (Final) 安装Oracle 11gR2
CentOS7.3安装VMware Tools
centOS7 安装mysql数据库
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服