Exemplo n.º 1
0
        protected override async Task <IResult> DoInvoke(IInvocation invocation)
        {
            RpcInvocation inv        = (RpcInvocation)invocation;
            var           methodName = RpcUtils.GetMethodName(invocation);

            inv.SetAttachment(Constants.PathKey, GetUrl().Path);
            inv.SetAttachment(Constants.VersionKey, _version);

            IExchangeClient currentClient;

            if (_clients.Length == 1)
            {
                currentClient = _clients[0];
            }
            else
            {
                currentClient = _clients[Interlocked.Increment(ref _index) % _clients.Length];
            }
            try
            {
                //Console.WriteLine("step5:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                var isAsync  = RpcUtils.IsAsync(GetUrl(), invocation);
                var isOneway = RpcUtils.IsOneway(GetUrl(), invocation);
                var timeout  = GetUrl().GetMethodParameter(methodName, Constants.TimeoutKey, Constants.DefaultTimeout.ToString());
                if (isOneway)
                {
                    var isSent = GetUrl().GetMethodParameter(methodName, Constants.SentKey, false);
                    await currentClient.SendAsync(inv, isSent);

                    return(new RpcResult());
                }
                else
                {
                    int.TryParse(timeout, out var time);
                    var future = await currentClient.Request(inv, time);

                    return(new RpcResult(future));
                }
            }
            catch (TimeoutException e)
            {
                throw new Exception("Invoke remote method timeout. method: " + invocation.MethodName + ", provider: " + GetUrl() + ", cause: " + e.Message, e);
            }
            catch (RemotingException e)
            {
                throw new Exception("Failed to invoke remote method: " + invocation.MethodName + ", provider: " + GetUrl() + ", cause: " + e.Message, e);
            }
        }
Exemplo n.º 2
0
            private IInvocation CreateInvocation(IChannel channel, URL url, string methodKey)
            {
                var method = url.GetParameter(methodKey, "");

                if (string.IsNullOrEmpty(method))
                {
                    return(null);
                }
                RpcInvocation invocation = new RpcInvocation();

                invocation.SetAttachment(Constants.PathKey, url.Path);
                invocation.SetAttachment(Constants.GroupKey, url.GetParameter(Constants.GroupKey, ""));
                invocation.SetAttachment(Constants.InterfaceKey, url.GetParameter(Constants.InterfaceKey, ""));
                invocation.SetAttachment(Constants.VersionKey, url.GetParameter(Constants.VersionKey, ""));
                if (url.GetParameter(Constants.StubEventKey, false))
                {
                    invocation.SetAttachment(Constants.StubEventKey, "true");
                }
                return(invocation);
            }
Exemplo n.º 3
0
        public async Task <IResult> Invoke(IInvocation inv)
        {
            if (_destroyed)
            {
                throw new Exception("Rpc invoker for service " + this + " on consumer " + NetUtils.GetLocalAddress()
                                    + " use dubbo version " + Common.Version.GetVersion()
                                    + " is DESTROYED, can not be invoked any more!");
            }
            RpcInvocation invocation = (RpcInvocation)inv;

            invocation.Invoker = this;
            if (_attachment != null && _attachment.Count > 0)
            {
                invocation.AddAttachmentsIfAbsent(_attachment);
            }
            //todo RpcContext
            //var context = RpcContext.getContext().getAttachments();
            //if (context != null)
            //{
            //    invocation.AddAttachmentsIfAbsent(context);
            //}
            if (GetUrl().GetMethodParameter(invocation.MethodName, Constants.AsyncKey, false))
            {
                invocation.SetAttachment(Constants.AsyncKey, "true");
            }
            RpcUtils.AttachInvocationIdIfAsync(GetUrl(), invocation);
            //Console.WriteLine("step4:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));

            try
            {
                return(await DoInvoke(invocation));
            }
            catch (Exception e)
            {
                return(new RpcResult(e));
            }
        }