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

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?

图片转代码服务由CSDN问答提供 功能建议

我有一个关于Zookeeper的问题,我正在尝试使用Zookeeper在go中实现简单的服务发现,我正在使用go -zookeeper: https://github.com/samuel/go-zookeeper

我的问题是每当我使用以下方式连接到zookeeper时:

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

该函数立即返回,并且未报告任何错误,但尚无实际的有效连接。 现在,例如,如果我要创建znodes,在执行此操作之前需要检查什么以确保我具有有效的连接?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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
    }
    
    打赏 评论

相关推荐 更多相似问题