由于 gRPC 以及依赖的第三方库都是托管在 Github 上,并且源码包总共加起来有 1GB 大小,众所周知,国内从 Github 下载源码速度慢且不稳定,过程令人抓狂。值得庆幸的是,码云(gitee.com)已对其中部分仓库作了镜像同步,每日自动同步一次,都在码云极速下载。因此,我们可借助 gitee.com 实现高速下载 gRPC 的相关源码,但下载时需要做一些适当的调整。操作如下。
- 下载 grpc 源码
可通过 gitee.com 先下载 grpc 主框架源码
git clone git@gitee.com:mirrors/grpc.git
- 修改 grpc 依赖的第三方库下载地址
进入前面下载的 grpc 主框架源码目录,修改 .gitmodules ,只需修改其中的 url 参数,其他保持不变,注意并非每个第三方库都在 gitee.com 上有镜像仓库,没有的保持不变。参考如下
➜ grpc-framework git:(786ebf69aa) ✗ more .gitmodules
[submodule "third_party/zlib"]
path = third_party/zlib
#url = https://github.com/madler/zlib.git
url = https://gitee.com/mirrors/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://github.com/google/protobuf.git
#url = https://gitee.com/githubplus/protobuf.git
[submodule "third_party/gflags"]
path = third_party/gflags
# url = https://github.com/gflags/gflags.git
url = https://gitee.com/mirrors/gflags.git
[submodule "third_party/googletest"]
path = third_party/googletest
# url = https://github.com/google/googletest.git
url = https://gitee.com/mirrors/googletest.git
[submodule "third_party/benchmark"]
path = third_party/benchmark
url = https://github.com/google/benchmark
[submodule "third_party/boringssl-with-bazel"]
path = third_party/boringssl-with-bazel
# url = https://github.com/google/boringssl.git
url = https://gitee.com/mirrors/boringssl.git
[submodule "third_party/cares/cares"]
path = third_party/cares/cares
# url = https://github.com/c-ares/c-ares.git
url = https://gitee.com/mirrors/c-ares.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/githubplus/bloaty.git
[submodule "third_party/abseil-cpp"]
path = third_party/abseil-cpp
# url = https://github.com/abseil/abseil-cpp.git
url = https://gitee.com/mirrors/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/githubplus/data-plane-api.git
[submodule "third_party/googleapis"]
path = third_party/googleapis
# url = https://github.com/googleapis/googleapis.git
url = https://gitee.com/mirrors/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/mirrors/protoc-gen-validate.git
[submodule "third_party/udpa"]
path = third_party/udpa
url = https://github.com/cncf/udpa.git
[submodule "third_party/libuv"]
path = third_party/libuv
# url = https://github.com/libuv/libuv.git
url = https://gitee.com/mirrors/libuv.git
更新 git submodule 配置
git submodule sync
更新第三方库源码
git submodule update --init
特殊说明一下,我的 zlib 库一开始没有自动下载到 third_party/zlib 目录,导致后续 grpc 编译失败。重新调整了 .gitmodules ,临时注释了 ignore 参数,再重新更新第三方库,才下载到 zlib 库。grpc 会校验依赖的第三方库的版本是否兼容,因此,不要随便用旧版本的第三方库。 后续的 grpc 安装过程本文就不再详述。