public ServiceEntry Locate(RemoteInvokeMessage invokeMessage)
        {
            var serviceEntries = _serviceEntryManager.GetEntries();

            return(serviceEntries.SingleOrDefault(i => i.Descriptor.Id == invokeMessage.ServiceId));
        }
Exemple #2
0
 public MessagePackRemoteInvokeMessage(RemoteInvokeMessage message)
 {
     ServiceId  = message.ServiceId;
     Parameters = message.Parameters?.Select(i => new ParameterItem(i)).ToArray();
 }
Exemple #3
0
 public MessagePackRemoteInvokeMessage(RemoteInvokeMessage remoteInvokeMessage)
 {
     ServiceId   = remoteInvokeMessage.ServiceId;
     Parameters  = remoteInvokeMessage.Parameters;
     Attachments = remoteInvokeMessage.Attachments;
 }
Exemple #4
0
        private async Task <RemoteInvokeResultMessage> MonitorRemoteInvokeAsync(IDictionary <string, object> parameters, string serviceId, string serviceKey, bool decodeJOject, int requestTimeout, string item)
        {
            CancellationTokenSource source = new CancellationTokenSource();
            var token         = source.Token;
            var invokeMessage = new RemoteInvokeMessage
            {
                Parameters   = parameters,
                ServiceId    = serviceId,
                ServiceKey   = serviceKey,
                DecodeJOject = decodeJOject,
                Attachments  = RpcContext.GetContext().GetContextParameters()
            };

            try
            {
                _serviceInvokeListenInfo.AddOrUpdate(serviceId, new ServiceInvokeListenInfo(), (k, v) =>
                {
                    v.RemoteServiceRequests = v.RemoteServiceRequests == null ? 1 : ++v.RemoteServiceRequests;
                    v.FinalRemoteInvokeTime = DateTime.Now;
                    ++v.ConcurrentRequests;
                    return(v);
                });
                var message = await _remoteInvokeService.InvokeAsync(new RemoteInvokeContext
                {
                    Item          = item,
                    InvokeMessage = invokeMessage
                }, requestTimeout);

                _serviceInvokeListenInfo.AddOrUpdate(serviceId, new ServiceInvokeListenInfo(), (k, v) =>
                {
                    v.SinceFaultRemoteServiceRequests = 0;
                    --v.ConcurrentRequests; return(v);
                });
                return(message);
            }
            catch (Exception ex)
            {
                await ExecuteExceptionFilter(ex, invokeMessage, token);

                if ((ex.InnerException != null && ex.InnerException is BusinessException) || ex is BusinessException)
                {
                    _serviceInvokeListenInfo.AddOrUpdate(serviceId, new ServiceInvokeListenInfo(), (k, v) =>
                    {
                        v.SinceFaultRemoteServiceRequests = 0;
                        --v.ConcurrentRequests;
                        return(v);
                    });
                    return(new RemoteInvokeResultMessage()
                    {
                        ExceptionMessage = ex.InnerException.GetExceptionMessage(),
                        Result = null,
                        StatusCode = ex.InnerException.GetExceptionStatusCode()
                    });
                }
                else
                {
                    _serviceInvokeListenInfo.AddOrUpdate(serviceId, new ServiceInvokeListenInfo(), (k, v) =>
                    {
                        ++v.FaultRemoteServiceRequests;
                        ++v.SinceFaultRemoteServiceRequests;
                        --v.ConcurrentRequests;
                        return(v);
                    });
                }

                return(new RemoteInvokeResultMessage()
                {
                    ExceptionMessage = ex.GetExceptionMessage(),
                    Result = null,
                    StatusCode = ex.GetExceptionStatusCode()
                });
            }
        }
        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);
                }
            }
        }