美文网首页
go-grpc-rest环境搭建

go-grpc-rest环境搭建

作者: 终南山下007 | 来源:发表于2021-03-16 15:37 被阅读0次
    1. 安装protoc

    下载:https://github.com/protocolbuffers/protobuf

    设置环境变量

    1. 安装gogoproto

    go get -u github.com/gogo/protobuf/..

    1. 安装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

    1. 下载googleapi

    https://github.com/googleapis/googleapis

    1. 脚本文件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
    

    相关文章

      网友评论

          本文标题:go-grpc-rest环境搭建

          本文链接:https://www.haomeiwen.com/subject/rysdcltx.html