一、下载grpc源码
如果你的电脑/服务器可以做代理,然后稳定链接上 GitHub 那么完全可以按照 GitHub 的官方文档来操作,我这里采用 Gitee 镜像来操作
git clone https://gitee.com/jiangxy__loey/grpc.git

二、下载依赖库
进入grpc 目录,然后里面有一个 .gitmodules 文件,我们将其替换成 gitee 的链接,首先清空内容
> .gitmodules
然后再将新内容填入:
[submodule "third_party/zlib"]
path = third_party/zlib
url = https://gitee.com/jiangxy__loey/zlib.git
ignore = dirty
[submodule "third_party/protobuf"]
path = third_party/protobuf
url = https://gitee.com/jiangxy__loey/protobuf.git
[submodule "third_party/gflags"]
path = third_party/gflags
url = https://gitee.com/jiangxy__loey/gflags.git
[submodule "third_party/benchmark"]
path = third_party/benchmark
url = https://gitee.com/jiangxy__loey/benchmark.git
[submodule "third_party/boringssl-with-bazel"]
path = third_party/boringssl-with-bazel
url = https://gitee.com/jiangxy__loey/boringssl-with-bazel.git
[submodule "third_party/re2"]
path = third_party/re2
url = https://gitee.com/hejuncheng1/re2.git
[submodule "third_party/cares/cares"]
path = third_party/cares/cares
url = https://gitee.com/jiangxy__loey/cares.git
[submodule "third_party/bloaty"]
path = third_party/bloaty
url = https://gitee.com/jiangxy__loey/bloaty.git
[submodule "third_party/abseil-cpp"]
path = third_party/abseil-cpp
url = https://gitee.com/jiangxy__loey/abseil-cpp.git
branch = lts_2020_02_25
[submodule "third_party/envoy-api"]
path = third_party/envoy-api
url = https://gitee.com/jiangxy__loey/envoy-api.git
[submodule "third_party/googleapis"]
path = third_party/googleapis
url = https://gitee.com/jiangxy__loey/googleapis.git
[submodule "third_party/protoc-gen-validate"]
path = third_party/protoc-gen-validate
url = https://gitee.com/jiangxy__loey/protoc-gen-validate.git
[submodule "third_party/udpa"]
path = third_party/udpa
url = https://gitee.com/jiangxy__loey/udpa.git
[submodule "third_party/libuv"]
path = third_party/libuv
url = https://gitee.com/jiangxy__loey/libuv.git
然后下载:
git submodule update --init

当然如果如果说你的电脑不能科学上网或者上面的操作报错了,说明电脑网络有问题,可以直接下载这个继续操作: 链接:https://pan.baidu.com/s/1sPFO5jXKBJnbNQwiWCLQEw?pwd=wmak 提取码:wmak
四、安装工具和环境依赖
sudo apt-get install pkg-config
sudo apt-get install autoconf automake libtool make g++ unzip
sudo apt-get install libgflags-dev libgtest-dev
sudo apt-get install clang libc++-dev
五、编译protobuf
进入grpc/third_party/protobuf 目录,然后给 autogen.sh 文件加上执行权限:
chmod +x autogen.sh
因为是在 gitee 下载的文件,所以文本格式为 PC ,我们需要将其转化为 unix ,否则编译会报错,我们通过借助dos2unix 工具转换
apt install dos2unix

- 在
grpc/third_party/protobuf 目录下对所有文件进行格式转换
dos2unix *

此时格式替换完成,我们就可以开始编译 protobuf 了,执行 autogen.sh 脚本即可
./autogen.sh

假设新建库的输出路径 /usr/local 设置库的输出路径
./configure --prefix=/usr/local
开始编译(可能会编的有点久,看个人机器)
make
make check
sudo make install
sudo ldconfig
protoc --version
显示3.19.4
注意,在这一步可能会遇到找不到gawk 的情况 我们只需要apt安装即可
apt install gawk
编译完成后此时我们进入/home/mangata/grpc_learn/grpc/protobuf ,然后输出一下当前的目录结构:

到这里, protobuf 编译完成,接下来就是编译 grpc
六、编译grpc
我们回到 grpc/ 这个路径,然后执行下面的操作即可完成
mkdir -p cmake/build
cd cmake/build
cmake ../..
make
sudo make install
在sudo make install 报错了,那就去 /usr/local/share/man/ 目录将man1 删掉,并且创建一个man1 的文件夹  
七、测试
进入grpc/examples/cpp/helloworld 目录,然后创建build 文件夹
cd grpc/examples/cpp/helloworld/
mkdir build
cd build/
cmake ..
make
进入 build 文件夹cd build ,然后cmake .. ,最后输入 make


启动服务和客户端
./greeter_server
Server listening on 0.0.0.0:50051
./greeter_client
Greeter received: Hello world
 看到回应 Hello world 就完成了,接下来就是学习 grpc 的四种模式了,下期再见~
|