示例#1
0
文件: zk.go 项目: ljvblfz/slot-golang
func WatchRmq(rmqRoot string) {
	zkRmqRoot := "/" + rmqRoot
	glog.Infof("Watching rabbitmq root [%s] OK!", zkRmqRoot)
	for {
		nodes, watch, err := zk.GetNodesW(zkConn, zkRmqRoot)
		if err == zookeeper.ErrNoNode || err == zookeeper.ErrNoChildrenForEphemerals {
			if glog.V(1) {
				glog.Errorf("[zk|rmq] error %v", err)
			}
			time.Sleep(time.Second)
			continue
		} else if err != nil {
			if glog.V(1) {
				glog.Errorf("[zk|rmq] error %v", err)
			}
			time.Sleep(time.Second)
			continue
		}
		var addrs []string = make([]string, 0, len(nodes))
		for _, n := range nodes {
			addr, err := zk.GetNodeData(zkConn, zkRmqRoot+"/"+n)
			if err != nil {
				glog.Errorf("[zk|rmq] [%s] cannot get", addr)
				continue
			}
			addrs = append(addrs, addr)
		}
		for _, addr := range addrs {
			confs := strings.SplitN(addr, ",", 2)
			if len(confs) != 2 {
				glog.Errorf("[zk|rmq] data in zk is not valid format(eg: url,queuename): %s", confs)
				continue
			}
			glog.Infof("[rmq] online %s", addr)
			GRmqs.Add(confs[0], confs[1])
		}
		e := <-watch
		glog.Infof("[zk|rmq] zk receive an event %v", e)
	}
}
示例#2
0
文件: zk.go 项目: ljvblfz/slot-golang
func InitZK(zkAddrs []string, msgbusName string, cometName string) {
	if len(msgbusName) == 0 {
		glog.Fatalf("[zk] root name for msgbus cannot be empty")
	}
	if len(cometName) == 0 {
		glog.Fatalf("[zk] root name for comet cannot be empty")
	}
	var (
		nodes []string
		err   error
		conn  *zookeeper.Conn
		addr  string
		watch <-chan zookeeper.Event
	)
	conn, err = zk.Connect(zkAddrs, 60*time.Second, onConnStatus)
	if err != nil {
		glog.Fatal(err)
	}
	zkConn = conn

	zkCometRoot = "/" + cometName

	err = zk.Create(zkConn, zkCometRoot)
	if err != nil {
		glog.Infof("[zk] create connection error: %v", err)
	}
	go ReportUsage()

	zkMsgBusRoot := "/" + msgbusName
	glog.Infof("Connect zk[%v] with msgbus root [%s] OK!", zkAddrs, zkMsgBusRoot)

	var lastErr error
	for {
		nodes, watch, err = zk.GetNodesW(conn, zkMsgBusRoot)
		if err != lastErr {
			if err != nil {
				glog.Errorln(err)
			}
			lastErr = err
		}
		if err == zookeeper.ErrNoNode || err == zookeeper.ErrNoChildrenForEphemerals {
			time.Sleep(time.Second)
			continue
		} else if err != nil {
			time.Sleep(time.Second)
			continue
		}
		var addrs []string = make([]string, 0, len(nodes))
		for _, n := range nodes {
			addr, err = zk.GetNodeData(conn, zkMsgBusRoot+"/"+n)
			if err != nil {
				glog.Errorf("[%s] cannot get", addr)
				continue
			}
			addrs = append(addrs, addr)
		}
		for _, addr := range addrs {
			GMsgBusManager.Online(addr)
		}
		e := <-watch
		glog.Infof("zk receive an event %v", e)
		time.Sleep(time.Second)
	}
}