linux 使用 go 连接 etcd 集群时报错

时间:2024-03-25 12:41:11

问题分析

在 ubuntu 中使用 github.com/coreos/etcd/clientv3 第三方库报错,报错信息如下:

# github.com/coreos/etcd/clientv3/balancer/resolver/endpoint
/root/go/pkg/mod/github.com/coreos/etcd@v3.3.27+incompatible/clientv3/balancer/resolver/endpoint/endpoint.go:114:87: undefined: resolver.BuildOption
/root/go/pkg/mod/github.com/coreos/etcd@v3.3.27+incompatible/clientv3/balancer/resolver/endpoint/endpoint.go:115:16: target.Authority undefined (type resolver.Target has no field or method Authority)  
/root/go/pkg/mod/github.com/coreos/etcd@v3.3.27+incompatible/clientv3/balancer/resolver/endpoint/endpoint.go:118:15: target.Authority undefined (type resolver.Target has no field or method Authority)  
/root/go/pkg/mod/github.com/coreos/etcd@v3.3.27+incompatible/clientv3/balancer/resolver/endpoint/endpoint.go:182:40: undefined: resolver.ResolveNowOption
# github.com/coreos/etcd/clientv3/balancer/picker
/root/go/pkg/mod/github.com/coreos/etcd@v3.3.27+incompatible/clientv3/balancer/picker/err.go:37:53: undefined: balancer.PickOptions
/root/go/pkg/mod/github.com/coreos/etcd@v3.3.27+incompatible/clientv3/balancer/picker/roundrobin_balanced.go:55:63: undefined: balancer.PickOptions

解决办法

使用如下方式引用 etcd v3:

import (
    "go.etcd.io/etcd/client/v3"
)

注:使用 "github.com/coreos/etcd/clientv3" 方式引用时会报如上错误!