/// <summary> /// 远程调用 /// </summary> /// <returns></returns> private string CallHttp() { // 当前请求调用的模型对应的主机名称 string httpHost; // 当前请求调用的Api名称 var httpApi = Data.RouteHost == HostConfig.DefaultHost ? Data.Uri.PathAndQuery : $"{Data.ApiName}{Data.Uri.Query}"; // 查找主机 if (Data.RouteHost.Hosts.Length == 1) { httpHost = Data.RouteHost.Hosts[0]; } else { lock (Data.RouteHost) { //平均分配 httpHost = Data.RouteHost.Hosts[Data.RouteHost.Next]; if (++Data.RouteHost.Next >= Data.RouteHost.Hosts.Length) { Data.RouteHost.Next = 0; } } } // 远程调用 var caller = new HttpApiCaller(httpHost) { Bearer = $"Bearer {ApiContext.RequestContext.Bear}" }; var req = caller.CreateRequest(httpApi, Data.HttpMethod, Request, Data); LogRecorder.BeginStepMonitor("内部HTTP调用"); LogRecorder.MonitorTrace($"Url:{req.RequestUri.PathAndQuery}"); LogRecorder.MonitorTrace($"Auth:{caller.Bearer}"); try { // 远程调用状态 Data.ResultMessage = caller.GetResult(req, out var webStatus); LogRecorder.MonitorTrace(webStatus.ToString()); if (webStatus != WebExceptionStatus.Success) { Data.Status = RouteStatus.RemoteError; } } catch (Exception ex) { LogRecorder.Exception(ex); LogRecorder.MonitorTrace($"发生异常:{ex.Message}"); Data.ResultMessage = RouteRuntime.NetworkError; Data.Status = RouteStatus.RemoteError; } finally { LogRecorder.MonitorTrace(Data.ResultMessage); LogRecorder.EndStepMonitor(); } return(Data.ResultMessage); }
/// <summary> /// 远程调用 /// </summary> /// <returns></returns> private async Task <string> CallHttp() { var host = Data.RouteHost as HttpHost; if (host == null) { LogRecorder.MonitorTrace("Host Type Failed"); return(Data.ResultMessage); } // 当前请求调用的模型对应的主机名称 string httpHost; // 当前请求调用的Api名称 var httpApi = host == HttpHost.DefaultHost ? Data.Uri.PathAndQuery : $"{Data.ApiName}{Data.Uri.Query}"; // 查找主机 if (host.Hosts.Length == 1) { httpHost = host.Hosts[0]; } else { lock (host) { //平均分配 httpHost = host.Hosts[host.Next]; if (++host.Next >= host.Hosts.Length) { host.Next = 0; } } } // 远程调用 using (MonitorScope.CreateScope("CallHttp")) { var caller = new HttpApiCaller(httpHost) { Bearer = $"Bearer {ApiContext.RequestContext.Bear}" }; caller.CreateRequest(httpApi, Data.HttpMethod, Request, Data); Data.ResultMessage = await caller.Call(); Data.Status = caller.Status; LogRecorder.MonitorTrace(caller.Status.ToString()); LogRecorder.MonitorTrace(Data.ResultMessage); } return(Data.ResultMessage); }