Exemplo n.º 1
0
        public async Task<RemoteInvokeResultMessage> InvokeAsync(RemoteInvokeContext context, CancellationToken cancellationToken)
        {
            if (context == null)
                throw new ArgumentNullException(nameof(context));

            if (context.InvokeMessage == null)
                throw new ArgumentNullException(nameof(context.InvokeMessage));

            if (string.IsNullOrEmpty(context.InvokeMessage.ServiceId))
                throw new ArgumentException("服务Id不能为空。", nameof(context.InvokeMessage.ServiceId));

            var invokeMessage = context.InvokeMessage;
            var address = await _addressResolver.Resolver(invokeMessage.ServiceId);

            if (address == null)
                throw new RpcException($"无法解析服务Id:{invokeMessage.ServiceId}的地址信息。");

            try
            {
                var client = _transportClientFactory.CreateClient(address.CreateEndPoint());
                var transportMessage = await client.SendAsync(context.InvokeMessage);
                var resultMessage = client.ReceiveAsync(transportMessage.Id);
                return await resultMessage;
            }
            catch (Exception exception)
            {
                _logger.Fatal($"发起请求中发生了错误,服务Id:{invokeMessage.ServiceId}。", exception);
                throw;
            }
        }
Exemplo n.º 2
0
        private async ValueTask <AddressModel> ResolverAddress(RemoteInvokeContext context, int hashCode)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (context.InvokeMessage == null)
            {
                throw new ArgumentNullException(nameof(context.InvokeMessage));
            }

            if (string.IsNullOrEmpty(context.InvokeMessage.ServiceId))
            {
                throw new ArgumentException("服务Id不能为空。", nameof(context.InvokeMessage.ServiceId));
            }
            var invokeMessage = context.InvokeMessage;
            var address       = await _addressResolver.Resolver(invokeMessage.ServiceId, hashCode);

            if (address == null)
            {
                throw new CPlatformException($"无法解析服务Id:{invokeMessage.ServiceId}的地址信息。");
            }
            return(address);
        }
        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;
            }
        }
Exemplo n.º 4
0
        private async ValueTask <AddressModel> ResolverAddress(RemoteInvokeContext context, string item)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (context.InvokeMessage == null)
            {
                throw new ArgumentNullException(nameof(context.InvokeMessage));
            }

            if (string.IsNullOrEmpty(context.InvokeMessage.ServiceId))
            {
                throw new ArgumentException("服务Id不能为空。", nameof(context.InvokeMessage.ServiceId));
            }
            var invokeMessage = context.InvokeMessage;
            var vt            = _addressResolver.Resolver(invokeMessage.ServiceId, item);
            var address       = vt.IsCompletedSuccessfully ? vt.Result : await vt;

            if (address == null)
            {
                throw new CPlatformException($"无法解析服务Id:{invokeMessage.ServiceId}的地址信息。");
            }
            return(address);
        }
        public async Task InvokeAsync(RemoteInvokeContext context, CancellationToken cancellationToken)
        {
            var mqttContext = context as MqttRemoteInvokeContext;

            if (mqttContext != null)
            {
                var invokeMessage = context.InvokeMessage;
                var host          = NetUtils.GetHostAddress();
                var addresses     = await _mqttBrokerEntryManger.GetMqttBrokerAddress(mqttContext.topic);

                addresses = addresses.Except(new AddressModel[] { host });
                foreach (var address in addresses)
                {
                    try
                    {
                        var endPoint = address.CreateEndPoint();
                        if (_logger.IsEnabled(LogLevel.Debug))
                        {
                            _logger.LogDebug($"使用地址:'{endPoint}'进行调用。");
                        }
                        var client = _transportClientFactory.CreateClient(endPoint);
                        await client.SendAsync(invokeMessage).WithCancellation(cancellationToken);
                    }
                    catch (CommunicationException)
                    {
                        await _healthCheckService.MarkFailure(address);
                    }
                    catch (Exception exception)
                    {
                        _logger.LogError(exception, $"发起请求中发生了错误,服务Id:{invokeMessage.ServiceId}。");
                    }
                }
            }
        }
        public async Task <T> Invoke <T>(IDictionary <string, object> parameters, string serviceId)
        {
            var context = new RemoteInvokeContext()
            {
                ServiceId  = serviceId,
                Parameters = parameters
            };
            RemoteInvokeResultMessage message;

            try
            {
                message = await _remoteServiceInvoker.InvokeAsync(context);
            }
            catch (Exception e)
            {
                //todo:log
                throw;
            }

            if (message == null)
            {
                return(default(T));
            }
            var result = _typeConvertibleService.Convert(message.Result, typeof(T));

            return((T)result);
        }
Exemplo n.º 7
0
        public async Task <RemoteInvokeResultMessage> InvokeAsync(RemoteInvokeContext context, int requestTimeout)
        {
            var invokeMessage = context.InvokeMessage;
            var address       = await ResolverAddress(context, context.Item);

            try
            {
                var endPoint = address.CreateEndPoint();
                if (_logger.IsEnabled(LogLevel.Debug))
                {
                    _logger.LogDebug($"使用地址:'{endPoint}'进行调用。");
                }
                var client = _transportClientFactory.CreateClient(endPoint);
                return(await client.SendAsync(invokeMessage).WithCancellation(requestTimeout));
            }
            catch (CommunicationException)
            {
                await _healthCheckService.MarkFailure(address);

                throw;
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, $"发起请求中发生了错误,服务Id:{invokeMessage.ServiceId}。");
                throw;
            }
        }
Exemplo n.º 8
0
        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;
            }
        }
Exemplo n.º 9
0
        public async Task <RemoteInvokeResultMessage> InvokeAsync(RemoteInvokeContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (context.ServiceId == null)
            {
                throw new ArgumentNullException("serviceId");
            }

            RemoteInvokeMessage message = new RemoteInvokeMessage()
            {
                ServiceId  = context.ServiceId,
                Parameters = context.Parameters
            };
            var transportMessage = TransportMessage.CreateInvokeMessage(message);


            var retryPolicy = Policy.Handle <RpcConnectedException>()
                              .Or <RpcRemoteException>()
                              .WaitAndRetryAsync(5, attempt => TimeSpan.FromSeconds(Math.Pow(2, attempt))
                                                 , async(ex, time, i, ctx) =>
            {
                var address = ctx["address"] as AddressModel;
                if (_logger.IsEnabled(LogLevel.Debug))
                {
                    _logger.LogDebug($"第{i}次重试,重试时间间隔{time.Seconds},发生时间:{DateTime.Now},地址:{address.ToString()}");
                }
                await _healthCheckService.MarkFailure(address);
            });


            #region MyRegion
            //var fallBackPolicy = Policy<TransportMessage>.Handle<Exception>().Fallback(new TransportMessage());
            //var mixPolicy = Policy.Wrap(fallBackPolicy, retryPolicy);
            //var breakerPolicy=Policy.Handle<RpcConnectedException>()
            //                    .CircuitBreakerAsync(5,TimeSpan.FromSeconds(10),)
            #endregion

            try
            {
                var policyContext = new Context();
                return(await retryPolicy.ExecuteAsync <RemoteInvokeResultMessage>(ctx =>
                {
                    return RetryExectueAsync(ctx, _addressResolver, context.ServiceId, transportMessage);
                }, policyContext));
            }
            catch (Exception e)
            {
                //await _healthCheckService.MarkFailure(address);
                _logger.LogError("发送远程消息错误", e);
                throw e;
            }
        }
Exemplo n.º 10
0
        public async Task <RemoteInvokeResultMessage> InvokeAsync(RemoteInvokeContext context,
                                                                  CancellationToken cancellationToken)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (context.InvokeMessage == null)
            {
                throw new ArgumentNullException(nameof(context.InvokeMessage));
            }

            if (string.IsNullOrEmpty(context.InvokeMessage.ServiceId))
            {
                throw new ArgumentException("服务Id不能为空", nameof(context.InvokeMessage.ServiceId));
            }

            var invokeMessage = context.InvokeMessage;
            var address       = await _addressResolver.Resolver(invokeMessage.ServiceId);

            if (address == null)
            {
                throw new RpcException($"无法解析服务Id:{invokeMessage.ServiceId}的地址信息");
            }

            try
            {
                var endPoint = address.CreateEndPoint();

                if (_logger.IsEnabled(LogLevel.Debug))
                {
                    _logger.LogDebug($"使用地址:'{endPoint}'进行调用");
                }

                var client = _transportClientFactory.CreateClient(endPoint);
                return(await client.SendAsync(context.InvokeMessage));
            }
            catch (RpcCommunicationException)
            {
                await _healthCheckService.MarkFailure(address);

                throw;
            }
            catch (Exception exception)
            {
                _logger.LogError($"发起请求中发生了错误,服务Id:{invokeMessage.ServiceId}", exception);
                throw;
            }
        }
Exemplo n.º 11
0
        public async Task InvokeAsync(RemoteInvokeContext context, int requestTimeout)
        {
            var mqttContext = context as MqttRemoteInvokeContext;

            if (mqttContext != null)
            {
                var invokeMessage = context.InvokeMessage;
                var host          = NetUtils.GetHostAddress();
                var addresses     = await _mqttBrokerEntryManger.GetMqttBrokerAddress(mqttContext.topic);

                if (addresses != null)
                {
                    addresses = addresses.Except(new AddressModel[] { host });
                    foreach (var address in addresses)
                    {
                        try
                        {
                            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(invokeMessage, cts.Token).WithCancellation(cts, requestTimeout);
                            }
                        }
                        catch (CommunicationException)
                        {
                            await _healthCheckService.MarkFailure(address);
                        }
                        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, $"发起mqtt请求中发生了错误,服务Id:{invokeMessage.ServiceId}。");
                        }
                    }
                }
            }
        }
        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;
            }
        }
Exemplo n.º 13
0
        public async Task <RemoteInvokeResultMessage> InvokeAsync(RemoteInvokeContext context, CancellationToken cancellationToken)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (context.InvokeMessage == null)
            {
                throw new ArgumentNullException(nameof(context.InvokeMessage));
            }

            if (string.IsNullOrEmpty(context.InvokeMessage.ServiceId))
            {
                throw new ArgumentException("服务Id不能为空。", nameof(context.InvokeMessage.ServiceId));
            }

            var invokeMessage = context.InvokeMessage;
            var address       = await _addressResolver.Resolver(invokeMessage.ServiceId);

            if (address == null)
            {
                throw new RpcException($"无法解析服务Id:{invokeMessage.ServiceId}的地址信息。");
            }

            try
            {
                var client        = _transportClientFactory.CreateClient(address.CreateEndPoint());
                var message       = context.InvokeMessage;
                var resultMessage = client.ReceiveAsync(message.Id);
                await client.SendAsync(message);

                return(await resultMessage);
            }
            catch (Exception exception)
            {
                _logger.Fatal($"发起请求中发生了错误,服务Id:{invokeMessage.ServiceId}。", exception);
                throw;
            }
        }
Exemplo n.º 14
0
        public async Task<RemoteInvokeResultMessage> InvokeAsync(RemoteInvokeContext context, CancellationToken cancellationToken)
        {
            if (context == null)
                throw new ArgumentNullException(nameof(context));

            if (context.InvokeMessage == null)
                throw new ArgumentNullException(nameof(context.InvokeMessage));

            if (string.IsNullOrEmpty(context.InvokeMessage.ServiceId))
                throw new ArgumentException("服务Id不能为空。", nameof(context.InvokeMessage.ServiceId));

            var invokeMessage = context.InvokeMessage;
            var address = await _addressResolver.Resolver(invokeMessage.ServiceId);

            if (address == null)
                throw new RpcException($"无法解析服务Id:{invokeMessage.ServiceId}的地址信息。");

            try
            {
                var endPoint = address.CreateEndPoint();

                if (_logger.IsEnabled(LogLevel.Debug))
                    _logger.LogDebug($"使用地址:'{endPoint}'进行调用。");

                var client = _transportClientFactory.CreateClient(endPoint);
                return await client.SendAsync(context.InvokeMessage);
            }
            catch (RpcCommunicationException)
            {
                await _healthCheckService.MarkFailure(address);
                throw;
            }
            catch (Exception exception)
            {
                _logger.LogError($"发起请求中发生了错误,服务Id:{invokeMessage.ServiceId}。", exception);
                throw;
            }
        }
        public async Task <RemoteInvokeResultMessage> InvokeAsync(RemoteInvokeContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (context.ServiceId == null)
            {
                throw new ArgumentNullException("serviceId");
            }

            RemoteInvokeMessage message = new RemoteInvokeMessage()
            {
                ServiceId  = context.ServiceId,
                Parameters = context.Parameters
            };
            var transportMessage = TransportMessage.CreateInvokeMessage(message);

            //todo: 添加断路器(polly)

            var address = await _addressResolver.ResolverAsync(context.ServiceId);

            var client = _clientFactory.CreateClientAsync(address.CreateEndPoint());

            try
            {
                return(await client.SendAsync(transportMessage));
            }
            catch (Exception e)
            {
                await _healthCheckService.MarkFailure(address);

                _logger.LogError("发送远程消息错误", e);
                throw e;
            }
        }
Exemplo n.º 16
0
 public async Task <RemoteInvokeResultMessage> InvokeAsync(RemoteInvokeContext context)
 {
     return(await InvokeAsync(context, Task.Factory.CancellationToken));
 }
Exemplo n.º 17
0
 public Task <RemoteInvokeResultMessage> InvokeAsync(RemoteInvokeContext context, EndPoint endPoint, int timeout)
 {
     return(InvokeAsync(context, endPoint, timeout, Task.Factory.CancellationToken));
 }
 public async Task InvokeAsync(RemoteInvokeContext context)
 {
     await InvokeAsync(context, Task.Factory.CancellationToken);
 }
Exemplo n.º 19
0
 public Task<RemoteInvokeResultMessage> InvokeAsync(RemoteInvokeContext context)
 {
     return InvokeAsync(context, Task.Factory.CancellationToken);
 }