Example #1
0
func InitZK(zkAddrs []string, rootName string) {
	if len(rootName) == 0 {
		glog.Fatalf("[zk] root name for rabbitmq cannot be empty")
	}
	var (
		err  error
		conn *zookeeper.Conn
	)

	gData = []byte(fmt.Sprintf("%s,MsgTopic", gAddr))

	zkRoot = "/" + rootName

	conn, err = zk.Connect(zkAddrs, 60*time.Second, onConnStatus)
	if err != nil {
		glog.Fatal(err)
	}
	zkConn = conn

	err = zk.Create(zkConn, zkRoot)
	if err != nil {
		glog.Infof("[zk] create connection error: %v", err)
	}
	go handleEvent()
}
Example #2
0
func InitZK(addrs []string, listenAddr string, rootName string, rmqRoot string) error {
	conn, err := zk.Connect(addrs, 60*time.Second, watchEvent)
	if err != nil {
		return err
	}
	zkRoot = "/" + rootName
	zkConn = conn
	zkListenAddr = listenAddr

	zk.Create(conn, zkRoot)
	glog.Infof("Connect zk[%v] on msgbus root [%s] OK!", addrs, rootName)
	createErr := zk.CreateTempW(conn, zkRoot, listenAddr, watchZK)
	zkOk.Set(true)
	if createErr != nil {
		return createErr
	}

	go WatchRmq(rmqRoot)
	return nil
}
Example #3
0
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)
	}
}