近来在学习etcd,拿到一份代码的demo.想跑起来看看效果。没想过这竟然是个艰难的过程,记录下来,以便大家遇到同样的问题时,可以快速解决。

protoc –go_out=plugins=grpc:. *.proto

报错如下:

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行,如下图

使用etcd和grpc遇到的版本冲突的那些事儿

go.mod目录下执行 go mod tidy

报错如下:

go: finding golang.org/x/time latest
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 => github.com/coreos/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 => github.com/coreos/go-systemd/v22 v22.0.0

再次go mod tidy , 这一项暂时通过了。

执行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
E:\gocode\pkg\mod\github.com\coreos\etcd@v3.3.20+incompatible\clientv3\balancer\picker\err.go:37:44: undefined: balancer.PickOptions
E:\gocode\pkg\mod\github.com\coreos\etcd@v3.3.20+incompatible\clientv3\balancer\picker\roundrobin_balanced.go:55:54: undefined: balancer.PickOptions

原因etcd3.3.20的release版本要求grpc的版本是 v1.26.0 之前的。而此时go.mod里面的 google.golang.org/grpc 是 v1.29.1
解决方法:在go.mod 里添加一句

replace google.golang.org/grpc => google.golang.org/grpc v1.26.0

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
继续 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遇到的版本冲突的那些事儿
那一刻,世界仿佛清静了许多。
对于程序员来说,最美好的事,莫过于no warning, no error

本作品采用《CC 协议》,转载必须注明作者和本文链接