2017-01-07 20:03

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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dss67853 dss67853 4年前

    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)
        return zoo_keeper, nil
    点赞 评论 复制链接分享