但是国内经常会出现如下错误:
$ go get -u google.golang.org/grpc package google.golang.org/grpc: unrecognized import path "google.golang.org/grpc" (https fetch: Get https://google.golang.org/grpc?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
因为google.golang.org在国内很难访问,所以会下载失败。
官网也给了多个解决方案:
https://github.com/grpc/grpc-go
我们采用第二种方法,直接将源码clone到本地
进入到$GOPATH/src目录,执行命令:
git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc
下载速度有时快,有时慢,非常慢的时候可以取消,重新触发,多试几次偶尔会很快。
下载完成后,安装gRPC:
ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src $ go install google.golang.org/grpc/ google.golang.org\grpc\credentials\credentials.go:31:2: cannot find package "github.com/golang/protobuf/proto" in any of: D:\Go\src\github.com\golang\protobuf\proto (from $GOROOT) C:\Users\ASUS\go\src\github.com\golang\protobuf\proto (from $GOPATH) google.golang.org\grpc\internal\binarylog\method_logger.go:28:2: cannot find package "github.com/golang/protobuf/ptypes" in any of: D:\Go\src\github.com\golang\protobuf\ptypes (from $GOROOT) C:\Users\ASUS\go\src\github.com\golang\protobuf\ptypes (from $GOPATH) google.golang.org\grpc\binarylog\grpc_binarylog_v1\binarylog.pb.go:9:2: cannot find package "github.com/golang/protobuf/ptypes/duration" in any of: D:\Go\src\github.com\golang\protobuf\ptypes\duration (from $GOROOT) C:\Users\ASUS\go\src\github.com\golang\protobuf\ptypes\duration (from $GOPATH) google.golang.org\grpc\binarylog\grpc_binarylog_v1\binarylog.pb.go:10:2: cannot find package "github.com/golang/protobuf/ptypes/timestamp" in any of: D:\Go\src\github.com\golang\protobuf\ptypes\timestamp (from $GOROOT) C:\Users\ASUS\go\src\github.com\golang\protobuf\ptypes\timestamp (from $GOPATH) google.golang.org\grpc\internal\transport\controlbuf.go:28:2: cannot find package "golang.org/x/net/http2" in any of: D:\Go\src\golang.org\x\net\http2 (from $GOROOT) C:\Users\ASUS\go\src\golang.org\x\net\http2 (from $GOPATH) google.golang.org\grpc\internal\transport\controlbuf.go:29:2: cannot find package "golang.org/x/net/http2/hpack" in any of: D:\Go\src\golang.org\x\net\http2\hpack (from $GOROOT) C:\Users\ASUS\go\src\golang.org\x\net\http2\hpack (from $GOPATH) google.golang.org\grpc\server.go:36:2: cannot find package "golang.org/x/net/trace" in any of: D:\Go\src\golang.org\x\net\trace (from $GOROOT) C:\Users\ASUS\go\src\golang.org\x\net\trace (from $GOPATH) google.golang.org\grpc\status\status.go:34:2: cannot find package "google.golang.org/genproto/googleapis/rpc/status" in any of: D:\Go\src\google.golang.org\genproto\googleapis\rpc\status (from $GOROOT) C:\Users\ASUS\go\src\google.golang.org\genproto\googleapis\rpc\status (from $GOPATH)
可以发现会有很多错误,根据提示可以发现是由于缺少包的原因,这里就不一点点分析错误信息了,直接给出所需的依赖包以及下载方法(在$GOPATH/src目录下执行命令):
1)text包
git clone https://github.com/golang/text.git ./golang.org/x/text
2)net包
git clone https://github.com/golang/net.git ./golang.org/x/net
3)genproto包
git clone https://github.com/google/go-genproto.git ./google.golang.org/genproto
4)protobuf包
两个:
git clone https://github.com/protocolbuffers/protobuf-go.git ./google.golang.org/protobuf
git clone https://github.com/golang/protobuf.git ./github.com/golang/protobuf
都要下,github.com/golang/protobuf的代码有的依赖google.golang.org/protobuf
以上依赖库都下载完成后后,重新安装gRPC:
ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src $ go install google.golang.org/grpc/ ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src $
可见已经没有错误了,也没啥输出。。。
验证gRPC是否OK
启两个bash窗口,分别执行如下指令:
go run google.golang.org/grpc/examples/helloworld/greeter_server/main.go
go run google.golang.org/grpc/examples/helloworld/greeter_client/main.go
$GOPATHgoogle.golang.orggrpcexampleshelloworldhelloworld目录中有如下文件:
ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master) $ ll total 16 -rw-r--r-- 1 ASUS 197121 4938 6月 1 21:46 helloworld.pb.go -rw-r--r-- 1 ASUS 197121 1208 6月 1 21:46 helloworld.proto -rw-r--r-- 1 ASUS 197121 2823 6月 1 21:46 helloworld_grpc.pb.go
我们先将.go文件备份一下
ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master) $ mv helloworld.pb.go helloworld.pb.go.bak ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master) $ mv helloworld_grpc.pb.go helloworld_grpc.pb.go.bak ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master) $ ll total 16 -rw-r--r-- 1 ASUS 197121 4938 6月 1 21:46 helloworld.pb.go.bak -rw-r--r-- 1 ASUS 197121 1208 6月 1 21:46 helloworld.proto -rw-r--r-- 1 ASUS 197121 2823 6月 1 21:46 helloworld_grpc.pb.go.bak
然后执行:
ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master) $ protoc --go_out=plugins=grpc:. helloworld.proto 'protoc-gen-go' ????????????????????????е???? ????????????? --go_out: protoc-gen-go: Plugin failed with status code 1.
发现有错误,需要安装protoc-gen-go
执行如下命令安装:
ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src $ go install github.com/golang/protobuf/protoc-gen-go/ ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src $ cd ../bin ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/bin $ ll total 11852 -rwxr-xr-x 1 ASUS 197121 3702272 5月 27 07:06 protoc.exe* -rwxr-xr-x 1 ASUS 197121 8431104 6月 1 23:13 protoc-gen-go.exe*
安装完成后,在$GOPATH/bin目录下会生成protoc-gen-go.exe文件
然后再执行编译proto文件:
会生成一个helloworld.pb.go的文件
有几个库,需要了解下:
1、https://github.com/protocolbuffers/protobuf
这个是google开源的protobuf源码库,这个库里面包含了常用的各种语言实现protobuf的源码
2、https://github.com/golang/protobuf
这个库是golang的protobuf开源库,查看这个库的README.md可以发现,这个库被google.golang.org/protobuf
替代了,点开这个链接可以发现,这个库对应的源码git仓库为:
https://github.com/protocolbuffers/protobuf-go
目前看开源社区,github的protobuf会逐渐被google.golang.org的库替代,但是当前插件还是得用github的protoc-gen-go,
如果用google.golang.org/protobuf/cmd/protoc-gen-go的话(即go install google.golang.org/protobuf/cmd/protoc-gen-go,这个同样会在$GOPATH/bin目录下生成protoc-gen-go.exe),会导致如下错误
ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master) $ protoc --go_out=plugins=grpc:. helloworld.proto --go_out: protoc-gen-go: plugins are not supported; use 'protoc --go-grpc_out=...' to generate gRPC ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master) $ protoc --go-grpc_out=. helloworld.proto 'protoc-gen-go-grpc' ????????????????????????е???? ????????????? --go-grpc_out: protoc-gen-go-grpc: Plugin failed with status code 1.
后面会有单独的protoc-gen-go-grpc来生成grpc接口,但是这块还在代码review阶段,待发布。。。
以上就是详解Windows10+golang+gRPC环境搭建的详细内容,更多请关注其它相关文章!