Dubbo的异步调用


在微服务环境中,往往一个接口,是经过多个服务间的接口调用,最后封装成一个接口中返回。如果每个等待每个接口串并执行结果,会比较耗时,此时我们就需要异步处理。

dubbo异步调用

dubbo的异步调用,基于NIO的非阻塞实现并行调用,客户端不需要启动多线程即可完成并行调用多个远程服务,相对多线程开销较小。
在这里插入图片描述
在userThread用户线程请求网关服务,调用某个方法,执行ioThread,再去调用服务端server,在服务端返回数据之前,将Future对象,复制到RpcContext中,在通过getFuture获取Future对象。
在服务端返回数据后,通知Future对象,通过Future的get方法,获取返回结果。

2.6版本中dubbo异步调用的实现

在客户端的consumer.xml 中配置如下:

<dubbo:reference id="fooService" interface="com.alibaba.dubbo.samples.async.api.FooService">
		<dubbo:method name="findFoo" async="true"/>
</dubbo:reference>

<dubbo:reference id="barService" interface="com.alibaba.dubbo.samples.async.api.BarService">
		<dubbo:method name="findBar" async="true"/>
</dubbo:reference>

调用代码。

// 此调用会立即返回null
fooService.findFoo(fooId)
// 拿到调用的Future引用, 当结果返回后,会被通知和设置到此Future。
Future<Foo> fooFuture = RpcContext.getContext().getFuture();

// 此调用会立即返回null
barService.findBar(barId)
// 拿到调用的Future引用, 当结果返回后,会被通知和设置到此Future。
Future<Bar> fooFuture = RpcContext.getContext().getFuture();

// 此时findFoo 和 findBar 的请求同时在执行,客户端不需要启动多线程来支持并行,而是借助NIO的非阻塞完成。

// 如果foo 已返回,直接拿到返回值,否则当前线程wait住, 等待foo返回后,线程会被notify唤醒。
Foo foo = fooFuture.get().
// 同理等待bar返回。
Bar bar = barFuture.get();
// 如果foo需要5秒返回,bar需要6秒返回,实际只需要6秒,即可获取到foo和bar,进行接下来的处理。

一些特殊场景下,客户端为了尽快调用返回值,可以设置是否等待消息发出

  • sent=“true” 等待消息发出,消息发送失败将抛出异常;
  • sent=“false” 不等待消息发出,将消息放入 IO 队列,即刻返回。
    默认为fase。配置方式如下:
<dubbo:method name="goodbye" async="true" sent="true" />

如果你只是想异步,完全忽略返回值,可以配置 return="false",以减少 Future 对象的创建和管理成本:

<dubbo:method name="goodbye" async="true" return="false"/>

此时,RpcContext.getContext().getFuture()将返回null

2.7版本dubbo 客户端Consumer异步调用

从v2.7.0开始,Dubbo的所有异步编程接口开始以CompletableFuture为基础。

使用CompletableFuture签名的接口

需要服务提供者事先定义CompletableFuture签名的服务,具体参见服务端异步执行接口定义:

public interface AsyncService {
    CompletableFuture<String> sayHello(String name);
}

注意接口的返回类型是CompletableFuture<String>

XML引用服务:

<dubbo:reference id="asyncService" timeout="10000" interface="com.alibaba.dubbo.samples.async.api.As
1、调用远程服务:
// 调用直接返回CompletableFuture
CompletableFuture<String> future = asyncService.sayHello("async call request");
// 增加回调
future.whenComplete((v, t) -> {
    if (t != null) {
        t.printStackTrace();
    } else {
        System.out.println("Response: " + v);
    }
});
// 早于结果输出
System.out.println("Executed before response return.");### Springboot 
2、 使用RpcContext

在 consumer.xml 中配置:

<dubbo:reference id="asyncService" interface="org.apache.dubbo.samples.governance.api.AsyncService">
      <dubbo:method name="sayHello" async="true" />
</dubbo:reference>

调用代码:

// 此调用会立即返回null
asyncService.sayHello("world");
// 拿到调用的Future引用,当结果返回后,会被通知和设置到此Future
CompletableFuture<String> helloFuture = RpcContext.getContext().getCompletableFuture();
// 为Future添加回调
helloFuture.whenComplete((retValue, exception) -> {
    if (exception == null) {
        System.out.println(retValue);
    } else {
        exception.printStackTrace();
    }
});

或者,你也可以这样做异步调用:

CompletableFuture<String> future = RpcContext.getContext().asyncCall(
    () -> {
        asyncService.sayHello("oneway call request1");
    }
);

future.get();

2.7 版本 服务提供者Provider异步执行

Provier端异步执行将阻塞的业务从Dubbo内部线程池切换到业务自定义线程,避免Dubbo线程池过度占用,有助于避免不同服务间的互相影响。异步执行无益于节省资源或提升RPC响应性能,因为如果业务执行需要阻塞,则始终还是要有线程来负责执行。

注意:Provider端异步执行和Consumer端异步调用是相互独立的,你可以任意正交组合两端配置

  • Consumer同步 - Provider同步
  • Consumer异步 - Provider同步
  • Consumer同步 - Provider异步
  • Consumer异步 - Provider异步
1、定义CompletableFuture签名的接口

服务接口定义:

public interface AsyncService {
    CompletableFuture<String> sayHello(String name);
}

服务实现:

public class AsyncServiceImpl implements AsyncService {
    @Override
    public CompletableFuture<String> sayHello(String name) {
        RpcContext savedContext = RpcContext.getContext();
        // 建议为supplyAsync提供自定义线程池,避免使用JDK公用线程池
        return CompletableFuture.supplyAsync(() -> {
            System.out.println(savedContext.getAttachment("consumer-key1"));
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "async response from provider.";
        });
    }
}

通过return CompletableFuture.supplyAsync(),业务执行已从Dubbo线程切换到业务线程,避免了对Dubbo线程池的阻塞。

2、使用AsyncContext

Dubbo提供了一个类似Serverlet 3.0的异步接口AsyncContext,在没有CompletableFuture签名接口的情况下,也可以实现Provider端的异步执行。

服务接口定义:

public interface AsyncService {
    String sayHello(String name);
}

服务暴露,和普通服务完全一致:

<bean id="asyncService" class="org.apache.dubbo.samples.governance.impl.AsyncServiceImpl"/>
<dubbo:service interface="org.apache.dubbo.samples.governance.api.AsyncService" ref="asyncService"/>

服务实现:

public class AsyncServiceImpl implements AsyncService {
    public String sayHello(String name) {
        final AsyncContext asyncContext = RpcContext.startAsync();
        new Thread(() -> {
            // 如果要使用上下文,则必须要放在第一句执行
            asyncContext.signalContextSwitch();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 写回响应
            asyncContext.write("Hello " + name + ", response from provider.");
        }).start();
        return null;
    }
}

springboot 项目集成异步调用

1、启动来添加注解@EnableAsync

@SpringBootApplication(scanBasePackages = {"com.stylefeng.guns"})
@EnableDubboConfiguration
@EnableAsync
public class GatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

2、 客户端注解@Reference中添加属性async = true

 @Reference(interfaceClass = FilmAsyncServiceApi.class, validation = "1.0", async = true)
    private FilmAsyncServiceApi filmAsyncServiceApi;

3、 接口调用

    /**
     *  影片详情接口
     * @return
     */
    @RequestMapping(value = "films/{searchParam}", method = RequestMethod.GET)
    public ResponseVO films(@PathVariable String searchParam,
                            int searchType) throws ExecutionException, InterruptedException {
        // 根据searchType,判断查询类型
        FilmDetailVO filmDetail = filmServiceApi.getFilmDetail(searchType, searchParam);
        // 查询影片的详细信息 -> Dubbo 的异步获取
        // 获取影片描述信息
        if (filmDetail == null ) {
            return ResponseVO.serviceFail("没有可查询的影片");
        } else if (filmDetail.getFilmId() == null || filmDetail.getFilmId().trim().length() == 0) {
            return ResponseVO.serviceFail("没有可查询的影片");
        }
        String filmId = filmDetail.getFilmId();
        
        filmServiceApi.getFilmDesc(filmId);
        // 拿到调用的Future引用,当结果返回后,会被通知和设置到此future。
        Future<FilmDescVO> filmDescVOFuture = RpcContext.getContext().getFuture();
        
        // 获取图片信息
       filmServiceApi.getImags(filmId);
        Future<ImgVO> imgVOFuture = RpcContext.getContext().getFuture();

        // 获取导演信息
        filmServiceApi.getDectInfo(filmId);
        Future<ActorVO> actorVOFuture = RpcContext.getContext().getFuture();
        
        // 获取演员信息
        filmServiceApi.getActors(filmId);
        Future<List<ActorVO>> actorsFuture = RpcContext.getContext().getFuture();

        FilmInfoVO filmInfoVO = new FilmInfoVO();
        BeanUtils.copyProperties(filmDetail, filmInfoVO);

        // 组织Actor属性
        InfoRequestVO infoRequestVO = new InfoRequestVO();

        ActorRequestVO actorRequestVO = new ActorRequestVO();
        actorRequestVO.setActors(actorsFuture.get());
        actorRequestVO.setDirector(actorVOFuture.get());

        infoRequestVO.setActors(actorsFuture.get());
        infoRequestVO.setBiography(filmDescVOFuture.get().getBiography());
        infoRequestVO.setFilmId(filmId);
        infoRequestVO.setImgVO(imgVOFuture.get());

        filmInfoVO.setInfo04(infoRequestVO);
        return ResponseVO.success(IMG_PRE, filmInfoVO);
    }

在每个服务调用后,都会拿到对应Future,拿到值之后,设置到Future中,通过get方法获取。

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值