Пример #1
0
        public static mapPortGroup ParseJson(string json)
        {
            //todo
            mapPortGroup mpg = JsonConvert.DeserializeObject <mapPortGroup>(json);

            return(mpg);
        }
Пример #2
0
        private bool checkOneGruopChange(mapPortGroup mpg)
        {
            var haveChanged = false;
            var allfailed   = true;

            foreach (var oneHost in mpg.outPortList)
            {
                if (oneHost.status == serverStatusEnum.Disable)
                {
                    continue;
                }
                var ts        = (DateTime.Now - oneHost.lastLive).TotalSeconds;
                var oldstatus = oneHost.status;
                if (ts > localRunServer.Instance.serverRemoveTimes)
                {
                    //
                    oneHost.setStatus(serverStatusEnum.Disable);
                }
                else
                {
                    if (ts > localRunServer.Instance.serverFailTimes)
                    {
                        if (checkOneMapHostOk(oneHost, mpg))
                        {
                            oneHost.lastLive = DateTime.Now;
                            oneHost.setStatus(serverStatusEnum.Ready);
                            allfailed = false;
                        }
                        else
                        {
                            oneHost.setStatus(serverStatusEnum.Fail);
                        }
                    }
                }

                if (oldstatus != oneHost.status)
                {
                    haveChanged = true;
                }
                if (oneHost.status == serverStatusEnum.Ready)
                {
                    allfailed = false;
                }
            }
            if (allfailed)
            {
                mpg.setstatus(serverStatusEnum.Fail);
            }
            else
            {
                mpg.setstatus(serverStatusEnum.Ready);
                mpg.lastLive = DateTime.Now;
            }
            return(haveChanged);
        }
Пример #3
0
 /// <summary>
 /// 更新基础属性是否需要重启
 /// </summary>
 /// <param name="jobj"></param>
 /// <returns></returns>
 public bool needRestartChanged(mapPortGroup obj)
 {
     if (this.port != obj.port ||
         this.httpsPort != obj.httpsPort ||
         this.host != obj.host ||
         this.useHttps != obj.useHttps ||
         this.appkey != obj.appkey)
     {
         return(true);
     }
     return(false);
 }
Пример #4
0
        public outMapPort(string cid, string host, string port, string httpsport, bool needcheck,
                          mapPortGroup opm, int _maxPerfData = 100, int _maxConnected = -1, bool _isfromeukera = false, int _planPercentCount = 1) : base()
        {
            this.clusterID        = cid;
            this.host             = host;
            this.port             = port;
            this.httpsPort        = httpsport;
            this.maxConnected     = _maxConnected;
            this.ownGroup         = opm;
            this.needCheckLive    = needcheck;
            this.isfromEureka     = _isfromeukera;
            this.planPercentCount = _planPercentCount;
            connectedCount        = 0;

            setStatus(serverStatusEnum.Ready);
        }
Пример #5
0
 /// <summary>
 /// 改变http转发的appkey
 /// </summary>
 /// <param name="mpg"></param>
 /// <param name="newKey"></param>
 public void changeAppkey(mapPortGroup mpg, string newKey)
 {
     if (mpg.mapType != 1)
     {
         return;
     }
     if (mpg.appkey == newKey)
     {
         return;
     }
     if (mpg.ownServerId != this.id || !maphttpGroup_dic.ContainsKey(mpg.appkey) || mpg.id != maphttpGroup_dic[mpg.appkey].id)
     {
         return;
     }
     maphttpGroup_dic.Remove(mpg.appkey);
     mpg.appkey = newKey;
     maphttpGroup_dic.Add(newKey, mpg);
 }
Пример #6
0
        /// <summary>
        /// 改变端口转发组的转发端口
        /// </summary>
        /// <param name="mpg"></param>
        /// <param name="newPort"></param>
        public void changePort(mapPortGroup mpg, string newPort)
        {
            if (mpg.mapType != 0)
            {
                return;
            }
            if (mpg.port == newPort)
            {
                return;
            }
            if (mpg.ownServerId != this.id || !mapPortGroup_dic.ContainsKey(mpg.port) || mpg.id != mapPortGroup_dic[mpg.port].id)
            {
                return;
            }


            AsyncHelpers.RunSync(() => mpg.inputChannel.CloseAsync());
            mapPortGroup_dic.Remove(mpg.port);
            mpg.port = newPort;
            mapPortGroup_dic.Add(newPort, mpg);
            mpg.inputChannel = AsyncHelpers.RunSync <IChannel>(() => startOnePortAsync(mpg.port));
        }
Пример #7
0
 /// <summary>
 /// 移除转发组
 /// </summary>
 /// <param name="mapKey"></param>
 /// <param name="mapType"></param>
 public void removeMapGroup(mapPortGroup mpg)
 {
     if (mpg.mapType == 0)
     {
         if (!mapPortGroup_dic.ContainsKey(mpg.port))
         {
             mapPortGroup_dic.Remove(mpg.port);
             AsyncHelpers.RunSync(() => mpg.inputChannel.CloseAsync());
             this.onChonage(new serverChangeEventArgs(this.id, serverChangeTypeEnum.serverParamsChanged, "mapPortGroup_dic", null, "移除组:" + mpg.port));
         }
         return;
     }
     if (mpg.mapType == 1)
     {
         if (!maphttpGroup_dic.ContainsKey(mpg.appkey))
         {
             maphttpGroup_dic.Remove(mpg.appkey);
             this.onChonage(new serverChangeEventArgs(this.id, serverChangeTypeEnum.serverParamsChanged, "maphttpGroup_dic", null, "移除组:" + mpg.appkey));
         }
         return;
     }
 }
Пример #8
0
        /// <summary>
        /// 添加转发组
        /// </summary>
        /// <param name="mgp">转发组对象</param>
        public void addMapGroup(mapPortGroup mgp)
        {
            if (mgp.mapType == 0)
            {
                if (!mapPortGroup_dic.ContainsKey(mgp.port))
                {
                    mapPortGroup_dic.Add(mgp.port, mgp);
                    mgp.inputChannel = AsyncHelpers.RunSync <IChannel>(() => startOnePortAsync(mgp.port));

                    this.onChonage(new serverChangeEventArgs(this.id, serverChangeTypeEnum.serverParamsChanged, "mapPortGroup_dic", null, "新增组:" + mgp.port));
                }
                return;
            }
            if (mgp.mapType == 1)
            {
                if (!maphttpGroup_dic.ContainsKey(mgp.appkey))
                {
                    maphttpGroup_dic.Add(mgp.appkey, mgp);
                    this.onChonage(new serverChangeEventArgs(this.id, serverChangeTypeEnum.serverParamsChanged, "maphttpGroup_dic", null, "新增组:" + mgp.appkey));
                }
                return;
            }
        }
Пример #9
0
        /// <summary>
        /// 监测主机是否存活
        /// </summary>
        /// <param name="omp"></param>
        /// <param name="mpg"></param>
        /// <returns></returns>
        private bool checkOneMapHostOk(outMapPort omp, mapPortGroup mpg)
        {
            if (mpg.mapType == 1)
            {
                //http转发,如果设置sayEchoUrl,就使用http请求测试,否则
                if (!string.IsNullOrEmpty(omp.sayEchoUrl))
                {
                    var url = string.Format("{0}://{1}:{2}/{3}", string.IsNullOrEmpty(omp.httpsPort) ? "http" : "https", omp.host, string.IsNullOrEmpty(omp.httpsPort) ? omp.port : omp.httpsPort, omp.sayEchoUrl);
                    return(canHaveEcho(url));
                }
            }
            try
            {
                Console.WriteLine("check tcp port");
                var bsp = new Bootstrap();
                bsp
                .Group(checkGroup)
                .Channel <TcpSocketChannel>()
                .Option(ChannelOption.TcpNodelay, true)
                .Option(ChannelOption.SoKeepalive, true)
                .Option(ChannelOption.ConnectTimeout, TimeSpan.FromSeconds(1))
                .Handler(new ActionChannelInitializer <ISocketChannel>(c =>
                {
                    IChannelPipeline pipeline = c.Pipeline;
                }));
                bsp.RemoteAddress(new IPEndPoint(IPAddress.Parse(omp.host), int.Parse(omp.port)));

                var clientChannel = AsyncHelpers.RunSync <IChannel>(() => bsp.ConnectAsync());
                AsyncHelpers.RunSync(() => clientChannel.CloseAsync());
                return(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine("check tcp port error:" + FrmLib.Extend.tools_static.getExceptionMessage(ex));
                return(false);
            }
        }
Пример #10
0
        /// <summary>
        ///
        /// </summary>
        public void loadPortProxyCfg()
        {
            string filename = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, localRunServer.portProxyFileName);

            if (!File.Exists(filename))
            {
                return;
            }
            XmlDocument tmpxmldoc = Static_xmltools.GetXmlDocument(filename);

            if (tmpxmldoc == null)
            {
                return;
            }
            XmlNodeList groupList = null;

            string xpathstr = "//application/group";

            groupList = Static_xmltools.SelectXmlNodes(tmpxmldoc, xpathstr);
            foreach (XmlNode obj in groupList)
            {
                //todo
                string appkey     = Static_xmltools.GetXmlAttr(obj, "Appkey");
                string listenport = Static_xmltools.GetXmlAttr(obj, "listenPort");
                string str        = Static_xmltools.GetXmlAttr(obj, "Policy");
                int    policy     = 0;
                int.TryParse(str, out policy);

                int mapType = 0;
                str = Static_xmltools.GetXmlAttr(obj, "mapType");
                int.TryParse(str, out mapType);
                if (mapType == 1)
                {
                    listenport = this.port;
                }
                string _httpsPort = Static_xmltools.GetXmlAttr(obj, "listenHttpsPort");

                outPortSelectPolicy selectpolicy = (outPortSelectPolicy)policy;
                str = Static_xmltools.GetXmlAttr(obj, "MaxWaitQueue");
                int maxQueueCount = -1;
                int.TryParse(str, out maxQueueCount);
                string host     = Static_xmltools.GetXmlAttr(obj, "Host");
                int    usehttps = 0;
                str = Static_xmltools.GetXmlAttr(obj, "useHttps");
                int.TryParse(str, out usehttps);
                mapPortGroup mpg = new mapPortGroup(host, listenport, appkey, maxQueueCount, selectpolicy
                                                    , _httpsPort, this, (listenHttpsEnum)(usehttps), mapType);
                xpathstr = ".//portMap";
                XmlNodeList maplist = Static_xmltools.SelectXmlNodes(obj, xpathstr);
                foreach (XmlNode onenode in maplist)
                {
                    string outHost       = Static_xmltools.GetXmlAttr(onenode, "host");
                    string outPort       = Static_xmltools.GetXmlAttr(onenode, "port");
                    string _outhttpsPort = Static_xmltools.GetXmlAttr(onenode, "httpsPort");
                    str = Static_xmltools.GetXmlAttr(onenode, "maxConnect");
                    int maxcount = int.Parse(str);
                    str = Static_xmltools.GetXmlAttr(onenode, "minConnect");
                    int    mincount  = int.Parse(str);
                    bool   needcheck = true;
                    string needcl    = Static_xmltools.GetXmlAttr(onenode, "needCheckLive");
                    bool.TryParse(needcl, out needcheck);

                    mpg.addOutPort(outHost, outPort, _outhttpsPort, maxcount, needcheck);
                }
                if (mapType == 0)
                {
                    if (mapPortGroup_dic.ContainsKey(listenport))
                    {
                        continue;
                    }


                    mapPortGroup_dic.Add(listenport, mpg);
                }
                else
                {
                    if (maphttpGroup_dic.ContainsKey(appkey))
                    {
                        continue;
                    }
                    maphttpGroup_dic.Add(appkey, mpg);
                }
            }
        }