golang安裝使用grpc


1. 安裝grpc

官網的安裝命令:

go get -u google.golang.org/grpc

貌似用不了,連不上服務器,即便我掛上vpn也沒有用,沒辦法只有迂回安裝了, 反正代碼在github上都有,就從github上clone下來, 需要的庫包括grpc-go, golang/net, golang/text, protobuf/proto, protobuf/protoc-gen-go, google/go-genproto

# 如果已經安裝了proto和protoc-gen-go的話就不用安裝了
go get -u github.com/golang/protobuf/{proto,protoc-gen-go}

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

# 下載golang/net
git clone https://github.com/golang/net.git $GOPATH/src/golang.org/x/net

# 下載golang/text
git clone https://github.com/golang/text.git $GOPATH/src/golang.org/x/text

# 下載go-genproto
git clone https://github.com/google/go-genproto.git $GOPATH/src/google.golang.org/genproto

# 安裝
cd $GOPATH/src/
go install google.golang.org/grpc

2. 生成grpc.pb

我們就用官網提供的helloworld example試下

protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld

plugins=grpc:helloworld, grpc:后面跟的helloworld是要生成的pb文件的文件路徑

3. 運行 helloworld example

  • 運行server
    $ go run greeter_server/main.go
    
  • 運行client
    $ go run greeter_client/main.go
    2018/09/19 11:23:31 Greeting: Hello world
    
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 介紹 grpc 是 Google 在 2015 年 2 月底時發布的一款開源 RPC 框架,其源碼是由 C 語言編...
    juniway閱讀 25,354評論 2 3
  • 1,關于grpc-go golang 可以可以做grpc的服務端和客戶端。 官網的文檔: http://www.g...
    ebayboy閱讀 1,113評論 0 1
  • 1、安裝Protobuf 在https://github.com/google/protobuf/releases...
    alvin2018閱讀 7,738評論 0 4
  • gRPC是由Google主導開發的RPC框架,使用HTTP/2協議并用ProtoBuf作為序列化工具。其客戶端提供...
    CZ_Golang閱讀 82,223評論 9 71
  • 1)簡介 gRPC負載平衡的主要實現機制是外部負載平衡,即通過外部負載平衡器來向客戶端提供更新后的服務器列表。 g...
    Jay_Guo閱讀 13,380評論 6 22