Example #1
0
File: zk.go Project: houcy/push
func InitZk(config *conf.ConfigStruct) error {
	conn, err := Connect(config.ZooKeeper.Addr, config.ZooKeeper.Timeout*time.Second)
	if err != nil {
		return err
	}
	_, err = conn.Create(config.ZooKeeper.Path, []byte(""), 0, zk.WorldACL(zk.PermAll))
	if err != nil && err != zk.ErrNodeExists {
		return err
	}
	ip := utils.GetLocalIP()
	port := config.Comet.Port
	addr := ip + ":" + port
	if port == "20000" {
		addr = ip
	}
	data, _ := json.Marshal(addr)
	err = Register(conn, config.ZooKeeper.Path, data)
	if err != nil {
		return err
	}
	return nil
}
Example #2
0
File: zk.go Project: houcy/push
func Register(conn *zk.Conn, fpath string, data []byte) error {
	_, err := conn.Create(fpath+"/", data, zk.FlagEphemeral|zk.FlagSequence, zk.WorldACL(zk.PermAll))
	if err != nil && err != zk.ErrNodeExists {
		return err
	}
	log.Infof("create zk node:%s", fpath)
	// watch self
	go func() {
		for {
			exist, _, watch, err := conn.ExistsW(fpath)
			if err != nil {
				log.Warnf("failed zk node \"%s\" set watch, [%v]", fpath, err)
				return
			}
			if !exist {
				log.Warnf("zk node \"%s\" not exist, [%v]", fpath, err)
				return
			}
			event := <-watch
			log.Infof("\"%s\" receive a event %v", fpath, event)
		}
	}()
	return nil
}