CentOS7.6下搭建GRPC环境

2020-02-13  本文已影响0人  我也有键盘

近日有个需求,要从java端调用C++的服务,和团队大佬一番讨论后最后选用grpc。

  1. pre-build
yum install git
yum install -y gcc-c++ autoconf libtool
yum groupinstall -y "Development Tools"

  1. 下载grpc源码及相关子模块
git clone -b v1.18.0 https://github.com/grpc/grpc.git
cd grpc
git submodule update --init
  1. 编译安装protobuf
cd third_party/protobuf/
./autogen
./configure
make
make install
# refresh shared library cache
ldconfig

#检验
protoc --version
  1. 安装grpc
#cd到grpc 目录
cd ../../
make
make install

第四步可能报错,因为third_party下zlib缺少zonf.h. 不要慌,到zlib下执行./configure, 然后重新执行第4步安装grpc

cd third_party/zlib
./configure
  1. 运行demo

安装grpc完成后,编译一下helloworld看看能否运行

cd examples/cpp/helloworld/
make

但是因为缺少PKG_CONFIG_PATH爆错了:

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
...

因此,先配置环境变量. 在grpc目录下新增文件activate.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

然后执行

source activate.sh
cd examples/cpp/helloworld/
make

不出意外的话,编译成功。运行./greeter.server启动服务器

c++服务监听.png
  1. 在java代码中调用(参考博客2)

    java调用demo.png
  1. 可能遇到端口50051打不开导致java端连不上的问题,打开linux防火墙

    firewall-cmd --list-all

    firewall-cmd --add-port=50051/tcp --permanent

    firewall-cmd --reload

  1. 参考博客

    1. 编译安装grpc:http://blog.dreamlikes.cn/archives/555

    2. java调用-demo:https://blog.csdn.net/shan165310175/article/details/86619242

感谢这两篇博客,亲测有用

上一篇 下一篇

猜你喜欢

热点阅读