首頁  >  文章  >  後端開發  >  詳解Windows10+golang+gRPC環境搭建

詳解Windows10+golang+gRPC環境搭建

藏色散人
藏色散人轉載
2021-03-15 17:08:173814瀏覽

下面由golang教學專欄跟大家介紹Windows10 golang gRPC環境搭建,希望對需要的朋友有幫助!

詳解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

詳解Windows10+golang+gRPC環境搭建

如圖可以看到服務端收到了客戶端發送的訊息

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階段,待發布。 。 。

以上是詳解Windows10+golang+gRPC環境搭建的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:segmentfault.com。如有侵權,請聯絡admin@php.cn刪除