public override void ChannelInactive(IChannelHandlerContext context) { var remoteServerEndpoint = context.Channel.RemoteAddress as IPEndPoint; if (remoteServerEndpoint != null) { _healthCheck.RemoveAddress(remoteServerEndpoint.Address.MapToIPv4(), remoteServerEndpoint.Port); } }
public async Task <RemoteResultMessage> Invoke(RemoteInvokeMessage remoteInvokeMessage, GovernanceOptions governanceOptions, string hashKey = null) { var serviceRoute = _serviceRouteCache.GetServiceRoute(remoteInvokeMessage.ServiceId); if (serviceRoute == null) { throw new LmsException($"通过{remoteInvokeMessage.ServiceId}找不到服务路由", StatusCode.NotFindServiceRoute); } if (!serviceRoute.Addresses.Any(p => p.Enabled)) { throw new NotFindServiceRouteAddressException($"通过{remoteInvokeMessage.ServiceId}找不到可用的服务提供者"); } var addressSelector = EngineContext.Current.ResolveNamed <IAddressSelector>(governanceOptions.ShuntStrategy.ToString()); var selectedAddress = addressSelector.Select(new AddressSelectContext(remoteInvokeMessage.ServiceId, serviceRoute.Addresses, hashKey)); bool isInvakeSuccess = true; var sp = Stopwatch.StartNew(); try { _remoteServiceSupervisor.Monitor((remoteInvokeMessage.ServiceId, selectedAddress), governanceOptions); var client = await _transportClientFactory.GetClient(selectedAddress); //RpcContext.GetContext().SetAttachment("localAddress", NetUtil.GetRpcAddressModel().ToString()); RpcContext.GetContext().SetAttachment("remoteAddress", selectedAddress.ToString()); return(await client.SendAsync(remoteInvokeMessage, governanceOptions.ExecutionTimeout)); } catch (IOException ex) { Logger.LogError($"服务提供者{selectedAddress}不可用,IO异常,原因:{ex.Message}"); _healthCheck.RemoveAddress(selectedAddress); isInvakeSuccess = false; throw new CommunicatonException(ex.Message, ex.InnerException); } catch (ConnectException ex) { Logger.LogError($"与服务提供者{selectedAddress}链接异常,原因:{ex.Message}"); MarkAddressFail(governanceOptions, selectedAddress); isInvakeSuccess = false; throw new CommunicatonException(ex.Message, ex.InnerException); } catch (ChannelException ex) { Logger.LogError($"与服务提供者{selectedAddress}通信异常,原因:{ex.Message}"); MarkAddressFail(governanceOptions, selectedAddress); isInvakeSuccess = false; throw new CommunicatonException(ex.Message, ex.InnerException); } catch (TimeoutException ex) { Logger.LogError($"与服务提供者{selectedAddress}执行超时,原因:{ex.Message}"); MarkAddressFail(governanceOptions, selectedAddress, true); isInvakeSuccess = false; throw; } finally { sp.Stop(); if (isInvakeSuccess) { _remoteServiceSupervisor.ExecSuccess((remoteInvokeMessage.ServiceId, selectedAddress), sp.Elapsed.TotalMilliseconds); } else { _remoteServiceSupervisor.ExceFail((remoteInvokeMessage.ServiceId, selectedAddress), sp.Elapsed.TotalMilliseconds); } } }