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