drddx3115 2017-01-07 20:03
浏览 241
已采纳

go-zookeeper Connect在没有有效连接的情况下返回

I have a question regarding zookeeper, I am trying to implement simple service discovery with zookeeper in go, I am using go-zookeeper: https://github.com/samuel/go-zookeeper

My question is whenever I connect to zookeeper using for example:

zoo_keeper, _, err := zk.Connect(s, time.Second)

the function returns immediately and no error is reported, but there is no actual valid connection yet. Now for example if I want to create znodes, what do I need to check to make sure I have a valid connection before doing so?

  • 写回答

1条回答 默认 最新

  • dss67853 2017-01-10 19:53
    关注

    After trying several things, and thanks to the suggestion from Imesha, I can achieve what I wanted using channels. Below is my sample code.

    func zk_connect(zk_server string) (*zk.Conn, error) {
        zoo_keeper, session, err := zk.Connect([]string{zk_server}, time.Second)
        if err != nil {
            return nil, err
        }
        for event := range session {
            if event.State == zk.StateConnected {
                log.Printf("zookeeper State: %s
    ", event.State)
                break
            }
        }
        return zoo_keeper, nil
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器