目錄
1、安裝protoc
2、安裝gRPC
3、編譯rpc
總結
問題:
首頁 後端開發 Golang 詳解Windows10+golang+gRPC環境搭建

詳解Windows10+golang+gRPC環境搭建

Mar 15, 2021 pm 05:08 PM
golang rpc

下面由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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1321
25
PHP教程
1269
29
C# 教程
1249
24
如何使用 Golang 安全地讀取和寫入檔案? 如何使用 Golang 安全地讀取和寫入檔案? Jun 06, 2024 pm 05:14 PM

在Go中安全地讀取和寫入檔案至關重要。指南包括:檢查檔案權限使用defer關閉檔案驗證檔案路徑使用上下文逾時遵循這些準則可確保資料的安全性和應用程式的健全性。

如何為 Golang 資料庫連線配置連線池? 如何為 Golang 資料庫連線配置連線池? Jun 06, 2024 am 11:21 AM

如何為Go資料庫連線配置連線池?使用database/sql包中的DB類型建立資料庫連線;設定MaxOpenConns以控制最大並發連線數;設定MaxIdleConns以設定最大空閒連線數;設定ConnMaxLifetime以控制連線的最大生命週期。

如何在 Golang 中將 JSON 資料保存到資料庫中? 如何在 Golang 中將 JSON 資料保存到資料庫中? Jun 06, 2024 am 11:24 AM

可以透過使用gjson函式庫或json.Unmarshal函數將JSON資料儲存到MySQL資料庫中。 gjson函式庫提供了方便的方法來解析JSON字段,而json.Unmarshal函數需要一個目標類型指標來解組JSON資料。這兩種方法都需要準備SQL語句和執行插入操作來將資料持久化到資料庫中。

Golang框架與Go框架:內部架構與外部特性對比 Golang框架與Go框架:內部架構與外部特性對比 Jun 06, 2024 pm 12:37 PM

GoLang框架與Go框架的差異體現在內部架構與外部特性。 GoLang框架基於Go標準函式庫,擴充其功能,而Go框架由獨立函式庫組成,以實現特定目的。 GoLang框架更靈活,Go框架更容易上手。 GoLang框架在效能上稍有優勢,Go框架的可擴充性更高。案例:gin-gonic(Go框架)用於建立RESTAPI,而Echo(GoLang框架)用於建立Web應用程式。

從前端轉型後端開發,學習Java還是Golang更有前景? 從前端轉型後端開發,學習Java還是Golang更有前景? Apr 02, 2025 am 09:12 AM

後端學習路徑:從前端轉型到後端的探索之旅作為一名從前端開發轉型的後端初學者,你已經有了nodejs的基礎,...

golang框架開發實戰教學:常見疑問解答 golang框架開發實戰教學:常見疑問解答 Jun 06, 2024 am 11:02 AM

Go框架開發常見問題:框架選擇:取決於應用需求和開發者偏好,如Gin(API)、Echo(可擴展)、Beego(ORM)、Iris(效能)。安裝和使用:使用gomod指令安裝,導入框架並使用。資料庫互動:使用ORM庫,如gorm,建立資料庫連線和操作。身份驗證和授權:使用會話管理和身份驗證中間件,如gin-contrib/sessions。實戰案例:使用Gin框架建立一個簡單的部落格API,提供POST、GET等功能。

Go語言中哪些庫是由大公司開發或知名的開源項目提供的? Go語言中哪些庫是由大公司開發或知名的開源項目提供的? Apr 02, 2025 pm 04:12 PM

Go語言中哪些庫是大公司開發或知名開源項目?在使用Go語言進行編程時,開發者常常會遇到一些常見的需求,�...

Golang的目的:建立高效且可擴展的系統 Golang的目的:建立高效且可擴展的系統 Apr 09, 2025 pm 05:17 PM

Go語言在構建高效且可擴展的系統中表現出色,其優勢包括:1.高性能:編譯成機器碼,運行速度快;2.並發編程:通過goroutines和channels簡化多任務處理;3.簡潔性:語法簡潔,降低學習和維護成本;4.跨平台:支持跨平台編譯,方便部署。

See all articles