private static void Reg(string[] servers) { foreach (string app in servers) { GrantRpcClientManager.Register(app); } }
/// <summary> /// 前端代理处理程序,收和发 /// </summary> /// <param name="context">http上下文</param> /// <returns>结果,不会抛出异常</returns> public static Result <object> Send(HttpContext context) { // 构造一个统计日志 var mainLog = new LogStat() { BusinessType = "http.Proxy" }; mainLog.SetInfo(context); var content = string.Empty; try { content = GetRequestValue(context.Request); Args <object> a = JsonConvert.DeserializeObject <Args <object> >(content, jSetting); a.Headers = GetRequestIp(context); if (string.IsNullOrEmpty(a.rid)) { // 提前端产生一个rid a.rid = Guid.NewGuid().ToString("N"); } var rtn = GrantRpcClientManager.Send(a, context.Request.Path); using (var strStream = new StreamWriter(context.Response.Body)) { strStream.Write(rtn.c); strStream.Flush(); strStream.Close(); } mainLog.SetInfo(a, rtn.r); logger.LogInformation(new EventId(0, a.rid), mainLog.ToString()); return(rtn.r); } catch (Exception e) { Result <object> rr = new Result <object>() { c = 500, msg = e.Message, }; // 处理返回值中的rid,解析请求内容获取rid if (!string.IsNullOrEmpty(content)) { try { Args <object> a = JsonConvert.DeserializeObject <Args <object> >(content); if (!string.IsNullOrEmpty(a.rid)) { rr.rid = a.rid; } } catch { //反序列化请求内容异常 } } //如果未获取到原始请求rid,则重新生成 if (string.IsNullOrEmpty(rr.rid)) { rr.rid = Guid.NewGuid().ToString("N"); } var jSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; jSetting.DateFormatString = "yyyy-MM-dd HH:mm:ss"; var rst = JsonConvert.SerializeObject(rr, jSetting); using (var strStream = new StreamWriter(context.Response.Body)) { strStream.Write(rst); strStream.Flush(); strStream.Close(); } logger.LogError(new EventId(0, rr.rid), e, $"GrantHttpProxy.Send.Error"); mainLog.SetInfo(null, rr, e); logger.LogInformation(new EventId(0, rr.rid), mainLog.ToString()); return(rr); } }