gRPC概述
gRPC 一开始由 google 开发,是一款语言中立、平台中立、开源的远程过程调用(RPC)系统。
目前提供C、Java和Go语言版本,分别是grpc、grpc-java、grpc-go。
gRPC基于HTTP/2标准设计,带来诸如双向流、流控、头部压缩、单TCP连接上的多复用请求等特性。
这些特性使得其在移动设备上表现更好,更省电和节省空间占用。

在 gRPC 里客户端应用可以像调用本地对象一样直接调用另一台不同的机器上服务端应用的方法,使得您能够更容易地创建分布式应用和服务。与许多 RPC 系统类似,gRPC 也是基于以下理念:定义一个服务,指定其能够被远程调用的方法(包含参数和返回类型)。在服务端实现这个接口,并运行一个 gRPC 服务器来处理客户端调用。在客户端拥有一个存根能够像服务端一样的方法。

对于开发者而言的使用方法:
首先需要使用protobuf定义接口,即.proto文件。
然后使用编译工具生成特定的语言执行代码。
启动服务端,通过侦听指定的port,来等待Client链接请求,通常使用Netty来构建,GRPC内置了Netty的支持。
启动一个或者多个Client端,Client也是基于Netty,Client通过与Server建立TCP长链接,并发送请求;Request与Response均被封装成HTTP2的stream Frame,通过Netty Channel进行交互。

gRPC安装与测试
首先是安装一系列的依赖:

  1. 安装openssl
apt remove openssl
 
wget https://www.openssl.org/source/openssl-1.1.1b.tar.gz
 
tar -xvf openssl-1.1.1b.tar.gz
 
cd openssl-1.1.1b
 
./config --prefix=/usr/local --openssldir=/usr/local/openssl
 
make && make install
 
vim ~/.bashrc
 
export PATH=/usr/local/openssl/bin:${PATH}
 
export OPENSSL_ROOT_DIR=/usr/local/openssl
 
export OPENSSL_LIBRARIES=/usr/local/openssl/lib
 
source ~/.bashrc

查看OpenSSL的相关安装信息:

openssl version -a
  1. 安装3.13以上的cmake
apt remove cmake
 
wget https://cmake.org/files/v3.17/cmake-3.17.2.tar.gz
 
tar zxvf cmake-3.17.2.tar.gz
 
cd cmake-3.17.2
 
./configure
 
sudo make && make install

查看版本:

cmake --version
  1. 安装grpc工具
sudo apt install -y build-essential autoconf libtool pkg-config
  1. 下载grpc源码进行安装:
git clone -b v1.28.0 https://github.com/grpc/grpc.git
cd grpc
git submodule update --init

执行git clone https://github.com/grpc/grpc.git时,如果卡在:正克隆到 'grpc'...可能是连不上外网导致的。“Ctrl+c”之后重新执行git clone https://github.com/grpc/grpc.git即可。

执行完git submodule update --init 命令,可能会卡住,别慌Ctrl+C退出即可。
执行下列命令,一个个下载

cd third_party/
git clone  https://gitclone.com/github.com/abseil/abseil-cpp.git
git clone  https://gitclone.com/github.com/google/benchmark.git
git clone  https://gitclone.com/github.com/google/bloaty.git
git clone https://gitclone.com/github.com/google/boringssl.git
git clone https://gitclone.com/github.com/c-ares/c-ares.git
git clone https://gitclone.com/github.com/envoyproxy/data-plane-api.git
git clone https://gitclone.com/github.com/gflags/gflags.git
git clone https://gitclone.com/github.com/googleapis/googleapis.git
git clone https://gitclone.com/github.com/google/googletest.git
git clone https://gitclone.com/github.com/libuv/libuv.git
git clone https://gitclone.com/github.com/google/protobuf.git
git clone https://gitclone.com/github.com/envoyproxy/protoc-gen-validate.git
git clone https://gitclone.com/github.com/cncf/udpa.git
git clone https://gitclone.com/github.com/madler/zlib.git
  1. 安装grpc的absl依赖
apt-get update && apt-get install -y libssl-dev
cd grpc/third_party/abseil-cpp/cmake
mkdir build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_POSITION_INDEPENDENT_CODE=TRUE ..
make install

如果在make install时,提示如下错误:在这里插入图片描述
需要升级gcc和g++的版本,可以参考:gcc/g++版本更换

  1. 安装grpc的c-ares依赖
cd grpc/third_party/cares/cares
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make install
  1. 安装grpc的benchmarks依赖
cd grpc/third_party/benchmarks
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make install
  1. 安装grpc的protobuf依赖

//首先要进入gRPC目录下

cd third_party/protobuf/
git submodule update --init --recursive   //确保克隆子模块,更新第三方源码
sudo ./autogen.sh      //生成配置脚本
sudo ./configure         //生成Makefile文件,为下一步的编译做准备,可以加上安装路径:--prefix=path,默认安装路径为usr/local/bin/
sudo make                 //从Makefile读取指令,然后编译
sudo make install //从Makefile读取指令,安装到指定位置,默认为/usr/local/,也可以指定安装目录:--prefix=path。
sudo ldconfig       // 更新共享库缓存
which protoc       // 查看软件的安装位置
protoc --version   //检查是否安装成功

可以看到显示了protoc的版本

  1. 安装grpc的zlib依赖
cd grpc/third_party/zlib
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make install
  1. 安装gRPC
    从当前目录回到gRPC根目录下,并执行以下命令:
cd ../..
sudo make                          //从Makefile读取指令,然后编译
sudo make install       //从Makefile读取指令,安装到指定位置,默认为/usr/local/,这里可以加上安装路径:prefix=/usr/local/

在sudo make install安装gRPC后出错:Installing via 'make' is no longer supported. Use cmake or bazel instead.
改用

cd grpc
mkdir build
cd build
sudo cmake -DCMAKE_BUILD_TYPE=Release \
  -DgRPC_INSTALL=ON \
  -DgRPC_BUILD_TESTS=OFF \
  -DgRPC_CARES_PROVIDER=package \
  -DgRPC_ABSL_PROVIDER=package \
  -DgRPC_PROTOBUF_PROVIDER=package \
  -DgRPC_SSL_PROVIDER=package \
  -DgRPC_ZLIB_PROVIDER=package ..
sudo make install

11.测试example中的用例,输出helloworld:

cd grpc/examples/cpp/helloworld
make                 //编译
./greeter_server  //服务器
./greeter_client   //客户端 这里要重新打开一个终端窗口。

运行的结果为:
可以在服务端看到:
在这里插入图片描述

服务端监听的url为0.0.0.0:50051
在客户端看到:
在这里插入图片描述

已经收到了helloworld。测试成功。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐