etcd、grpc、protobuf兼容性问题
文章目录
近来在学习 etcd, 拿到一份代码的 demo. 想跑起来看看效果。没想过这竟然是个艰难的过程,记录下来,以便大家遇到同样的问题时,可以快速解决。
protoc –-go_out=plugins=grpc:. *.proto
1. 错误1
报错如下:
2020/04/26 16:33:39 WARNING: Missing 'go_package' option in "hello.proto", please specify:
option go_package = ".;protoes";
A future release of protoc-gen-go will require this be specified.
See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information.
这个比较好解决,按照提示把第 3 行改为第 4 行(注意:Windows下可能需要将.改为./),如下图
![在这里插入图片描述 etcd、grpc、protobuf兼容性问题_github](https://s2.51cto.com/images/blog/202207/12145653_62cd1b35705b110160.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
2. 错误2
go.mod 目录下执行 go mod tidy
报错如下:
go: finding github.com/golang/groupcache latest
go: etcdDemo/etcdservice imports
go.etcd.io/etcd/clientv3 tested by
go.etcd.io/etcd/clientv3.test imports
github.com/coreos/etcd/auth imports
github.com/coreos/etcd/mvcc/backend imports
github.com/coreos/bbolt: github.com/coreos/bbolt@v1.3.4: parsing go.mod:
module declares its path as: go.etcd.io/bbolt
but was required as: github.com/coreos/bbolt
在 go.mod 中添加两行
replace github.com/coreos/bbolt v1.3.4 => go.etcd.io/bbolt v1.3.4
replace go.etcd.io/bbolt v1.3.4 =>
再次执行 go mod tidy, 再次报错如下:
go: finding golang.org/x/net latest
go: finding github.com/coreos/go-systemd latest
go: finding github.com/coreos/pkg latest
go: finding golang.org/x/time latest
go: finding github.com/xiang90/probing latest
go: finding github.com/tmc/grpc-websocket-proxy latest
go: finding github.com/golang/groupcache latest
etcdDemo/etcdservice imports
go.etcd.io/etcd/clientv3 imports
github.com/coreos/etcd/pkg/logutil imports
github.com/coreos/go-systemd/journal: no matching versions for query "latest"
go 1.13 的版本导致的 (go 1.14 应该没有)
解决方法:在 go.mod 中添加
replace github.com/coreos/go-systemd =>
再次 go mod tidy , 这一项暂时通过了。
3. 错误3
执行 go build
报错如下:
D:\code\etcdDemo\server>go build
#github.com/coreos/etcd/clientv3/balancer/resolver/endpoint
E:\gocode\pkg\mod\github.com\coreos\etcd@v3.3.20+incompatible\clientv3\balancer\resolver\endpoint\endpoint.go:114:78: undefined: resolver.BuildOption
E:\gocode\pkg\mod\github.com\coreos\etcd@v3.3.20+incompatible\clientv3\balancer\resolver\endpoint\endpoint.go:182:31: undefined: resolver.ResolveNowOption
#github.com/coreos/etcd/clientv3/balancer/picker
原因 etcd3.3.20 的 release 版本要求 grpc 的版本是 v1.26.0 之前的。而此时 go.mod 里面的 google.golang.org/grpc 是 v1.29.1
解决方法:在 go.mod 里添加一句
replace google.golang.org/grpc =>
4. 错误4
go build 接着报错,如下:
D:\code\etcdDemo\server>go build
# etcdDemo/protoes
..\protoes\hello.pb.go:292:11: undefined: grpc.SupportPackageIsVersion6
..\protoes\hello.pb.go:303:5: undefined: grpc.ClientConnInterface
# etcdDemo/etcdservice
..\etcdservice\resolver.go:82:4: invalid case "go.etcd.io/etcd/mvcc/mvccpb".PUT in switch on ev.Type (mismatched types "go.etcd.io/etcd/mvcc/mvccpb".Event_EventType and "github.com/coreos/etcd/mvcc/mvccpb".
Event_EventType)
..\etcdservice\resolver.go:87:4: invalid case "go.etcd.io/etcd/mvcc/mvccpb".DELETE in switch on ev.Type (mismatched types "go.etcd.io/etcd/mvcc/mvccpb".Event_EventType and "github.com/coreos/etcd/mvcc/mvccp
b".Event_EventType)
原因:此时设备上的 proto-gen-go 是 v1.4.0 版本,它要求 grpc 的版本是 v1.27.0 往后的,而刚才 etcd 要求 grpc 是 v.1.26.0 ! 无奈了,柿子挑软的捏吧,把 proto-gen-go 降级到能够匹配 grpc v.1.26.0 的版本。执行下面这条命令
go get -u -v github.com/golang/protobuf/protoc-gen-go@v1.2.0
之后千万别忘了,重新生成 proto
5. 错误5
继续 go build, 继续报错!!! (心态有点儿崩了。。。)
D:\code\etcdDemo\server>go build
go: finding google.golang.org/genproto latest
go: finding github.com/golang/protobuf v1.3.3
# etcdDemo/etcdservice
..\etcdservice\resolver.go:82:4: invalid case "go.etcd.io/etcd/mvcc/mvccpb".PUT in switch on ev.Type (mismatched types "go.etcd.io/etcd/mvcc/mvccpb".Event_EventType and "github.com/coreos/etcd/mvcc/mvccpb".
Event_EventType)
..\etcdservice\resolver.go:87:4: invalid case "go.etcd.io/etcd/mvcc/mvccpb".DELETE in switch on ev.Type (mismatched types "go.etcd.io/etcd/mvcc/mvccpb".Event_EventType and "github.com/coreos/etcd/mvcc/mvccp
b".Event_EventType)
..\etcdservice\resolver.go:82:4: invalid case "go.etcd.io/etcd/mvcc/mvccpb".PUT in switch on ev.Type (mismatched types "go.etcd.io/etcd/mvcc/mvccpb".Event_EventTyp
Event_EventType)
..\etcdservice\resolver.go:87:4: invalid case "go.etcd.io/etcd/mvcc/mvccpb".DELETE in switch on ev.Type (mismatched types "go.etcd.io/etcd/mvcc/mvccpb".Event_Event
b".Event_EventType)
缓缓。。。
仔细看了一下报错的内容,编译器要求 mvccpb 使用 github.com/coreos/etcd/mvcc/mvccpb 这个包,而代码中使用的是
go.etcd.io/etcd/mvcc/mvccpb 。去对应的代码文件看了一下,果然是这样。按照要求,把 github.com/coreos/etcd/mvcc/mvccpb import 进来。
继续 go build, 终于
![在这里插入图片描述 etcd、grpc、protobuf兼容性问题_go_02](https://s2.51cto.com/images/blog/202207/12145653_62cd1b35a49ba65826.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
那一刻,世界仿佛清静了许多。
对于程序员来说,最美好的事,莫过于 no warning, no error
————————————————
原文作者:xiaoma_nmg