解决 etcd 与 grpc 不兼容问题

今天遇到了一个 Golang 的依赖问题,在一个使用了 Protobuf 的项目,引入了 Prometheus 的 Package 之后编译时发现 etcd 报错:

# github.com/coreos/etcd/clientv3/balancer/resolver/endpoint
../../go/pkg/mod/github.com/coreos/etcd@v3.3.18+incompatible/clientv3/balancer/resolver/endpoint/endpoint.go:114:78: undefined: resolver.BuildOption
../../go/pkg/mod/github.com/coreos/etcd@v3.3.18+incompatible/clientv3/balancer/resolver/endpoint/endpoint.go:182:31: undefined: resolver.ResolveNowOption
# github.com/coreos/etcd/clientv3/balancer/picker
../../go/pkg/mod/github.com/coreos/etcd@v3.3.18+incompatible/clientv3/balancer/picker/err.go:37:44: undefined: balancer.PickOptions
../../go/pkg/mod/github.com/coreos/etcd@v3.3.18+incompatible/clientv3/balancer/picker/roundrobin_balanced.go:55:54: undefined: balancer.PickOptions

根据问题搜索到 issue:clientv3: grpc-go (v1.27.0) made API changes to balancer / resolver. #11563,依据里面的方法将 grpc 降级到 v1.2.6.0

require (
	...
	google.golang.org/grpc v1.26.0
)

但又出现了另外一个问题 https://github.com/grpc/grpc-go/issues/3347

>  project [master] ⚡ make build
# google.golang.org/api/internal
../../go/pkg/mod/google.golang.org/api@v0.20.0/internal/conn_pool.go:29:2: undefined: grpc.ClientConnInterface

降级protoc-gen-go并不能解决问题,查看 go.sum 之后, 发现 v0.14.0 同样可以满足依赖关系

google.golang.org/api v0.14.0 h1:uMf5uLi4eQMRrMKhCplNik4U4H8Z6C1br3zOtAa/aDE=
google.golang.org/api v0.14.0/go.mod h1:iLdEw5Ide6rF15KTC1Kkl0iskquN2gFfn9o9XIsbkAI=
google.golang.org/api v0.20.0 h1:jz2KixHX7EcCPiQrySzPdnYT7DbINAypCqKZ1Z7GM40=
google.golang.org/api v0.20.0/go.mod h1:BwFmGc8tA3vsd7r/7kR8DY7iEEGSU04BFxCo5jP/sfE=

将这两个 package 锁定使用版本后解决。

replace (
	google.golang.org/api => google.golang.org/api v0.14.0
	google.golang.org/grpc => google.golang.org/grpc v1.26.0
)

Note: etcd 的问题已经看到有 PR(https://github.com/etcd-io/etcd/pull/11564) 合并到了 master,但目前还未 release。