Exemplo n.º 1
0
        private string GetApiVipUrl(string serverUrl, ApiConfig apiConfig)
        {
            ApiRule apiRule = ClusterManager.GetElementByWeight(apiConfig.Rules);

            if (EnvDict.ContainsKey(apiRule.Name))
            {
                List <EnvConfig> envConfigs = EnvDict[apiRule.Name];
                return(GetEnvVipUrl(serverUrl, envConfigs));
            }
            else
            {
                List <EnvConfig> envConfigs = EnvDict[GlobalDict["def_env"]];
                return(GetEnvVipUrl(serverUrl, envConfigs));
            }
        }
Exemplo n.º 2
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);
        }