/// <summary> /// EtermServer调度,自由调度算法 /// </summary> /// <param name="appid">应用程序ID</param> /// <param name="url">指令名</param> /// <param name="cmd">请求POST数据或脚本</param> /// <param name="TimeOut">请求超时时间</param> /// <returns></returns> public string Invoke(int appid, string url, string cmd, TimeSpan TimeOut) { string.Format(JMetricsHelper.JetermCount, "Common").MeterMark("次");//计数 return(string.Format(JMetricsHelper.JetermExecTime, "Common").HistogramUpdate(() => { string sret = string.Empty; string server = string.Empty; string dtStr = string.Empty; string finalUrl = string.Empty; try { int ntimeout = (int)TimeOut.TotalMilliseconds; DateTime dt = DateTime.Now; dtStr = dt.ToString("yyyy-MM-dd HH:mm:ss ffff"); List <Config> list = RedisHelper.tGet <List <Config> >("140106_140110_EtermUrl"); if (list != null) { list.RemoveAll(l => l == null); } if (list == null || list.Count == 0) { list = new JetermClient.DAL.EtermConfig().GetConfigs(); if (list != null && list.Count > 0) { RedisHelper.tSet("140106_140110_EtermUrl", list, TimeSpan.FromDays(30)); } } if (list == null || list.Count == 0) { return string.Empty; } string method = GetParam(ref url, "method", true); string officeno = GetParam(ref url, "officeno", true); IEnumerable <Config> configs = list.ToArray(); if (!string.IsNullOrEmpty(method)) { configs = from l in configs where l.cmdType.Contains((EtermCommand.CmdType)Enum.Parse(typeof(EtermCommand.CmdType), method)) select l; } if (!string.IsNullOrEmpty(officeno)) { configs = from l in configs where l.OfficeNo == officeno select l; } if (configs == null || configs.Count() == 0) { return string.Empty; } server = ((Config)configs.ElementAt(new Random(DateTime.Now.GetHashCode()).Next(0, configs.Count()))).ServerUrl; finalUrl = string.Format("http://{0}/{1}", server, url); sret = HttpService.HttpPost(finalUrl, cmd, ntimeout, dtStr); log.Info(string.Format("JetermClient.Common请求:{0}请求方式:InvokeEtermServer{0}请求时间:{1}{0}请求url:{0}{2}{0}应用程序ID[{3}]{0}请求数据:{0}{4}{0}返回:{0}{5}。", Environment.NewLine, dtStr, finalUrl, appid, cmd, (string.IsNullOrWhiteSpace(sret) ? "返回为空" : sret))); } catch (Exception ex) { string.Format(JMetricsHelper.JetermErrCount, "Common").MeterMark("次");//失败计数 string innerErrorMessage = string.Format("JetermClient.Common请求抛异常:{0}请求方式:InvokeEtermServer{0}请求时间:{1}{0}请求url:{0}{2}{0}应用程序ID[{3}]{0}请求数据:{0}{4}{0}返回:{0}{5}{0}异常信息为:{0}【{0}{6}{0}】。", Environment.NewLine, dtStr, finalUrl, appid, cmd, (string.IsNullOrWhiteSpace(sret) ? "返回为空" : sret), ex.ToString()); log.Error(innerErrorMessage); throw new Exception(innerErrorMessage); } return sret; })); }
/// <summary> /// 获取服务器地址 /// </summary> /// <param name="request"></param> /// <returns></returns> private string ServerUrl <T>(Command <T> request) where T : new() { List <Config> list = RedisHelper.tGet <List <Config> >("140106_140110_EtermUrl"); if (list != null) { list.RemoveAll(l => l == null); } if (list == null || list.Count == 0) { list = new JetermClient.DAL.EtermConfig().GetConfigs(); if (list != null && list.Count > 0) { RedisHelper.tSet("140106_140110_EtermUrl", list, TimeSpan.FromDays(30)); } } if (list == null || list.Count == 0) { return(string.Empty); } string method = request.request.GetType().Name; IEnumerable <Config> configs = from l in list where l.cmdType.Contains((EtermCommand.CmdType)Enum.Parse(typeof(EtermCommand.CmdType), method)) select l; if (!string.IsNullOrEmpty(request.officeNo)) { configs = from l in configs where l.OfficeNo == request.officeNo select l; } if (!string.IsNullOrEmpty(request.ConfigName)) { configs = from l in configs where l.ConfigList.Contains(request.ConfigName) select l; } if (method.Equals(EtermCommand.CmdType.Booking.ToString())) { if (request.request != null) { JetermEntity.Request.Booking booking = (request.request as JetermEntity.Request.Booking); if (booking != null && booking.FlightList.Count > 0) { string FlightNo = booking.FlightList.FirstOrDefault().FlightNo; if (!string.IsNullOrEmpty(FlightNo) && FlightNo.Length > 2) { configs = from l in configs where l.AllowAirLine.Contains(FlightNo.Substring(0, 2)) select l; } } } } else if (method.Equals(EtermCommand.CmdType.AV.ToString())) { if (request.request != null) { JetermEntity.Request.AV av = (request.request as JetermEntity.Request.AV); if (av != null) { if (!string.IsNullOrEmpty(av.FlightNo) && av.FlightNo.Length > 2) { configs = from l in configs where l.AllowAirLine.Contains(av.FlightNo.Substring(0, 2)) select l; } } } } else if (method.Equals(EtermCommand.CmdType.AVH.ToString())) { if (request.request != null) { JetermEntity.Request.AVH avh = (request.request as JetermEntity.Request.AVH); if (avh != null && !string.IsNullOrWhiteSpace(avh.Airline)) { configs = from l in configs where l.AllowAirLine.Contains(avh.Airline) select l; } } } if (configs == null || configs.Count() == 0) { return(string.Empty); } return(((Config)configs.ElementAt(new Random(DateTime.Now.GetHashCode()).Next(0, configs.Count()))).ServerUrl); }