go grpc example
- grpc client load balance example
$ brew install protobuf
# 检查是否安装成功
$ protoc --version
go install 需要 go1.16 版本
# 官方工具
$ go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
$ go install -v google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
# 使用 gogo/protobuf 优化代码生成
$ go install github.com/gogo/protobuf/protoc-gen-gogofast@latest
介绍官方生成器和 gogo/protobuf 两种生成器, 新手可以忽略掉 gogo/protobuf
#!/bin/bash -x
set -e
OUT_DIR=./pb
rm -rf $OUT_DIR/*.{go,json}
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
pb/origin-hello.proto
./scripts/origin-gen.sh 对应 proto 文件 ./pb/origin-hello.proto
#!/bin/bash -x
set -e
OUT_DIR=./pb
GOGOPROTO_ROOT="$(GO111MODULE=on go list -m -f '{{ .Dir }}' -m github.com/gogo/protobuf)"
GOGOPROTO_PATH="${GOGOPROTO_ROOT}:${GOGOPROTO_ROOT}/protobuf"
rm -rf $OUT_DIR/*.{go,json}
protoc --gogofast_out=plugins=grpc:. \
-I=. \
-I="${GOGOPROTO_PATH}" \
pb/hello.proto
./scripts/gen.sh 对应 proto 文件 ./pb/hello.proto
服务端实现生成接口 pb.HelloServer
接口, 客户端直接使用 pb.NewHelloClient
初始化 client 调用接口.
MIT © zcong1993