Home > Back-end > Dubbo asynchronous invocation solutions to problems
Dubbo asynchronous invocation solutions to problems
Time:09-17
Scenario description: client remote asynchronous call ServiceA, ServiceA in the process of handling the client requests to remote synchronization call ServiceB, ServiceA took the data from the response of ServiceB, get is null, the is the pits, Use the DEBUG mode, analysis of Dubbo source code for the cause of the problem, The analysis process is as follows: Client and server communication, netty configuration used for network transmission, through the NettyHandler specific messaging operations, so from now on start with source code analysis, Client to A remote method ServiceA asynchronous invocation, the RpcContext (RpcContext recorder is A temporary state, when receiving the RPC requests, or launch the RPC request, RpcContext state will change, such as: A, B, B and C, B machine, before the C B, RpcContext record information is A callback B, after the C B, RpcContext record is the information of the C B) the attachments (Map) structure attribute to add async=true of key-value pairs, as well as in RpcInvocation attachments (Map) add async=true key/value pair, After a series of the Filter, the program runs to AbstractInvoker invoke method, pay attention to the method in the following code segment, Map The context=RpcContext. GetContext (). GetAttachments (); If (the context.=null) { Invocation. AddAttachmentsIfAbsent (context); } Here will add attachments in the current RpcContext to call ServiceB RpcInvocation, at this time the async=true have been added, and then execute the following code snippet, If (getUrl (). GetMethodParameter (invocation. GetMethodName (), the ASYNC_KEY, false)) { Invocation. SetAttachment (Constants. ASYNC_KEY, Boolean. TRUE. The toString ()); } The above code to determine whether a call ServiceB URL contains async=true, if you have to set the async=true to RpcInvocation attachments, at that time is not contain, Continue to tracking code, run into the DubboInvoker, call doInvoke method, this method has the following code snippet, Boolean isAsync=RpcUtils. IsAsync (getUrl (), invocation), the isAsync method concrete statement as follows, Public static Boolean isAsync (URL URL, Invocation inv) { Boolean isAsync; //if the Java code set priority. If (Boolean. TRUE. The toString () equals (inv. GetAttachment (the ASYNC_KEY))) { IsAsync=true; } else { IsAsync=url. GetMethodParameter (getMethodName (inv), the ASYNC_KEY, false); } Return isAsync; } In the attachments above method first determines RpcInvocation async=true whether, if found, this is an asynchronous invocation, or whether async in the request URL=true, if the establishment is an asynchronous invocation, or is a synchronous call, according to the above parameters passed, now isAsync method returns true, ServiceA synchronous invocation ServiceB became an asynchronous call, continue to look down at the asynchronous call, the code is as follows, Else if (isAsync) { ResponseFuture future=currentClient. Request (inv, timeout); RpcContext. GetContext (). SetFuture (new FutureAdapter