コード例 #1
0
        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;
            }
        }
コード例 #2
0
ファイル: RemoteInvokeService.cs プロジェクト: yxfcn/surging
        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;
            }
        }
コード例 #3
0
        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;
            }
        }
コード例 #4
0
        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}。");
                }
            }
        }
コード例 #5
0
        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);
            }
        }
コード例 #6
0
        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);
            }
        }
コード例 #7
0
 public MonitorEntry(AddressModel addressModel, bool health = true)
 {
     EndPoint = addressModel.CreateEndPoint();
     Health   = health;
 }