- 安装protoc
设置环境变量
- 安装gogoproto
go get -u github.com/gogo/protobuf/..
- 安装grpc-gateway
go get -u github.com/grpc-ecosystem/grpc-gateway
go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway
go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2
go install google.golang.org/protobuf/cmd/protoc-gen-go
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc
- 脚本文件genproto.sh
#!/usr/bin/env bash
#
# Generate all etcd protobuf bindings.
# Run from repository root.
#
set -e
# directories containing protos to be built
DIRS="./pb"
# disable go mod
export GO111MODULE=off
# set up self-contained GOPATH for building
GOPATH="/usr/local/go"
GOGOPROTO_ROOT="$GOPATH/src/github.com/gogo/protobuf"
GOGOPROTO_PATH="$GOPATH/src/github.com/gogo/protobuf/protobuf"
GRPC_GATEWAY_ROOT="$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway"
GATEWAY_FILE_PATH="gw"
for dir in ${DIRS}; do
pushd "${dir}"
mkdir -p ${GATEWAY_FILE_PATH}
protoc --gogofast_out=plugins=grpc:. -I . -I ${GOPATH}/src -I ${GOGOPROTO_PATH} -I ${GOGOPROTO_ROOT} --grpc-gateway_out ${GATEWAY_FILE_PATH} ./*.proto
sed -i.bak -E 's/_ \"google\/api\"/_ \"google\.golang\.org\/genproto\/googleapis\/api\/annotations\"/g' ./*.pb.go
rm -f ./*.bak
goimports -w ./*.pb.go
popd
done
有疑问加站长微信联系(非本文作者)