Exemplo n.º 1
0
 /// <summary>
 /// 获取全域信息,非主域控服务器
 /// </summary>
 /// <returns></returns>
 public regionZoneServer getRegionFromRegionMaster(string hostUri)
 {
     try
     {
         var respone = hclient.doAsycHttpRequest(string.Format(getRegionUrl, hostUri, localRunServer.Instance.region));
         if (respone.IsSuccessStatusCode)
         {
             var jobj = JObject.Parse(hclient.ResponseToString(respone));
             if (checkOk(jobj))
             {
                 regionZoneServer zsc = regionZoneServer.parlseJson(jobj["Data"].ToString());
                 return(zsc);
             }
             else
             {
                 FrmLib.Log.commLoger.runLoger.Error(string.Format("server {0} getRegionFromRegionMaster to{1} error ,resopne:{2}", ownServer.id, ownCluser.master.serviceUrl, jobj.ToString()));
             }
         }
         FrmLib.Log.commLoger.runLoger.Error(string.Format("server {0} getRegionFromRegionMaster to{1} error ,resopne:{2}", ownServer.id, ownCluser.master.serviceUrl, respone.ToString()));
     }
     catch (Exception ex)
     {
         Console.WriteLine("getRegionFromRegionMaster some error:ex:" + FrmLib.Extend.tools_static.getExceptionMessage(ex));
         return(null);
     }
     return(null);
 }
Exemplo n.º 2
0
        private zoneServerCluster getZone(string zoneName, string regionName)
        {
            regionZoneServer regionZone = getRegion(regionName);

            if (regionZone.containZone(zoneName))
            {
                return(regionZone.getzoneServerClusterByName(zoneName));
            }

            return(null);
        }
Exemplo n.º 3
0
        private regionZoneServer getRegion(string regionName)
        {
            regionZoneServer regionZone = null;

            if (localRunServer.Instance.region_dic.ContainsKey(regionName))
            {
                regionZone = localRunServer.Instance.region_dic[regionName];
            }


            return(regionZone);
        }
Exemplo n.º 4
0
        public IActionResult sayAliveToZoneMaster([FromRoute] string id)
        {
            if (localRunServer.Instance.zoneRole != ServerRoleEnum.zoneMaster)
            {
                return(FailedMsg(502, "not a zoneMaster"));
            }
            regionZoneServer regionZone = getRegion(localRunServer.Instance.region);

            var server = regionZone.getServerById(id);

            if (server != null)
            {
                server.lastLive = DateTime.Now;
            }
            return(SuccessMsg());
        }
Exemplo n.º 5
0
        /// <summary>
        /// 向主域服务器注册(集群主服务器)
        ///
        /// </summary>
        public JObject   registerForRegionMaster(regionZoneServer rzs)
        {
            if (this.ownServer != null && localRunServer.Instance.zoneRole != ServerRoleEnum.regionMaster)
            {
                var respone = hclient.doAsycHttpRequest(string.Format(registerForZoneUrl, rzs.regionMaster.serviceUrl), JObject.FromObject(this.ownServer), false, rzs.regionMaster.serviceUrl.ToLower().StartsWith("https"));

                if (respone.IsSuccessStatusCode)
                {
                    return(JObject.Parse(hclient.ResponseToString(respone)));
                }
                else
                {
                    FrmLib.Log.commLoger.runLoger.Error(string.Format("server {0} registerForRegionMaster {1} error ,resopne:{2}", ownServer.id, rzs.regionMaster.serviceUrl, respone.ToString()));
                }
            }
            return(null);
        }
Exemplo n.º 6
0
        public IActionResult region([FromRoute] string regionName)
        {
            if (string.IsNullOrEmpty(regionName))
            {
                return(FailedMsg(400, "bad region name"));
            }



            regionZoneServer regionZone = getRegion(regionName);

            if (regionZone == null)
            {
                return(FailedMsg(404, "no found"));
            }

            return(SuccessData(regionZone));
        }
Exemplo n.º 7
0
        /// <summary>
        /// 启动服务
        ///
        /// </summary>
        public void startServer()
        {
            regionZoneServer  rzs        = null;
            proxyNettyServer  zoneMaster = null;
            zoneServerCluster zsc        = null;
            var regionMaster             = getRegionMaster();

            if (regionMaster == null)
            {
                this.regionRole = ServerRoleEnum.regionMaster;
            }
            else
            {
                rzs = _rsycClient.getRegionFromRegionMaster(regionMaster.serviceUrl);
                if (rzs.ContainsKey(this.zoneclusterId))
                {
                    var cluster = rzs.getzoneServerCluster(this.zoneclusterId);
                    if (cluster != null && cluster.master != null)
                    {
                        zoneMaster = cluster.master;
                    }
                    else
                    {
                        //集群存在,但集群的主服务器是空,存在异常,但有可能是集群的主服务器掉线,新的主服务器还没有选出;退出重试
                        throw new Exception("从域服务器获得的集群信息中,主服务器异常");
                    }
                }
                if (zoneMaster != null && !zoneUrls.Contains(zoneMaster.serviceUrl))
                {
                    zoneUrls.Add(zoneMaster.serviceUrl);
                }
            }

            zoneMaster = getZoneMaster(zoneUrls);
            if (zoneMaster == null)
            {
                if (rzs != null && rzs.ContainsKey(this.zoneclusterId))
                {
                    throw new Exception("获取不到集群主服务器,但主域的信息中包含了集群的信息");
                }
                zoneRole = ServerRoleEnum.zoneMaster;
            }
            else
            {
                zsc = _rsycClient.getClusterFromZoneMaster(zoneMaster.serviceUrl);
            }


            bool   isRegionMaster = regionRole == ServerRoleEnum.regionMaster;
            bool   isZoneMaster   = zoneRole == ServerRoleEnum.zoneMaster;
            string serviceUrl     = string.Format("{0}://{1}:{2}", protocol, ownHost, lPort);

            if (isRegionMaster && isZoneMaster)
            {
                //既是主域服务器也是主集群服务器
                ownServer = new proxyNettyServer(this.clusterId, this.httpProxyPort, serviceUrl);
                ownServer.loadPortProxyCfg();
                regionZoneServer rz = new regionZoneServer(region);
                rz.addZoneServer(new zoneServerCluster(region, zone, ownServer));
                rz.setMaster(ownServer);
                this.region_dic.Add(region, rz);
                RegionBroadCastTimer.Elapsed  += new System.Timers.ElapsedEventHandler(broadCastForRegion);
                RegionBroadCastTimer.AutoReset = true;
                RegionBroadCastTimer.Start();
            }
            else
            {
                if (!isRegionMaster)
                {
                    if (isZoneMaster)
                    {
                        ownServer = new proxyNettyServer(this.clusterId, this.httpProxyPort, serviceUrl);
                        ownServer.loadPortProxyCfg();
                        zsc = new zoneServerCluster(this.region, this.zone, ownServer);
                        var jobj = _rsycClient.registerForRegionMaster(rzs);
                        if (jobj["status"].ToObject <int>() != 200)
                        {
                            throw new Exception("服务注册失败");
                        }
                        rzs.addZoneServer(zsc);
                        zsc.setMaster(ownServer);
                        this.region_dic.Add(this.region, rzs);
                        sayAliveTimer.Elapsed  += new System.Timers.ElapsedEventHandler(triggerSayAliveToRegionMaster);
                        sayAliveTimer.AutoReset = true;
                        sayAliveTimer.Start();
                    }
                    else
                    {
                        this.clusterId = zsc.clusterId;
                        ownServer      = new proxyNettyServer(this.clusterId, this.httpProxyPort, serviceUrl);
                        ownServer.loadPortProxyCfg();
                        var jobj = _rsycClient.registerForZoneMaster(zsc);
                        if (jobj["Status"].ToObject <int>() != 100)
                        {
                            throw new Exception("服务注册失败");
                        }
                        else
                        {
                            if (jobj["Data"]["serverType"].ToObject <int>() == (int)ServerRoleEnum.zoneSlave)
                            {
                                zsc.setSlave(ownServer);
                            }
                            else
                            {
                                zsc.addRepetion(ownServer);
                            }
                        }

                        rzs.addZoneServer(zsc);
                        this.region_dic.Add(region, rzs);

                        sayAliveTimer.Elapsed  += new System.Timers.ElapsedEventHandler(triggerSayAliveToZoneMaster);
                        sayAliveTimer.AutoReset = true;
                        sayAliveTimer.Start();
                    }
                }
                else
                {
                    //是域主控,不是集群主控;这种情况暂不考虑,不应该发生,没有域信息

                    throw new Exception("是域主控,不是集群主控是异常信息");
                }
            }

            actionDoTimer.Start();
            checkAliveTimer.Start();
            ownServer.Start();//启动自身的转发服务
        }
Exemplo n.º 8
0
        /// <summary>
        /// 选择域主控服务器
        /// </summary>
        /// <param name="serverlist">从域中获取到主服务器列表</param>
        /// <returns></returns>
        private proxyNettyServer selectRegionMasterServer(List <proxyNettyServer> serverlist)
        {
            if (serverlist == null || serverlist.Count == 0)
            {
                return(null);
            }
            if (serverlist.Count == 1)
            {
                return(serverlist[0]);
            }

            proxyNettyServer         masterServer    = null;
            IList <proxyNettyServer> waitforNextList = new List <proxyNettyServer>();

            var olddt = (from x in serverlist select x).Min(a => a.createDt);

            waitforNextList = (from x in serverlist where x.createDt == olddt select x).ToList();
            if (waitforNextList.Count > 1)
            {
                int zoneCount = int.MinValue;
                proxyNettyServer maxZoneCountServer = null;
                regionZoneServer maxrzs             = null;
                IList <Tuple <proxyNettyServer, regionZoneServer> > waitforNextList2 = new List <Tuple <proxyNettyServer, regionZoneServer> >();

                //比较包含集群的数量
                foreach (var one in waitforNextList)
                {
                    var rzs = _rsycClient.getRegionFromRegionMaster(one.serviceUrl);
                    if (rzs.zoneServerCount > zoneCount)
                    {
                        maxZoneCountServer = one;
                        maxrzs             = rzs;
                        zoneCount          = rzs.zoneServerCount;
                        waitforNextList2.Clear();
                    }
                    else
                    {
                        if (rzs.zoneServerCount == zoneCount)
                        {
                            if (maxZoneCountServer.id == one.id)
                            {
                                continue;//相同的server
                            }
                            waitforNextList2.Add(new Tuple <proxyNettyServer, regionZoneServer>(one, rzs));
                        }
                    }
                }

                waitforNextList2.Add(new Tuple <proxyNettyServer, regionZoneServer>(maxZoneCountServer, maxrzs));
                if (waitforNextList2.Count > 1)
                {
                    long                     serverCount      = int.MinValue;
                    proxyNettyServer         maxCountServer   = null;
                    IList <proxyNettyServer> waitforNextList3 = new List <proxyNettyServer>();
                    //比较总服务器数量
                    for (int i = 0; i < waitforNextList2.Count; i++)
                    {
                        var obj = waitforNextList2[i].Item2;
                        var tmp = obj.getServerCount();
                        if (tmp > serverCount)
                        {
                            serverCount    = tmp;
                            maxCountServer = waitforNextList2[i].Item1;
                            waitforNextList3.Clear();
                        }
                        else
                        {
                            if (tmp == serverCount)
                            {
                                if (maxCountServer.id == waitforNextList2[i].Item1.id)
                                {
                                    continue;
                                }
                                waitforNextList3.Add(waitforNextList2[i].Item1);
                            }
                        }
                    }
                    waitforNextList3.Add(maxCountServer);
                    if (waitforNextList3.Count > 1)
                    {
                        //仍然相等,不做处理,返回第一个
                        return(waitforNextList3[0]);
                    }
                    else
                    {
                        masterServer = waitforNextList3[0];
                    }
                }
                else
                {
                    masterServer = waitforNextList2[0].Item1;
                }
            }
            else
            {
                masterServer = waitforNextList[0];
            }
            _rsycClient.noticeRegionSetMaster(serverlist, masterServer);  //通知相关的服务器设置master
            return(masterServer);
        }