2019-02-28 02:41:16 +00:00
|
|
|
package zk
|
2019-02-28 10:01:09 +00:00
|
|
|
|
2019-02-28 02:41:16 +00:00
|
|
|
import (
|
2019-02-28 10:01:09 +00:00
|
|
|
_ "bytes"
|
2019-02-28 02:41:16 +00:00
|
|
|
zk "github.com/samuel/go-zookeeper/zk"
|
2019-03-13 01:20:09 +00:00
|
|
|
"go-tcpFramework-rudy/config"
|
2019-02-28 02:41:16 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
_ "strings"
|
2019-02-28 10:01:09 +00:00
|
|
|
"time"
|
2019-02-28 02:41:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var g_zkCnn *zk.Conn
|
|
|
|
|
|
|
|
type ServerStatus struct {
|
2019-02-28 10:01:09 +00:00
|
|
|
CnnCount int32 `json:"cnn_count"` //当前在线连接数目
|
|
|
|
ServerID string `json:"server_id"` //serverID
|
2019-02-28 02:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ConnectZooKeeper() error {
|
|
|
|
var e error
|
|
|
|
var evChan <-chan zk.Event
|
|
|
|
log.Println(config.Configs().Zkserver)
|
2019-02-28 10:01:09 +00:00
|
|
|
g_zkCnn, evChan, e = zk.Connect(config.Configs().Zkserver, time.Second)
|
|
|
|
if nil != e {
|
|
|
|
log.Println("", e.Error())
|
2019-02-28 02:41:16 +00:00
|
|
|
return e
|
|
|
|
}
|
2019-02-28 10:01:09 +00:00
|
|
|
if evChan == nil {
|
2019-02-28 02:41:16 +00:00
|
|
|
log.Println("")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2019-02-28 10:01:09 +00:00
|
|
|
if g_zkCnn == nil {
|
2019-02-28 02:41:16 +00:00
|
|
|
log.Println("[ERROR] could not connect zookeeper")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2019-02-28 10:01:09 +00:00
|
|
|
return nil
|
2019-02-28 02:41:16 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 10:01:09 +00:00
|
|
|
func ZkInstance() *zk.Conn {
|
2019-02-28 02:41:16 +00:00
|
|
|
return g_zkCnn
|
2019-02-28 10:01:09 +00:00
|
|
|
}
|