使用 go-micro用etcd接入微服务,版本冲突解决

Programming4年前 (2020)更新 bruce
900 0
内容纲要

报错提示

使用go-micro分布式系统开发框架,通过etcd管理服务,基于gRPC的微服务,编译报错如下,

../lib/pb/publisher.basic.pb.go:225:7: undefined: grpc.ClientConnInterface
../lib/pb/publisher.basic.pb.go:229:11: undefined: grpc.SupportPackageIsVersion6
../lib/pb/publisher.basic.pb.go:240:5: undefined: grpc.ClientConnInterface
../lib/pb/publisher.basic.pb.go:243:22: undefined: grpc.ClientConnInterface

环境
github.com/golang/protobuf v1.4.2
github.com/micro/go-micro/v2 v2.9.0
google.golang.org/grpc v1.26.0
google.golang.org/grpc v1.27.0

Makefile

build:
    protoc --proto_path=./proto/ --micro_out=./pb/ --go_out=./pb/ --go_opt=plugins=grpc,paths=source_relative  proto/publisher.basic.proto
    protoc --proto_path=./proto/ --micro_out=./pb/ --go_out=./pb/ --go_opt=plugins=grpc,paths=source_relative  proto/publisher.rpc.proto

原因分析

protoc-gen-go 是 https://github.com/golang/protobuf 项目下的一个golang工具,请见https://github.com/golang/protobuf/tree/master/protoc-gen-go

go get 安装的 protoc-gen-go 是最新版本,我们通过MakeFile文件,make build生成 pd.go文件,grpc下的一些方法是在grpc类库找不到的。

通过查看go.mod,google.golang.org/grpc v1.26.0
升级到 1.27.0,解决了protoc编译出来pb.go文件与 google.golang.org/grpc 不兼容问题。

产生了新的问题,etcd 和 grpc v1.27.0 产生冲突,表明不支持grpc这么高版本。

github.com/coreos/etcd@v3.3.18+incompatible... undefined: resolver.BuildOption
github.com/coreos/etcd@v3.3.18+incompatible... undefined: resolver.ResolveNowOption

protoc –grpc-gateway_out= 生成出来的 pb.go 代码需要 grpc 1.27.0,而 etcd 需要 grpc 1.26.0

解决方案

  1. 那么在go.mod内添加一个模块的两个版本,
    google.golang.org/grpc v1.26.0
    google.golang.org/grpc v1.27.0

  2. 还原 grpc 到 1.26.0,protoc-gen-go 降级
    github.com/golang/protobuf v1.4.2 降级到 1.3.2

    go get -u -v github.com/golang/protobuf/protoc-gen-go@v1.3.2

技术总结

  • 理解这几个类库设计目的和定位
  • 梳理清楚go-micro\etcd框架、编译Protobuf文件、gRPC相互依赖的版本
  • 尽量控制在最新稳定版本,版本不要最新,也不要太旧

Protobuf 类库和版本规范

  • The github.com/golang/protobuf module is the original Go protocol buffer API.
  • The google.golang.org/protobuf module is an updated version of this API designed for simplicity, ease of use, and safety. The flagship features of the updated API are support for reflection and a separation of the user-facing API from the underlying implementation.
  • gogoprotobuf is a fork of golang/protobuf with extra code generation features.

当前版本优先使用
proto3
google.golang.org/protobuf/proto

参考资料

Google Protocol buffers FAQ

© 版权声明

相关文章

暂无评论

暂无评论...