教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 Windows10 golang gRPC环境搭建

Windows10 golang gRPC环境搭建

发布时间:2021-12-19   编辑:jiaochengji.com
教程集为您提供Windows10 golang gRPC环境搭建等资源,欢迎您收藏本站,我们将为您提供最新的Windows10 golang gRPC环境搭建资源
<h5>1、安装protoc</h5>

下载地址: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都可以用到

<h5>2、安装gRPC</h5>

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

<pre><code>$ 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)</code></pre>

因为google.golang.org在国内很难访问,所以会下载失败。
官网也给了多个解决方案:
https://github.com/grpc/grpc-go
<span class="img-wrap"></span>
我们采用第二种方法,直接将源码clone到本地
进入到$GOPATH/src目录,执行命令:

<pre><code>git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc</code></pre>

下载速度有时快,有时慢,非常慢的时候可以取消,重新触发,多试几次偶尔会很快。
下载完成后,安装gRPC:

<pre><code>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) </code></pre>

可以发现会有很多错误,根据提示可以发现是由于缺少包的原因,这里就不一点点分析错误信息了,直接给出所需的依赖包以及下载方法(在$GOPATH/src目录下执行命令):
1)text包

<pre><code>git clone https://github.com/golang/text.git ./golang.org/x/text</code></pre>

2)net包

<pre><code>git clone https://github.com/golang/net.git ./golang.org/x/net</code></pre>

3)genproto包

<pre><code>git clone https://github.com/google/go-genproto.git ./google.golang.org/genproto</code></pre>

4)protobuf包
两个:

<pre><code>git clone https://github.com/protocolbuffers/protobuf-go.git ./google.golang.org/protobuf</code></pre> <pre><code>git clone https://github.com/golang/protobuf.git ./github.com/golang/protobuf</code></pre>

都要下,github.com/golang/protobuf的代码有的依赖google.golang.org/protobuf

以上依赖库都下载完成后后,重新安装gRPC:

<pre><code>ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src $ go install google.golang.org/grpc/ ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src $ </code></pre>

可见已经没有错误了,也没啥输出。。。

验证gRPC是否OK
启两个bash窗口,分别执行如下指令:

<pre><code>go run google.golang.org/grpc/examples/helloworld/greeter_server/main.go </code></pre> <pre><code>go run google.golang.org/grpc/examples/helloworld/greeter_client/main.go</code></pre>

<span class="img-wrap"></span>
如图可以看到服务端收到了客户端发送的消息

<h5>3、编译rpc</h5>

对于编写好的proto文件,需要经过编译才能变成.go文件,例如:
$GOPATHgoogle.golang.orggrpcexampleshelloworldhelloworld目录中有如下文件:

<pre><code>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 </code></pre>

我们先将.go文件备份一下

<pre><code>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 </code></pre>

然后执行:

<pre><code>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. </code></pre>

发现有错误,需要安装protoc-gen-go
执行如下命令安装:

<pre><code>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* </code></pre>

安装完成后,在$GOPATH/bin目录下会生成protoc-gen-go.exe文件
然后再执行编译proto文件:
会生成一个helloworld.pb.go的文件

<h5>总结</h5>

有几个库,需要了解下:

1、https://github.com/protocolbuffers/protobuf
这个是google开源的protobuf源码库,这个库里面包含了常用的各种语言实现protobuf的源码

2、https://github.com/golang/protobuf
这个库是golang的protobuf开源库,查看这个库的README.md可以发现,这个库被<code>google.golang.org/protobuf</code>替代了,点开这个链接可以发现,这个库对应的源码git仓库为:
https://github.com/protocolbuffers/protobuf-go

<h5>问题:</h5>

目前看开源社区,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),会导致如下错误

<pre><code>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. </code></pre>

后面会有单独的protoc-gen-go-grpc来生成grpc接口,但是这块还在代码review阶段,待发布。。。

到此这篇关于“Windows10 golang gRPC环境搭建”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
Windows10 golang gRPC环境搭建
Windows环境下gRPC安装
golang:REST接口
Golang微服务micro 环境搭建,纯小白..
Golang 微服务 - 01 环境和工具
golang微服务框架对比_Golang 中的微服务 - 第一部分
Golang基础第五篇——golang的gRPC
Windows10下VScode环境编译运行go语言
go 语言环境搭建
golang SDK环境搭建

[关闭]
~ ~