Exemplo n.º 1
0
        /// <summary>
        /// 刷新对应模块的Url
        /// </summary>
        /// <param name="app"></param>
        /// <returns></returns>
        public static bool RefreshEurekaUrls(String app)
        {
            try
            {
                List <EurekaUrlAction> urlsOfApp = new List <EurekaUrlAction>();
                foreach (String serverUrl in _EurekaServers) //遍历EUREKA服务的APP内容
                {
                    String eurekaServerInfo = HttpDataHelper.GetString(serverUrl);

                    XmlDocument d = new XmlDocument();
                    d.LoadXml(eurekaServerInfo);

                    XmlNode     top          = d.SelectNodes("//applications")[0];
                    XmlNodeList applications = top.SelectNodes("//application");
                    foreach (XmlElement element in applications)
                    {
                        string name = element.GetElementsByTagName("name")[0].InnerText;
                        if (name.Equals(app))   //找到对应的application
                        {
                            XmlNodeList instances = element.GetElementsByTagName("instance");

                            foreach (XmlElement ele in instances)
                            {
                                string ipAddress = ele.GetElementsByTagName("ipAddr")[0].InnerText;
                                string port      = ele.GetElementsByTagName("port")[0].InnerText;
                                string newUrl    = "http://" + ipAddress + ":" + port;

                                EurekaUrlAction ua = new EurekaUrlAction();
                                ua.Url = newUrl;

                                if (!Contains(urlsOfApp, newUrl))
                                {
                                    EurekaUrlAction eua = new EurekaUrlAction();
                                    eua.Url      = newUrl;
                                    eua.IsActive = true;
                                    eua.Count    = 0;
                                    urlsOfApp.Add(eua);
                                }
                            }
                        }
                    }
                }
                lock (_EurekaUrls)
                {
                    _EurekaUrls.Remove(app);
                    _EurekaUrls.Add(app, urlsOfApp);
                }
                return(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(false);
            }
        }
Exemplo n.º 2
0
        private static T CommWithStatus <T, U>(MyComWithStatus <T, U> func, String app, String relativeUrl, out String statusCode, U parms)
        {
            List <EurekaUrlAction> urls = new List <EurekaUrlAction>();

            if (!_EurekaUrls.TryGetValue(app, out urls))
            {
                RefreshEurekaUrls(app);
                urls = _EurekaUrls[app];
            }

            bool   success      = false;
            String errorMessage = String.Empty;
            T      result       = default(T);
            String status       = String.Empty;

            int count = urls.Count;

            for (int i = 0; i < count; i++)
            {
                EurekaUrlAction eua = urls[i];
                try
                {
                    if ((eua.IsActive && eua.Count < invalidCount) || (!eua.IsActive && eua.Count == revokeCount))
                    {
                        //  获取数据
                        result = func(eua.Url + relativeUrl, out status, parms);

                        success = true;
                        lock (urls) // 获取数据成功,将已使用的地址轮换至最后以平衡服务器
                        {
                            eua.IsActive = true;
                            eua.Count    = 0;
                            urls.Remove(eua);
                            urls.Add(eua);
                        }
                    }
                    else
                    {
                        lock (urls)
                        {
                            if (eua.IsActive)
                            {
                                //该地址失效次数过多,被熔断
                                eua.IsActive = false;
                                eua.Count    = 0;
                            }
                            else
                            {
                                //次数+1
                                eua.Count++;
                            }
                        }
                    }
                }
                catch (BizCustomException bex)
                {
                    lock (urls) // 出现业务异常,跳出,将已使用的地址轮换至最后以平衡服务器
                    {
                        eua.IsActive = true;
                        eua.Count    = 0;
                        urls.Remove(eua);
                        urls.Add(eua);
                    }
                    throw bex;
                }
                catch (Exception ex)
                {
                    errorMessage += eua.Url + relativeUrl + " --- " + ex.Message + "\r\n";
                    lock (urls)
                    {
                        if (eua.IsActive)
                        {
                            eua.Count++;
                        }
                        else
                        {
                            eua.Count = 0;
                        }
                    }
                }
                if (success)
                {
                    break;
                }
            }

            if (success)
            {
                statusCode = status;
                return(result);
            }
            else
            {
                throw new Exception(errorMessage);
            }
        }