Beispiel #1
0
        public IActionResult reportServerChanged([FromRoute] string id, [FromBody] proxyNettyServer server)
        {
            if (localRunServer.Instance.ownServer.id != id)
            {
                return(FailedMsg("错误的Id,与请求服务器不符"));
            }
            if (localRunServer.Instance.zoneRole != ServerRoleEnum.zoneMaster)
            {
                return(FailedMsg("I am not a master server"));
            }
            var oldserver = localRunServer.Instance.ownCluster.getzoneServerById(server.id);

            if (oldserver == null)
            {
                if (localRunServer.Instance.ownCluster.slave == null)
                {
                    localRunServer.Instance.ownCluster.setSlave(server);
                }
                else
                {
                    localRunServer.Instance.ownCluster.addRepetion(server);
                }
            }
            else
            {
                oldserver = server;
            }

            var           o  = new { Actiondo = (int)enum_Actiondo.needRsycOneServer, url = localRunServer.Instance.ownServer.serviceUrl, region = localRunServer.Instance.region, zone = localRunServer.Instance.zone, id = server.id };
            actionMessage am = new actionMessage(enum_Actiondo.needNoticeServer, localRunServer.Instance.ownServer.id, localRunServer.Instance.region, "", "", JsonConvert.SerializeObject(o));

            localRunServer.Instance.addActions(am);
            return(SuccessMsg());
        }
Beispiel #2
0
 /// <summary>
 /// 代理服务器监测转发服务组
 /// </summary>
 private void checkMapGroup(proxyNettyServer server)
 {
     if (server.checkMapGroupChange())
     {
         actionMessage am = new actionMessage(enum_Actiondo.ServerChanged, ownServer.id, region, "", "", JsonConvert.SerializeObject(new { url = ownServer.serviceUrl, region = region, zone = zoneclusterId, id = server.id }));
         addActions(am);
     }
 }
Beispiel #3
0
        public IActionResult registerToZoneMaster([FromBody] proxyNettyServer server)
        {
            //Stream stream = HttpContext.Request.Body;
            //byte[] buffer = new byte[HttpContext.Request.ContentLength.Value];
            //stream.Read(buffer, 0, buffer.Length);
            //string temp = Encoding.UTF8.GetString(buffer);
            //Console.WriteLine(temp);
            //if (string.IsNullOrEmpty(temp))
            //    return FailedMsg("参数为空");
            //proxyNettyServer server = JsonConvert.DeserializeObject<proxyNettyServer>(temp);

            if (localRunServer.Instance.zoneRole != ServerRoleEnum.zoneMaster)
            {
                return(FailedMsg(400, "not a zoneMaster"));
            }
            var zoneCluster = getZone(localRunServer.Instance.zone, localRunServer.Instance.region);

            if (zoneCluster == null || zoneCluster.clusterId != server.clusterID)
            {
                return(FailedMsg(400, "not a same cluster"));
            }
            Object o;

            lock (zoneCluster)
            {
                if (zoneCluster.slave == null)
                {
                    zoneCluster.setSlave(server);
                    o = new { serverType = (int)ServerRoleEnum.zoneSlave };
                }
                else
                {
                    if (zoneCluster.slave.id == server.id)
                    {
                        zoneCluster.setSlave(server);
                        o = new { serverType = (int)ServerRoleEnum.zoneSlave };
                    }
                    else
                    {
                        var oldserver = (from x in zoneCluster.repetionList where x.id == server.id select x).FirstOrDefault();
                        if (oldserver != null)
                        {
                            oldserver.setStatus(serverStatusEnum.Ready);
                        }
                        else
                        {
                            zoneCluster.addRepetion(server);
                        }
                        o = new { serverType = (int)ServerRoleEnum.zoneRepetiton };
                    }
                }
            }
            return(SuccessData(o));
        }
Beispiel #4
0
        /// <summary>
        /// 检查单台服务器,失联时间超过remove时间,移除并不在检查;小于移除时间大于失效时间,设置失效状态,但进行检查
        /// </summary>
        /// <param name="server"></param>
        private void checkOneProxyServer(proxyNettyServer server)
        {
            if (server == null)
            {
                return;
            }
            if (server.id == ownServer.id)
            {
                return;
            }
            if (server.status == serverStatusEnum.Disable)
            {
                return;
            }
            var ts        = (DateTime.Now - server.lastLive).TotalSeconds;
            var oldstatus = server.status;

            if (ts > this.serverRemoveTimes)
            {
                //should remove server
                Console.WriteLine("checkOneProxyServer server set Disable ");
                server.setStatus(serverStatusEnum.Disable);
            }
            else
            {
                if (ts > this.serverFailTimes)
                {
                    var sid = _rsycClient.getEchoServerId(server.serviceUrl);
                    if (sid == server.id)
                    {
                        Console.WriteLine("checkOneProxyServer server set ready ");
                        server.lastLive = DateTime.Now;
                        server.setStatus(serverStatusEnum.Ready);
                    }
                    else
                    {
                        Console.WriteLine("checkOneProxyServer server set fail ");
                        server.setStatus(serverStatusEnum.Fail);
                    }
                }
            }

            if (oldstatus != server.status)
            {
                Console.WriteLine("checkOneProxyServer server add actions ");
                actionMessage am = new actionMessage(enum_Actiondo.ServerChanged, ownServer.id, region, "", "", JsonConvert.SerializeObject(new { url = ownServer.serviceUrl, region = region, zone = zoneclusterId, id = server.id }));
                addActions(am);
            }
        }
Beispiel #5
0
        public proxyNettyServer getServer(string url, string id, string region, string zone)
        {
            var respone = hclient.doAsycHttpRequest(string.Format(getServerUrl, url, id, region, zone));

            if (respone.IsSuccessStatusCode)
            {
                var jobj = JObject.Parse(hclient.ResponseToString(respone));
                if (checkOk(jobj))
                {
                    proxyNettyServer server = proxyNettyServer.parlseJson(jobj["Data"].ToString());
                    return(server);
                }
                else
                {
                    FrmLib.Log.commLoger.runLoger.Error(string.Format("server {0} getServer to{1} error ,resopne:{2}", id, url, jobj.ToString()));
                }
            }
            return(null);
        }
Beispiel #6
0
        /// <summary>
        /// 通知服务器列表重新设置集群主服务器
        /// </summary>
        /// <param name="noticeList">被通知列表</param>
        /// <param name="masterServer">集群主服务器</param>

        public void noticeZoneSetMaster(IList <proxyNettyServer> noticeList, proxyNettyServer masterServer)
        {
            foreach (var one in noticeList)
            {
                if (one.id == ownServer.id)
                {
                    continue;
                }
                var jobj = (new JObject());
                jobj.Add("url", masterServer.serviceUrl);
                actionMessage am      = new actionMessage(enum_Actiondo.noticeToRsycZoneMaster, ownServer.id, this.region, one.id, this.region, jobj.ToString());
                var           respone = hclient.doAsycHttpRequest(string.Format(zoneMasterNoticeUrl, one.serviceUrl, one.id), JObject.FromObject(am), false, one.serviceUrl.ToLower().StartsWith("https"));

                if (respone.IsSuccessStatusCode)
                {
                    continue;
                }
                else
                {
                    FrmLib.Log.commLoger.runLoger.Error(string.Format("server {0} noticeZoneSetMaster {1} error ,resopne:{2}", ownServer.id, one.serviceUrl, respone.ToString()));
                }
            }
        }
Beispiel #7
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));
        }
Beispiel #8
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();//启动自身的转发服务
        }
Beispiel #9
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);
        }
Beispiel #10
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);
        }