由于公司使用的dubbo版本低于2.7.0。
且服务提供方不提供相应异步改造,所以作为服务消费方只能使用RpcContext的方式进行异步调用。
主要是在spring-dubbo-consumer.xml
文件中进行配置
<dubbo:reference id="xxService" interface="com.xx.xx.api.xxService">
<dubbo:method name="xxMethod" async="true" timeout="1000"/>
</dubbo:reference>
xxService.xxMethod();
Future future = RpcContext.getContext().getFuture();
future.get();
但是此种方法会让所有xxMethod的调用都变成异步。
最终使用定义两个bean的方式实现
<dubbo:reference id="xxServiceAsync" interface="com.xx.xx.api.xxService">
<dubbo:method name="xxMethod" async="true" timeout="1000"/>
</dubbo:reference>
<dubbo:reference id="xxService" interface="com.xx.xx.api.xxService"/>
并在注入的时候用name进行区别
当要异步时
@Resource(name = “xxServiceAsync”)
private xxService xxServiceAsync;
同步时
@Resource(name = “xxService”)
private xxService xxService;
实现两者的共存。