Ubuntu18.04安装gRPC-Go的详细步骤(2021/10)

Ubuntu18.04安装gRPC-Go的详细步骤

1、首先需要安装protobuf,在国内网络环境下使用go get命令是不行的,所以需要使用git clone手动安装。

git clone https://github.com/golang/protobuf.git $GOPATH/src/github.com/golang/protobuf
cd $GOPATH/src/github.com/golang/protobuf
go install ./proto
go install ./protoc-gen-go

Ubuntu18.04安装gRPC-Go的详细步骤(2021/10)
然后切换到$GOPATH/src/github.com/golang/protobuf进行安装
Ubuntu18.04安装gRPC-Go的详细步骤(2021/10)
2.安装grpc-go以及相关依赖

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
git clone https://github.com/protocolbuffers/protobuf-go.git $GOPATH/src/google.golang.org
git clone https://github.com/google/go-genproto.git $GOPATH/src/google.golang.org/genproto
git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc

cd $GOPATH/src/
go install google.golang.org/grpc

Ubuntu18.04安装gRPC-Go的详细步骤(2021/10)
如果安装的过程中提示缺少什么组件,直接git clone相关组件就好,我第一次安装的时候就提示如下。
Ubuntu18.04安装gRPC-Go的详细步骤(2021/10)

3.运行官方示例测试是否安装成功

先在工作目录下克隆grpc-go

git clone -b v1.30.0 https://github.com/grpc/grpc-go

Ubuntu18.04安装gRPC-Go的详细步骤(2021/10)
然后再切换到示例server目录
Ubuntu18.04安装gRPC-Go的详细步骤(2021/10)
运行server示例:

Ubuntu18.04安装gRPC-Go的详细步骤(2021/10)
我第一次运行的时候发现连接不了$GOPROXY,国内访问最好将$GOPROXY设置为国内的,否则会导致访问不了,使用go env -w GOPROXY=https://goproxy.cn,direct进行设置,但是如果之前设置过会出现如下的警告。

Ubuntu18.04安装gRPC-Go的详细步骤(2021/10)
使用unset GOPROXY先删除GOPROXY环境变量,再重新设置。
Ubuntu18.04安装gRPC-Go的详细步骤(2021/10)
然后就可以先将server跑起来
Ubuntu18.04安装gRPC-Go的详细步骤(2021/10)
再将client跑起来
Ubuntu18.04安装gRPC-Go的详细步骤(2021/10)
看到hello world说明安装成功了。

上一篇:Spring Cloud 六《Spring Cloud+gRPC+Nacos》


下一篇:GRPC环境配置