Dubbo--异步

Dubbo支持异步调用的方式,摘自官网流程图在这里插入图片描述
服务提供方:

@Service
@com.alibaba.dubbo.config.annotation.Service(version = "1.0.0",timeout = 10000)
public class AsyncServiceImpl implements AsyncService {

    private static final Logger logger = LoggerFactory.getLogger(AsyncServiceImpl.class);

    @Override
    public String sayHello01() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "1";
    }
 }

和正常请求服务没有区别,有区别的是消费端,引用服务的时候,设置async的值为true即可。

@Reference(version = "1.0.0",async = true)
    private AsyncService asyncService;

    @GetMapping("/getAsyncResult")
    public Object getAsyncResult() throws ExecutionException, InterruptedException {
        String result01 = asyncService.sayHello01();
        logger.info("result01:"+result01);
        // 拿到调用的Future引用,当结果返回后,会被通知和设置到此Future
        Future<String> future = RpcContext.getContext().getFuture();
        String result02=future.get();
        return result02;
    }

需要的是接口的返回值永远为null,只有通过Future中获取到的值才是服务返回的值,注解的方式将导致接口下所有的服务都是异步的;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值