public async Task <RemoteInvokeResultMessage> InvokeAsync(RemoteInvokeContext context, CancellationToken cancellationToken) { var invokeMessage = context.InvokeMessage; AddressModel address = null; var vt = ResolverAddress(context, context.Item); address = vt.IsCompletedSuccessfully? vt.Result: await vt; try { var endPoint = address.CreateEndPoint(); if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug($"使用地址:'{endPoint}'进行调用。"); } var client = await _transportClientFactory.CreateClientAsync(endPoint); RpcContext.GetContext().SetAttachment("RemoteAddress", address.ToString()); return(await client.SendAsync(invokeMessage, cancellationToken).WithCancellation(cancellationToken)); } catch (CommunicationException) { await _healthCheckService.MarkFailure(address); throw; } catch (Exception exception) { _logger.LogError(exception, $"发起请求中发生了错误,服务Id:{invokeMessage.ServiceId}。"); throw; } }
public async Task <RemoteInvokeResultMessage> InvokeAsync(RemoteInvokeContext context, int requestTimeout) { var invokeMessage = context.InvokeMessage; AddressModel address = null; var vt = ResolverAddress(context, context.Item); address = vt.IsCompletedSuccessfully ? vt.Result : await vt; try { var endPoint = address.CreateEndPoint(); if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug($"使用地址:'{endPoint}'进行调用。"); } var client = _transportClientFactory.CreateClient(endPoint); using (var cts = new CancellationTokenSource()) { return(await client.SendAsync(invokeMessage, cts.Token).WithCancellation(cts, requestTimeout)); } } catch (CommunicationException) { await _healthCheckService.MarkFailure(address); throw; } catch (Exception exception) { _logger.LogError(exception, $"发起请求中发生了错误,服务Id:{invokeMessage.ServiceId}。错误信息:{exception.Message}"); throw; } }
public async Task <RemoteInvokeResultMessage> InvokeAsync(RemoteInvokeContext context, int requestTimeout) { var invokeMessage = context.InvokeMessage; AddressModel address = null; try { address = await ResolverAddress(context, context.Item); var endPoint = address.CreateEndPoint(); if (_logger.IsEnabled(LogLevel.Information)) { _logger.LogInformation($"使用地址:'{endPoint}'进行调用。"); } var client = await _transportClientFactory.CreateClientAsync(endPoint); RpcContext.GetContext().SetAttachment("RemoteAddress", address.ToString()); using (var cts = new CancellationTokenSource()) { var rpcResult = await client.SendAsync(invokeMessage, cts.Token).WithCancellation(cts, requestTimeout); return(rpcResult); } } catch (CommunicationException ex) { if (address != null) { _logger.LogError($"使用地址:'{address.ToString()}'调用服务{context.InvokeMessage.ServiceId}失败,原因:{ex.Message}"); await _healthCheckService.MarkFailure(address); } throw; } catch (TimeoutException ex) { if (address != null) { _logger.LogError($"使用地址:'{address.ToString()}'调用服务{context.InvokeMessage.ServiceId}超时,原因:{ex.Message}"); //await _healthCheckService.MarkTimeout(address, invokeMessage.ServiceId); } throw; } catch (Exception exception) { _logger.LogError(exception, $"发起请求中发生了错误,服务Id:{invokeMessage.ServiceId}。错误信息:{exception.Message}"); throw; } }
public async Task InvokeAsync(RemoteInvokeMessage message, int requestTimeout) { var host = NetUtils.GetHostAddress(); AddressModel address = host; var route = await _serviceRotueProvider.GetRouteByPath(_routePath); if (route != null && route.Address.Count() > 1) { try { var addresses = route.Address.ToList(); var index = addresses.IndexOf(host); for (var i = 1; i < AppConfig.Option.ClusterNode; i++) { ++index; if (addresses.Count == index) { index = 0; } address = addresses[index]; if (host == address) { break; } var endPoint = address.CreateEndPoint(); if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug($"使用地址:'{endPoint}'进行调用。"); } var client = await _transportClientFactory.CreateClientAsync(endPoint); using (var cts = new CancellationTokenSource()) { await client.SendAsync(message, cts.Token).WithCancellation(cts, requestTimeout); } } } catch (CommunicationException ex) { await _healthCheckService.MarkFailure(address); } catch (Exception exception) { _logger.LogError(exception, $"发起live请求中发生了错误,服务Id:{message.ServiceId}。"); } } }
private static bool Check(AddressModel address) { bool isHealth = false; using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { try { socket.Connect(address.CreateEndPoint()); isHealth = true; } catch { } return(isHealth); } }
private static async Task <bool> Check(AddressModel address, int timeout) { bool isHealth = false; using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) { SendTimeout = timeout }) { try { await socket.ConnectAsync(address.CreateEndPoint()); isHealth = true; } catch { } return(isHealth); } }
public MonitorEntry(AddressModel addressModel, bool health = true) { EndPoint = addressModel.CreateEndPoint(); Health = health; }