Пример #1
0
 /// <summary>
 //  从主集群服务器获取本集群信息
 /// </summary>
 /// <returns></returns>
 public zoneServerCluster getClusterFromZoneMaster(string hostUri)
 {
     try
     {
         var respone = hclient.doAsycHttpRequest(string.Format(getZoneClustUrl, hostUri, localRunServer.Instance.zone));
         if (respone.IsSuccessStatusCode)
         {
             var jobj = JObject.Parse(hclient.ResponseToString(respone));
             if (checkOk(jobj))
             {
                 zoneServerCluster zsc = zoneServerCluster.parlseJson(jobj["Data"].ToString());
                 return(zsc);
             }
             else
             {
                 FrmLib.Log.commLoger.runLoger.Error(string.Format("server {0} getClusterFromZoneMaster to{1} error ,resopne:{2}", ownServer.id, ownCluser.master.serviceUrl, jobj.ToString()));
             }
         }
         FrmLib.Log.commLoger.runLoger.Error(string.Format("server {0} getClusterFromZoneMaster to{1} error ,resopne:{2}", ownServer.id, ownCluser.master.serviceUrl, respone.ToString()));
     }
     catch (Exception ex)
     {
         Console.WriteLine("some error:ex:" + FrmLib.Extend.tools_static.getExceptionMessage(ex));
         return(null);
     }
     return(null);
 }
Пример #2
0
        /// <summary>
        /// 向主集群服务器注册(集群从服务器或副本),返回数据中
        /// data.type代表是从服务器还是副本
        /// </summary>
        public JObject registerForZoneMaster(zoneServerCluster zsc)
        {
            if (this.ownServer != null && localRunServer.Instance.zoneRole != ServerRoleEnum.zoneMaster)
            {
                var respone = hclient.doAsycHttpRequest(string.Format(registerForZoneUrl, zsc.master.serviceUrl), JObject.FromObject(this.ownServer), false, zsc.master.serviceUrl.ToLower().StartsWith("https"));

                if (respone.IsSuccessStatusCode)
                {
                    return(JObject.Parse(hclient.ResponseToString(respone)));
                }
                else
                {
                    FrmLib.Log.commLoger.runLoger.Error(string.Format("server {0} registerForZoneMaster {1} error ,resopne:{2}", ownServer.id, zsc.master.serviceUrl, respone.ToString()));
                }
            }
            return(null);
        }
Пример #3
0
        public IActionResult registerToRegionMaster([FromBody] zoneServerCluster zsc)
        {
            if (localRunServer.Instance.zoneRole != ServerRoleEnum.regionMaster)
            {
                return(FailedMsg(400, "not a zoneMaster"));
            }
            if (zsc == null || zsc.master == null)
            {
                return(FailedMsg(400, "param error"));
            }
            var zoneName               = zsc.zoneName;
            var server                 = zsc.master;
            var regionZone             = getRegion(localRunServer.Instance.region);
            var zoneCluster            = getZone(zoneName, localRunServer.Instance.region);
            proxyNettyServer oldmaster = null;

            if (zoneCluster != null)
            {
                oldmaster = zoneCluster.master;
            }

            if (oldmaster != null && oldmaster.status == serverStatusEnum.Ready && oldmaster.id != server.id)
            {
                return(FailedMsg(400, "the  cluster master is already run"));
            }

            if (zoneCluster.clusterId == zsc.clusterId)
            {
                foreach (var one in zoneCluster.allAvailableServers())
                {
                    if (one.id != server.id)
                    {
                        if (zsc.slave == null)
                        {
                            zsc.setSlave(one);
                        }
                        else
                        {
                            zsc.addRepetion(one);
                        }
                    }
                }
            }


            Object o;

            lock (regionZone)
            {
                if (regionZone.containZone(zsc.zoneName))
                {
                    regionZone.removeZoneServerClusterByName(zsc.zoneName);
                }
                regionZone.addZoneServer(zsc);

                if (regionZone.regionSlave == null)
                {
                    regionZone.setSlave(server);
                    o = new { serverType = (int)ServerRoleEnum.regionSlave };
                }
                else
                {
                    if (regionZone.regionSlave.id == server.id)
                    {
                        var oldserver = regionZone.getServerById(server.id);
                        oldserver = server;
                        regionZone.setSlave(server);//待确认
                        o = new { serverType = (int)ServerRoleEnum.regionSlave };
                    }
                    else
                    {
                        o = new { serverType = (int)ServerRoleEnum.unkown }
                    };
                }
            }
            return(SuccessData(o));
        }
Пример #4
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();//启动自身的转发服务
        }
Пример #5
0
        /// <summary>
        /// 选集群主控
        /// </summary>
        /// <param name="serverlist"></param>
        /// <returns></returns>
        private proxyNettyServer selectZoneMasterServer(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;
                zoneServerCluster maxrzs             = null;
                IList <Tuple <proxyNettyServer, zoneServerCluster> > waitforNextList2 = new List <Tuple <proxyNettyServer, zoneServerCluster> >();

                //比较包含服务器的数量
                foreach (var one in waitforNextList)
                {
                    var zcluster = _rsycClient.getClusterFromZoneMaster(one.serviceUrl);
                    if (zcluster.getZoneServerCount() > zoneCount)
                    {
                        maxZoneCountServer = one;
                        maxrzs             = zcluster;
                        zoneCount          = zcluster.getZoneServerCount();
                        waitforNextList2.Clear();
                    }
                    else
                    {
                        if (zcluster.getZoneServerCount() == zoneCount)
                        {
                            if (maxZoneCountServer.id == one.id)
                            {
                                continue;//相同的server
                            }
                            waitforNextList2.Add(new Tuple <proxyNettyServer, zoneServerCluster>(one, zcluster));
                        }
                    }
                }

                waitforNextList2.Add(new Tuple <proxyNettyServer, zoneServerCluster>(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].Item1;
                        var tmp = obj.mapGroupCount;
                        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.noticeZoneSetMaster(serverlist, masterServer);   //通知集群设置主服务器
            return(masterServer);
        }