正常来说,clietntv3的初始化代码如下:
config := clientv3.Config{ Endpoints: []string{"localhost:2379"}, DialTimeout: 2*time.Second, } client, err := clientv3.New(config) if err != nil{ panic(fmt.Errorf("ectd client init failed! err: %s ", err.Error())) }
但是,如果使用新版本的客户端( v3.3.x) 。当我们指定到了一个无效的etcd服务端地址时, 即使设置了DialTimeout选项,clientv3.New()函数依然不会抛出任何错误。直到执行后续的etcd命令时候,才会抛出错误。
貌似是新版本使用的新的均衡器带来的问题。
目前正确的使用方法是在New()函数后使用Status函数来检测当前的连接状态, 代码如下:
//初始化 func (e *EtcdRegistry) Init(ctx context.Context, opts ...registry.Option) (err error) { e.options = ®istry.Options{} for _, opt := range opts { opt(e.options) } clientConfig := clientv3.Config{ Endpoints: e.options.Addrs, DialTimeout: e.options.Timeout, } e.client, err = clientv3.New(clientConfig) if err != nil { err = fmt.Errorf("init etcd failed, err:%v", err) return } //下面代码 是因为当etcd ip链接不同 不报错导致超时 阻塞的办法 timeoutCtx, cancel := context.WithTimeout(context.Background(), 2 * time.Second) defer cancel() _, err =e.client.Status(timeoutCtx, clientConfig.Endpoints[0]) fmt.Println(err) if err != nil { panic("xxxxxxxxxxxxxx") err = fmt.Errorf("error checking etcd status: %v", err) return } return }
本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/p/13903521.html
分类:
golang
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能