简体   繁体   中英

go-zookeeper Connect returns without a valid connection

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?

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\n", event.State)
            break
        }
    }
    return zoo_keeper, nil
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM