Beispiel #1
0
        public static void InitRefreshThread(ITopClient client)
        {
            if (refreshThread == null)
            {
                lock (initLock)
                {
                    if (refreshThread == null)
                    {
                        try
                        {
                            DnsConfig remoteConfig = GetDnsConfigFromTop(client);
                            if (dnsConfig == null)
                            {
                                dnsConfig = remoteConfig;
                            }
                            else if (remoteConfig != null && remoteConfig.GetVersion() > dnsConfig.GetVersion())
                            {
                                dnsConfig = remoteConfig;
                            }
                        }
                        catch (TopException e)
                        {
                            if ("22".Equals(e.ErrorCode))
                            {
                                return; // 如果HTTP DNS服务不存在,则退出守护线程
                            }
                        }

                        refreshThread = new Thread(o =>
                        {
                            while (true)
                            {
                                try
                                {
                                    Thread.Sleep(dnsConfig.GetRefreshInterval() * 60 * 1000);
                                    dnsConfig = GetDnsConfigFromTop(client);
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine(e.StackTrace);
                                    Thread.Sleep(3 * 1000); // 出错则过3秒重试
                                }
                            }
                        });
                        refreshThread.IsBackground = true;
                        refreshThread.Name         = "HTTP_DNS_REFRESH_THREAD";
                        refreshThread.Start();
                    }
                }
            }
        }
Beispiel #2
0
        private static DnsConfig GetDnsConfigFromTop(ITopClient client)
        {
            HttpdnsGetRequest  req = new HttpdnsGetRequest();
            HttpdnsGetResponse rsp = client.Execute(req);

            if (!rsp.IsError)
            {
                return(DnsConfig.parse(rsp.Result));
            }
            else
            {
                throw new TopException(rsp.ErrCode, rsp.ErrMsg);
            }
        }
Beispiel #3
0
        public static DnsConfig parse(string json)
        {
            DnsConfig   dnsConfig = new DnsConfig();
            IDictionary root      = TopUtils.JsonToObject(json) as IDictionary;

            foreach (string configType in root.Keys)
            {
                if ("config".Equals(configType))
                {
                    dnsConfig.GlobalDict = new Dictionary <string, string>();
                    IDictionary globalInfo = root[configType] as IDictionary;
                    foreach (string key in globalInfo.Keys)
                    {
                        dnsConfig.GlobalDict[key] = globalInfo[key] as string;
                    }
                }
                else if ("env".Equals(configType))
                {
                    IDictionary envInfos = root[configType] as IDictionary;
                    dnsConfig.EnvDict = new Dictionary <string, List <EnvConfig> >();
                    foreach (string envName in envInfos.Keys)
                    {
                        IDictionary      envInfo    = envInfos[envName] as IDictionary;
                        List <EnvConfig> envConfigs = new List <EnvConfig>();

                        foreach (string domainName in envInfo.Keys)
                        {
                            IDictionary domainInfo = envInfo[domainName] as IDictionary;
                            EnvConfig   envConfig  = new EnvConfig();
                            envConfig.Domain   = domainName as string;
                            envConfig.Protocol = domainInfo["proto"] as string;
                            IList          vipInfos = domainInfo["vip"] as IList;
                            List <VipRule> vipRules = new List <VipRule>();
                            foreach (string vipInfo in vipInfos)
                            {
                                string[] vipInfoTmp = vipInfo.ToString().Split('|');
                                VipRule  vipRule    = new VipRule();
                                vipRule.Vip    = vipInfoTmp[0];
                                vipRule.Weight = double.Parse(vipInfoTmp[1]);
                                vipRules.Add(vipRule);
                            }
                            envConfig.VipRules = vipRules;
                            envConfigs.Add(envConfig);
                        }

                        dnsConfig.EnvDict[envName] = envConfigs;
                    }
                }
                else if ("api".Equals(configType))
                {
                    dnsConfig.ApiDict = new Dictionary <string, ApiConfig>();
                    IDictionary apiInfos = root[configType] as IDictionary;
                    foreach (string apiName in apiInfos.Keys)
                    {
                        IDictionary apiInfo   = apiInfos[apiName] as IDictionary;
                        ApiConfig   apiConfig = new ApiConfig();
                        apiConfig.User = apiInfo["user"] as string;
                        List <ApiRule> apiRules     = new List <ApiRule>();
                        IList          apiRuleInfos = apiInfo["rule"] as IList;
                        foreach (string apiRuleInfo in apiRuleInfos)
                        {
                            string[] apiRuleInfoTmp = apiRuleInfo.ToString().Split('|');
                            ApiRule  apiRule        = new ApiRule();
                            apiRule.Name   = apiRuleInfoTmp[0];
                            apiRule.Weight = double.Parse(apiRuleInfoTmp[1]);
                            apiRules.Add(apiRule);
                        }
                        apiConfig.Rules            = apiRules;
                        dnsConfig.ApiDict[apiName] = apiConfig;
                    }
                }
                else if ("user".Equals(configType))
                {
                    dnsConfig.UserDict = new Dictionary <string, IDictionary <string, string> >();
                    IDictionary userInfos = root[configType] as IDictionary;
                    foreach (string routeName in userInfos.Keys)
                    {
                        IDictionary envInfos = userInfos[routeName] as IDictionary;
                        IDictionary <string, string> tags = new Dictionary <string, string>();
                        foreach (string envName in envInfos.Keys)
                        {
                            IList tagInfos = envInfos[envName] as IList;
                            foreach (string tagName in tagInfos)
                            {
                                tags.Add(tagName, envName);
                            }
                        }
                        dnsConfig.UserDict[routeName] = tags;
                    }
                }
            }
            return(dnsConfig);
        }