系统环境
cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
安装
gRPC
go get -u google.golang.org/grpc
Protocol Buffers
- 方法一
-
安装依赖
yum install -y gcc-c++ autoconf libtool yum groupinstall -y "Development Tools"
-
安装Protocol Buffers v3
wget https://github.com/google/protobuf/releases/download/v3.5.1/protobuf-all-3.5.1.zip unzip protobuf-all-3.5.1.zip cd protobuf-3.5.1/ ./configure make make install
检测是否安装成功
protoc --version
-
- 方法二(来自官方文档)
- 下载平台对应预编译的二进制文件(protoc- < version > - < platform > .zip):https://github.com/google/protobuf/releases
- 解压文件
- 把二进制文件的路径加入环境变量
PATH
(或者直接丢到$GOPATH/bin
)
安装Protoc Plugin
go get -u github.com/golang/protobuf/protoc-gen-go
遇到的问题
-
生成
.pb.go
文件时报protoc-gen-go
找不到,如下:protoc --go_out=plugins=grpc:. *.proto protoc-gen-go: program not found or is not executable --go_out: protoc-gen-go: Plugin failed with status code 1.
原因是:
protoc-gen-go
需要位于shell路径中,即PATH
环境变量中列出的目录之一,它与Go路径不同。您可以通过protoc-gen-go
在命令行输入来测试这个:如果它显示“未找到命令”(或类似),那么它不在你的PATH
。解决方法:
设置
GOPATH/bin
到环境变量export GOPATH=$HOME/gocode export PATH=$PATH:$GOPATH/bin
-
墙的问题导致包下载,通过GitHub替换
git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc git clone https://github.com/golang/net.git $GOPATH/src/golang.org/x/net git clone https://github.com/golang/text.git $GOPATH/src/golang.org/x/text git clone https://github.com/golang/sys.git $GOPATH/src/golang.org/x/sys go get -u github.com/golang/protobuf/{proto,protoc-gen-go} git clone https://github.com/google/go-genproto.git $GOPATH/src/google.golang.org/genproto cd $GOPATH/src/ go install google.golang.org/grpc
参考
https://www.36nu.com/question/132
https://blog.csdn.net/cyq6239075/article/details/83037585