正常来说,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命令时候,才会抛出错误。
貌似是新版本使用的新的均衡器带来的问题。

golang etcd clientv3.New()不报超时错误的问题解决

 

 

目前正确的使用方法是在New()函数后使用Status函数来检测当前的连接状态, 代码如下:

golang etcd clientv3.New()不报超时错误的问题解决

 

 

//初始化
func (e *EtcdRegistry) Init(ctx context.Context, opts ...registry.Option) (err error) {

    e.options = &registry.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
}

 

相关文章:

  • 2021-05-27
  • 2021-07-19
  • 2022-12-23
  • 2021-06-22
  • 2021-09-04
  • 2021-04-07
  • 2022-12-23
  • 2021-12-11
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-04
  • 2022-12-23
  • 2021-07-24
  • 2023-03-05
  • 2021-06-25
相关资源
相似解决方案