dubbo的rpc服务如果使用的是长连接的协议,如dubbo协议,tcp长连接本身是双向异步的,dubbo内部通过监听返回结果,来实现方法调用的同步,所以性能上是有牺牲的。
例子:
consumer.xmls
<beanid="demoCallback"class="com.alibaba.dubbo.callback.implicit.NofifyImpl"/>
< dubbo:reference id="demoService"interface="com.alibaba.dubbo.callback.implicit.IDemoService"version="1.0.0"group="cn">
< dubbo:methodname="get"async="true"onreturn="demoCallback.onreturn"onthrow="demoCallback.onthrow"/>
</ dubbo:reference>
class NofifyImpl implements Nofify {
public Map<Integer, Person> ret =newHashMap<Integer, Person>();
public Map<Integer, Throwable> errors =newHashMap<Integer, Throwable>();
public void onreturn(Person msg, Integer id) { //第一个参数为返回对象,第二个开始为入参
System.out.println("onreturn:"+ msg);
ret.put(id, msg);
}
public void onthrow(Throwable ex, Integer id) {//第一个参数为异常,第二个开始参数为入参,异常只有在onreturn的时候发生发生异常(返回异常,这里onreturn调用也是异常的),才调用,
errors.put(id, ex);
}
}
主要关联的几个类,FutureFilter, AbstractInvoker, DubboInvoker
最后欢迎大家访问我的个人网站:1024s