PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

详解Windows10+golang+gRPC环境搭建

藏色散人
藏色散人 转载
2021-03-15 17:08:17 3612浏览

下面由golang教程栏目给大家介绍windows10+golang+grpc环境搭建,希望对需要的朋友有所帮助!

1、安装protoc

下载地址:https://github.com/protocolbuffers/protobuf/release
(注:https://github.com/protocolbuffers/protobuf 是其源码库,可以学习,如果源码库下载过慢,可以到码云上搜,很多同步的库,是国内的源,下载速度比较快,当然也可以自己在码云上创建个同步的库)

当前最新版本3.12.2
我的是windows10 64位操作系统,所以选择版本:protoc-3.12.2-win64.zip
直接用浏览器即可下载
如果网速不行,还可以用迅雷下载:https://github.com/protocolbuffers/protobuf/releases/download/v3.12.2/protoc-3.12.2-win64.zip
解压之后,将protoc.exe拷贝到$GOPATH/bin目录下
如果有多个GOPATH,放置到放公共第三方库的那个GOPATH中,这样多个project都可以用到

2、安装gRPC

gRPC源码:https://github.com/grpc/grpc-go.git
官网给的安装方法为:go get -u google.golang.org/grpc
但是国内经常会出现如下错误:

$ 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

db85cee7a7d667411407846f1174860.png

如图可以看到服务端收到了客户端发送的消息

3、编译rpc

对于编写好的proto文件,需要经过编译才能变成.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阶段,待发布。。。

声明:本文转载于:segmentfault,如有侵犯,请联系admin@php.cn删除