[Protobuf][golang] protoc proto-gen-grpc protobuf 版本兼容问题

商飞龙
2023-12-01

编译命令

  • 参考文档
    • https://studygolang.com/articles/28132?fr=sidebar
protoc  --go_out=plugins=grpc:./ *.proto

issue1

172-1-1-156:proto $ protoc  --go_out=plugins=grpc:./ *.proto
--go_out: protoc-gen-go: plugins are not supported; use 'protoc --go-grpc_out=...' to generate gRPC

protoc --go_out=. --go_opt=paths=source_relative \
    --go-grpc_out=. --go-grpc_opt=paths=source_relative \
    *.proto
  • protoc-gen-go版本问题
  • 解决方法
See https://grpc.io/docs/languages/go/quickstart/#regenerate-grpc-code for more information.
172-1-1-156:proto $ GO111MODULE=on go get -u github.com/golang/protobuf/protoc-gen-go@v1.3.5
go get: downgraded github.com/golang/protobuf v1.5.0 => v1.3.5
go get: downgraded google.golang.org/protobuf v1.26.0 => v1.26.0-rc.1

issue2

# trading-system/grpc/etc/proto
grpc/etc/proto/etc.pb.go:30:11: undefined: "github.com/golang/protobuf/proto".ProtoPackageIsVersion4

  • 问题分析
    • 不是最新版本
  • 解决方法
//我只用了如下方法
go get -u google.golang.org/grpc
go install github.com/golang/protobuf/protoc-gen-go

// 以下方法来自http://www.manongjc.com/detail/11-wgczdfriohrkgjm.html
GIT_TAG="v1.2.0" # change as needed
go get -d -u github.com/golang/protobuf/protoc-gen-go
git -C "$(go env GOPATH)"/src/github.com/golang/protobuf checkout $GIT_TAG
go install github.com/golang/protobuf/protoc-gen-go

issue 3

*ApiGRPCServer does not implement "trading-system/grpc/api/proto".ApiServiceServer (missing "trading-system/grpc/api/proto".mustEmbedUnimplementedApiServiceServer method)
                have mustEmbedUnimplementedApiServiceServer()
                want "trading-system/grpc/api/proto".mustEmbedUnimplementedApiServiceServer()
  • 解决方法 同issue2
  • 用如下命令

protoc --go_out=plugins=grpc:./ *.proto

问题总结

  • google.golang.org/protobuf/cmd/protoc-gen-go
    • 插件错误,目前google自己包不支持grpc插件功能

    • 参考issue:https://github.com/golang/protobuf/issues/1070

    • 错误提示

plugins are not supported; use 'protoc --go-grpc_out=...' 
  • github.com/golang/protobuf/protoc-gen-go

    • github提供的插件是支持的
  • 版本兼容支持,目前以下版本是兼容的

    • github.com/golang/protobuf v1.5.2
    • google.golang.org/grpc v1.39.0
    • google.golang.org/protobuf v1.27.1
  • 关于go.mod文件,自动升级的问题

In Go 1.15 and lower, the -mod=mod flag was enabled by default, so updates were performed automatically. Since Go 1.16, the go command acts as if -mod=readonly were set instead: if any changes to go.mod are needed, the go command reports an error and suggests a fix
  • go 1.16之后都是默认readonly
  • go 1.16之前的用 (go build, go get, go install, go list, go test, go mod tidy)-mod=readonly

package 问题

package proto.api;
option go_package = "./;proto";
  • package proto.api; 用于包装pb文件的方法,供client端调用,client端也需要有相同的命名。

    • 例如
          rpc CreateAccount (AccountInfoRequest) returns (CommonResponse) {
      

    }
    ```

    • 对应:

      func (c *apiServiceClient) CreateAccount(ctx context.Context, in *AccountInfoRequest, opts ...grpc.CallOption) (*CommonResponse, error) {
              out := new(CommonResponse)
              err := c.cc.Invoke(ctx, "/proto.api.ApiService/CreateAccount", in, out, opts...)//对应这里
              if err != nil {
              	return nil, err
              }
          return out, nil
      }
      
  • option go_package = “./;proto”;这个是为了本包的文件引用用的

 类似资料: