dubbo接口的异步调用

由于公司使用的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;
实现两者的共存。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值