dubbo异步同步调用混合使用问题

来源:互联网 发布:mac os x镜像下载iso 编辑:程序博客网 时间:2024/05/22 09:41

个人笔记。

先看下面的case



我们假设

Assume1: ServiceB返回字符串”ServiceB”,

Assume2: ServiceC返回字符串”ServiceC”,

ServiceA 拼接字符串”ServiceA”,加上assume1 和 assume2 的返回结果

预期:client获得“ServiceA ServiceB ServiceC”

实际:client获得“ServiceA ServiceC”

没有获得ServiceB的返回结果, 从log看的话,在ServiceA里面获取的step3的结果也的确是null。



分析:

Client 和 server进行交互的方式,默认是netty,通过NettyHandler进行操作,有消息的收发处理方法。

进行debug,

client到ServiceA之后,在RpcInvocation里有一个async=true,同时RpcContext.getContext().getAttachments()也包含这个属性。

然后进行step2,进行一系列filter,最后到AbstractInvoker, #134开始,

Map<String, String> context = RpcContext.getContext().getAttachments();        if (context != null) {        invocation.addAttachmentsIfAbsent(context);        }

//这里吧当前context里面的attachments付给了调用ServiceBRpcInvocation,所以这时变成async=true

if (getUrl().getMethodParameter(invocation.getMethodName(), Constants.ASYNC_KEY, false)){    invocation.setAttachment(Constants.ASYNC_KEY, Boolean.TRUE.toString());}

//这里判断后,由于调用ServiceB的url里面是没有async的属性的,所以返回false

继续向后,进入到DubboProtocol的reply,可以获取到DubboInvoker, DubboInvoker.doInvoke方法,先判断一下是否为异步

boolean isAsync = RpcUtils.isAsync(getUrl(), invocation);

由于上面的方法导致此时为true(本来的同步调用变成了异步调用)

else if (isAsync) {            ResponseFuture future = currentClient.request(inv, timeout) ;                RpcContext.getContext().setFuture(new FutureAdapter<Object>(future));                return new RpcResult();            }

进入这个逻辑后,设置了future,然后返回了空的RpcResult,data和exception都为null。

所以在step3获得的结果为null。


执行完后,继续,有一些清理操作,在ConsumerContextFilter里面会清掉attachements

然后在step4,就是正常的同步调用,可以获取结果。


修改:

简单的进行了一下修改,AbstractInvoker的#137处,每次都对async进行实际赋值

boolean isAsync = getUrl().getMethodParameter(invocation.getMethodName(), Constants.ASYNC_KEY, false);invocation.setAttachment(Constants.ASYNC_KEY, String.valueOf(isAsync));





2 0