go-grpc環境搭建
前言:
隨著微服務的興起,現在 越來越多的 rpc 框架 開始出現,前段時間自己學習搭建了一下 grpc ,在這裡記錄一下,方便以後查閱和與大家一起分享一下。
第一步安裝protobuf:
protobuf是Google推出的 一種高效能的 資料結構。和 json 和xml 一樣 用於傳輸 資料。它的效能巨好。。。
- 先下載這個檔案ofollow,noindex">https://github.com/protocolbuffers/protobuf/releases/download/v3.6.0/protoc-3.6.0-win32.zip
- 把這麼檔案裡面的bin裡面的protoc.exe 複製到GOPATH/bin下,GOPATH/bin加入環境變數。也可以放到別的目錄,把那個目錄 配置到環境變數。反正最後的效果就是 在cmd 輸入 protoc 不會報錯。
-
在cmd 中執行
go get -u github.com/golang/protobuf/protoc-gen-go
獲取protobuf的編譯器外掛 protoc-gen-go。 -
到這裡 protobuf 就配的差不多了,
protoc --go_out=plugins=grpc:. xxxx.proto
這條命令就是 編譯 協議的,具體怎麼使用我們以後說。
第二步安裝grpc:
go get -u google.golang.org/grpc
git clone https://github.com/grpc/grpc-go.git$GOPATH/src/google.golang.org/grpc git clone https://github.com/golang/net.git$GOPATH/src/golang.org/x/net git clone https://github.com/golang/text.git$GOPATH/src/golang.org/x/text git clone https://github.com/google/go-genproto.git$GOPATH/src/google.golang.org/genproto cd $GOPATH/src/ go install google.golang.org/grpc
- 注意 $GOPATH 每個人不同的。
總結:
到這裡我們grpc 環境就搭建好了,小夥伴們可以開始愉快的grpc了。