Beispiel #1
0
func StartMs() {

	ms := NewMS()
	rpc.Register(ms)
	rpc.HandleHTTP()

	go func() {
		err := http.ListenAndServe(Conf.TcpBind, nil)
		if err != nil {
			fmt.Printf("ms rpc error: %s\n", err.Error())
		}
	}()

	//服务器初始化完成以后,开启zookeeper
	zkConn = common.ZkConnect(Conf.ZooKeeper)
	common.ZkCreateRoot(zkConn, Conf.ZkRoot)
	//为当前ms服务器创建一个节点,加入到ms集群中
	path := Conf.ZkRoot + "/" + Conf.TcpBind
	common.ZkCreateTempNode(zkConn, path)
	go func() {
		exist, _, watch, err := zkConn.ExistsW(path)
		if err != nil {
			//发生错误,当前节点退出
			fmt.Printf("%s occur error\n", path)
			common.KillSelf()
		}

		if !exist {
			//节点不存在了
			fmt.Printf("%s not exist\n", path)
			common.KillSelf()
		}

		event := <-watch
		fmt.Printf("%s receiver a event %v\n", path, event)
	}()
}
Beispiel #2
0
//服务器启动
func (self *Server) Start() {
	listener, err := net.Listen("tcp", Conf.TcpBind)
	self.listener = listener
	if err != nil {
		fmt.Printf("server %s listen failed", Conf.TcpBind)
		return
	}

	fmt.Printf("server %s start\n", Conf.TcpBind)

	go func() {
		fmt.Printf("begin to accept connects\n")

		for {
			conn, err := self.listener.Accept()

			if err != nil {
				fmt.Printf("server accept error : \"%v\"", err)
				return
			}

			fmt.Printf("new client connect: \"%v\"", conn)
			self.pending <- conn
			fmt.Printf("new client into pending\n")
		}
	}()

	//服务器初始化完成以后,开启zookeeper
	zkConn = common.ZkConnect(Conf.ZooKeeper)
	common.ZkCreateRoot(zkConn, Conf.ZkRoot)
	//为当前服务器创建一个节点,加入到集群中
	path := Conf.ZkRoot + "/" + Conf.RcpBind
	common.ZkCreateTempNode(zkConn, path)
	go func() {
		for {
			exist, _, watch, err := zkConn.ExistsW(path)
			if err != nil {
				//发生错误,当前节点退出
				common.KillSelf()
				return
			}

			if !exist {
				//节点不存在了
				common.KillSelf()
				return
			}

			event := <-watch
			fmt.Printf("%s receiver a event %v\n", path, event)
		}
	}()

	//获取存储子节点列表
	var size int
	children := common.ZkGetChildren(zkConn, Conf.MsZkRoot)
	if children != nil {
		//开启新的客户端
		for _, host := range children {
			msClients[size] = common.InitRpcClient(host)
			size++
		}

		msLen = size
		isMsStop = false
		fmt.Printf("ms len %d\n", msLen)
	}

	//开一个goroute处理ms服务器节点变化
	go func() {
		for {
			_, _, events, err := zkConn.ChildrenW(Conf.MsZkRoot)
			if err != nil {
				time.Sleep(time.Second * 1)
				continue
			}

			evt := <-events
			if evt.Type == zk.EventNodeChildrenChanged {
				// ms节点有变化,更新ms列表
				// 停止当前的ms处理
				isMsStop = true
				time.Sleep(time.Second * 1) //暂停一秒,让在处理中的ms处理完成

				size = 0
				//关闭原先ms客户端
				for _, mc := range msClients {
					if mc != nil {
						mc.Close()
						msClients[size] = nil
					}
					size++
				}

				//获取子节点列表
				children := common.ZkGetChildren(zkConn, Conf.MsZkRoot)
				if children == nil {
					fmt.Printf("no ms rpc servers\n")
					continue
				}

				size = 0
				//开启新的客户端
				for _, host := range children {
					msClients[size] = common.InitRpcClient(host)
					size++
				}

				msLen = size
				isMsStop = false
				fmt.Printf("ms len %d\n", msLen)
			}
		}
	}()
}
Beispiel #3
0
func StartPushSrv() {
	ps := NewPushSrv()
	rpc.Register(ps)
	rpc.HandleHTTP()

	go func() {
		err := http.ListenAndServe(Conf.TcpBind, nil)
		if err != nil {
			fmt.Printf("pushsrv rpc error: %s\n", err.Error())
		}
	}()

	//服务器初始化完成以后,开启zookeeper
	zkConn = common.ZkConnect(Conf.ZooKeeper)
	common.ZkCreateRoot(zkConn, Conf.ZkRoot)
	//为当前push服务器创建一个节点,加入到push集群中
	path := Conf.ZkRoot + "/" + Conf.TcpBind
	common.ZkCreateTempNode(zkConn, path)
	go func() {
		exist, _, watch, err := zkConn.ExistsW(path)
		if err != nil {
			//发生错误,当前节点退出
			fmt.Printf("%s occur error\n", path)
			common.KillSelf()
		}

		if !exist {
			//节点不存在了
			fmt.Printf("%s not exist\n", path)
			common.KillSelf()
		}

		event := <-watch
		fmt.Printf("%s receiver a event %v\n", path, event)
	}()

	//初始化conn srv可用列表
	connZkConn := common.ZkConnect(Conf.ConnZooKeeper)
	children := common.ZkGetChildren(connZkConn, Conf.ConnZkRoot)
	if children != nil {
		//更新可用客户端列表
		for _, host := range children {
			avalConnClient[host] = initRpcClient(host)
		}
	}

	//开一个goroute处理conn srv服务器节点变化
	go func() {
		for {
			_, _, events, err := connZkConn.ChildrenW(Conf.ConnZkRoot)
			if err != nil {
				time.Sleep(time.Second * 1)
				continue
			}

			evt := <-events
			if evt.Type == zk.EventNodeChildrenChanged {
				//conn节点有变化,更新conn列表
				isStopSecond = true
				time.Sleep(time.Second * 1)

				for host, rcpCli := range avalConnClient {
					rcpCli.Close()
					delete(avalConnClient, host)
				}

				children := common.ZkGetChildren(connZkConn, Conf.ConnZkRoot)
				for _, host := range children {
					avalConnClient[host] = initRpcClient(host)
				}

				isStopSecond = false
			}
		}
	}()

	//可以开始处理消息
	isStopSecond = false
}