下面由golang教程栏目给大家介绍Windows10+golang+gRPC环境搭建,希望对需要的朋友有所帮助!
下面由golang教程栏目给大家介绍Windows10+golang+gRPC环境搭建,希望对需要的朋友有所帮助!1、安装protoc下载地址:https://github.com/protocolbuffers/protobuf/release 当前最新版本3.12.2 2、安装gRPCgRPC源码:https://github.com/grpc/grpc-go.git $ 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在国内很难访问,所以会下载失败。 git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/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目录下执行命令): 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 go run google.golang.org/grpc/examples/helloworld/greeter_server/main.go go run google.golang.org/grpc/examples/helloworld/greeter_client/main.go 如图可以看到服务端收到了客户端发送的消息 3、编译rpc对于编写好的proto文件,需要经过编译才能变成.go文件,例如: 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文件 总结有几个库,需要了解下: 1、https://github.com/protocolbuffers/protobuf 2、https://github.com/golang/protobuf 问题:目前看开源社区,github的protobuf会逐渐被google.golang.org的库替代,但是当前插件还是得用github的protoc-gen-go, 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环境搭建的详细内容,更多请关注模板之家(www.mb5.com.cn)其它相关文章! |