Exemplo n.º 1
0
func GetAvalConnSrv(w http.ResponseWriter, r *http.Request) {
	//获取可用的conn srv子节点列表
	children := common.ZkGetChildren(zkConn, Conf.ZkRoot)
	if children != nil {
		//随机一个机器
		rand.Seed(time.Now().UnixNano())
		idx := rand.Intn(len(children))
		host, _ := redis.String(redClient.Do("GET", RCP_TCP_HOST_PREFIX+children[idx]))
		retJson(w, r, 0, host, nil)
		return
	}

	retJson(w, r, -1, "没有可用的连接服务器", nil)
	return
}
Exemplo n.º 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)
			}
		}
	}()
}
Exemplo n.º 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
}
Exemplo n.º 4
0
func StartSendSrv() {
	isMsStop = true
	isPsStop = true
	msLen = 0
	psLen = 0

	rand.Seed(time.Now().UnixNano())

	conn, err := redis.Dial("tcp", Conf.Redis)
	if err != nil {
		fmt.Printf("tcp redis %v\n", err.Error())
		return
	}
	redClient = conn

	msgGet, msgPut = common.MakeMessageRecycler()

	msgPushPool = make(chan *common.Message, 4096)
	msgStorePool = make(chan *common.Message)

	go func() {
		msIdx := 0
		for {
			//ms列表有变化
			if isMsStop || msLen == 0 {
				time.Sleep(time.Second * 1)
				continue
			}
			msIdx = rand.Intn(msLen)
			m, err := redis.String(redClient.Do("RPOP", "msg_queue_0"))
			if m == "" {
				time.Sleep(time.Second)
				continue
			}
			if err == nil {
				var msg common.Message
				err = json.Unmarshal([]byte(m), &msg)
				if err == nil {
					HandleServerMsg(&msg, msClients[msIdx])
				}
			}
		}
	}()

	go func() {
		var success bool
		psIdx := 0
		for {
			//ms列表有变化
			if isPsStop || psLen == 0 {
				time.Sleep(time.Second * 1)
				continue
			}

			if m := <-msgPushPool; m != nil {
				psIdx = rand.Intn(psLen)
				err := pushSrvClients[psIdx].Call("PushSrv.SendMsg", *m, &success)
				if err != nil || !success {
					fmt.Printf("call push srv send message failed")
				}
			}
		}
	}()

	go func() {
		var success bool
		msIdx := 0
		for {
			//ms列表有变化
			if isMsStop || msLen == 0 {
				time.Sleep(time.Second * 1)
				continue
			}

			select {
			case m := <-msgStorePool:
				//MS落地存储
				msIdx = rand.Intn(msLen)
				err := msClients[msIdx].Call("MS.SaveMessage", *m, &success)
				if err != nil || !success {
					fmt.Printf("send server call MS SaveMessage failed")
				}
				msgPut <- m
			}
		}
	}()

	//服务器初始化完成,启动zookeeper
	zkConn = common.ZkConnect(Conf.ZooKeeper)
	size := 0
	//获取子节点列表
	children := common.ZkGetChildren(zkConn, Conf.MsZkRoot)
	if children != nil {
		//开启新的客户端
		for _, host := range children {
			msClients[size] = initRpcClient(host)
			size++
		}

		msLen = size
		isMsStop = false
		fmt.Printf("ms len %d\n", msLen)
	}
	//获取push子节点列表
	psSize := 0
	psChildren := common.ZkGetChildren(zkConn, Conf.PsZkRoot)
	if psChildren != nil {
		//开启新的客户端
		for _, psHost := range psChildren {
			pushSrvClients[psSize] = initRpcClient(psHost)
			psSize++
		}

		psLen = psSize
		isPsStop = false
		fmt.Printf("ps len %d\n", psLen)
	}

	//开一个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] = initRpcClient(host)
					size++
				}

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

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

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

				psSize = 0
				//关闭原先push客户端
				for _, psc := range pushSrvClients {
					if psc != nil {
						psc.Close()
						pushSrvClients[psSize] = nil
					}
					psSize++
				}

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

				psSize = 0
				//开启新的客户端
				for _, host := range children {
					pushSrvClients[psSize] = initRpcClient(host)
					psSize++
				}

				psLen = psSize
				isPsStop = false
				fmt.Printf("ps len %d\n", psLen)
			}
		}
	}()
}