一、下载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://github.com/madler/zlib
url = https://gitee.com/jiangxy__loey/zlib.git
# When using CMake to build, the zlib submodule ends up with a
# generated file that makes Git consider the submodule dirty. This
# state can be ignored for day-to-day development on gRPC.
ignore = dirty
[submodule "third_party/protobuf"]
path = third_party/protobuf
url = https://gitee.com/jiangxy__loey/protobuf.git
#url = https://github.com/google/protobuf.git
[submodule "third_party/gflags"]
path = third_party/gflags
#url = https://github.com/gflags/gflags.git
url = https://gitee.com/jiangxy__loey/gflags.git
[submodule "third_party/benchmark"]
path = third_party/benchmark
#url = https://github.com/google/benchmark
url = https://gitee.com/jiangxy__loey/benchmark.git
[submodule "third_party/boringssl-with-bazel"]
path = third_party/boringssl-with-bazel
#url = https://github.com/google/boringssl.git
url = https://gitee.com/jiangxy__loey/boringssl-with-bazel.git
[submodule "third_party/re2"]
path = third_party/re2
#url = git://github.com/google/re2.git
url = https://gitee.com/hejuncheng1/re2.git
[submodule "third_party/cares/cares"]
path = third_party/cares/cares
#url = https://github.com/c-ares/c-ares.git
url = https://gitee.com/jiangxy__loey/cares.git
#branch = cares-1_12_0
[submodule "third_party/bloaty"]
path = third_party/bloaty
#url = https://github.com/google/bloaty.git
url = https://gitee.com/jiangxy__loey/bloaty.git
[submodule "third_party/abseil-cpp"]
path = third_party/abseil-cpp
#url = https://github.com/abseil/abseil-cpp.git
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://github.com/envoyproxy/data-plane-api.git
url = https://gitee.com/jiangxy__loey/envoy-api.git
[submodule "third_party/googleapis"]
path = third_party/googleapis
#url = https://github.com/googleapis/googleapis.git
url = https://gitee.com/jiangxy__loey/googleapis.git
[submodule "third_party/protoc-gen-validate"]
path = third_party/protoc-gen-validate
#url = https://github.com/envoyproxy/protoc-gen-validate.git
url = https://gitee.com/jiangxy__loey/protoc-gen-validate.git
[submodule "third_party/udpa"]
path = third_party/udpa
#url = https://github.com/cncf/udpa.git
url = https://gitee.com/jiangxy__loey/udpa.git
[submodule "third_party/libuv"]
path = third_party/libuv
#url = https://github.com/libuv/libuv.git
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
启动服务和客户端
# 启动服务端,监听在50051端口
./greeter_server
Server listening on 0.0.0.0:50051
# 启动客户端,服务端返回Hello world
./greeter_client
Greeter received: Hello world
看到回应 Hello world
就完成了,接下来就是学习 grpc
的四种模式了,下期再见~