溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何搭建Windows10+golang+gRPC環(huán)境

發(fā)布時間:2021-03-16 09:40:07 來源:億速云 閱讀:316 作者:小新 欄目:編程語言

這篇文章主要介紹了如何搭建Windows10+golang+gRPC環(huán)境,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

1、安裝protoc

下載地址:https://github.com/protocolbuffers/protobuf/release
(注:https://github.com/protocolbuffers/protobuf 是其源碼庫,可以學習,如果源碼庫下載過慢,可以到碼云上搜,很多同步的庫,是國內(nèi)的源,下載速度比較快,當然也可以自己在碼云上創(chuàng)建個同步的庫)

當前最新版本3.12.2
我的是windows10 64位操作系統(tǒng),所以選擇版本:protoc-3.12.2-win64.zip
直接用瀏覽器即可下載
如果網(wǎng)速不行,還可以用迅雷下載: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
官網(wǎng)給的安裝方法為:go get -u google.golang.org/grpc
但是國內(nèi)經(jīng)常會出現(xiàn)如下錯誤:

$ 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在國內(nèi)很難訪問,所以會下載失敗。
官網(wǎng)也給了多個解決方案:
https://github.com/grpc/grpc-go
我們采用第二種方法,直接將源碼clone到本地
進入到$GOPATH/src目錄,執(zhí)行命令:

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

下載速度有時快,有時慢,非常慢的時候可以取消,重新觸發(fā),多試幾次偶爾會很快。
下載完成后,安裝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)

可以發(fā)現(xiàn)會有很多錯誤,根據(jù)提示可以發(fā)現(xiàn)是由于缺少包的原因,這里就不一點點分析錯誤信息了,直接給出所需的依賴包以及下載方法(在$GOPATH/src目錄下執(zhí)行命令):
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
$

可見已經(jīng)沒有錯誤了,也沒啥輸出。。。

驗證gRPC是否OK
啟兩個bash窗口,分別執(zhí)行如下指令:

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環(huán)境

如圖可以看到服務(wù)端收到了客戶端發(fā)送的消息

3、編譯rpc

對于編寫好的proto文件,需要經(jīng)過編譯才能變成.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

然后執(zhí)行:

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.

發(fā)現(xiàn)有錯誤,需要安裝protoc-gen-go
執(zhí)行如下命令安裝:

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文件
然后再執(zhí)行編譯proto文件:
會生成一個helloworld.pb.go的文件

總結(jié)

有幾個庫,需要了解下:

1、https://github.com/protocolbuffers/protobuf
這個是google開源的protobuf源碼庫,這個庫里面包含了常用的各種語言實現(xiàn)protobuf的源碼

2、https://github.com/golang/protobuf
這個庫是golang的protobuf開源庫,查看這個庫的README.md可以發(fā)現(xiàn),這個庫被google.golang.org/protobuf替代了,點開這個鏈接可以發(fā)現(xiàn),這個庫對應的源碼git倉庫為:
https://github.com/protocolbuffers/protobuf-go

問題:

目前看開源社區(qū),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階段,待發(fā)布。。。

感謝你能夠認真閱讀完這篇文章,希望小編分享的“如何搭建Windows10+golang+gRPC環(huán)境”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI