dubbo--异步调用服务接口

我们知道,Dubbo缺省协议采用单一长连接,底层实现是Netty的NIO异步通讯机制,基于这种机制,Dubbo实现了以下几种调用方式。

同步调用

异步调用

参数回调

时间通知

本文主要介绍客户端通过dubbo异步调用服务端接口。

当服务端提供的服务耗时过长,客户端采用异步调用服务端接口,这样能有效利用客户端资源

 服务提供端:接口方法延迟3秒

@Service(value = "orderService2")
public class OrderServiceImpl2 implements IOrderServices{

    @Override
    public DoOrderResponse doOrder(DoOrderRequest request) {
        System.out.println("曾经来过版本2:"+request);
        DoOrderResponse response = new DoOrderResponse();
        response.setData("版本测试");
        response.setCode("0002");
        response.setMemo("处理成功,版本2");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return response;
    }
}

服务端相关配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd" default-autowire="byName">
    <context:annotation-config></context:annotation-config>
    <context:component-scan base-package="com.gupao.vip.mic.dubbo.order"/>
    <!--name:当前项目在整个分布式架构里面的唯一名称,计算依赖关系的标签 owner表示当前application是由谁维护-->
    <dubbo:application name="order-provider" owner="mic"/>
    <!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
    <dubbo:registry protocol="zookeeper" address="192.168.70.63:2181,192.168.70.64:2181,192.168.70.65:2181,192.168.70.66:2181"/>
    <!--当前服务发布所依赖的协议-->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!--指定hessian协议-->
    <dubbo:protocol name="hessian" port="8090" server="jetty"/>
    <!--服务发布的配置,需要暴露的服务接口,因线程故意睡了3秒,这边我们配置timeout的时间为4秒,否则就会调用超时-->
    <dubbo:service interface="com.gupao.vip.mic.dubbo.order.IOrderServices" ref="orderService2" protocol="dubbo" version="2.0" timeout="4000"/>
    <dubbo:service interface="com.gupao.vip.mic.dubbo.order.IOrderQueryService" ref="orderQueryService" protocol="dubbo" version="1.0"/>
</beans>

 

异步操作主要在消费端处理:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!--name:当前项目在整个分布式架构里面的唯一名称,计算依赖关系的标签 owner表示当前application是由谁维护-->
    <dubbo:application name="order-user" owner="mic"/>
    <!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
    <dubbo:registry address="zookeeper://192.168.70.66:2181?backup=192.168.70.65:2181,192.168.70.64:2181,192.168.70.63:2181"/>
    <!--生成一个远程服务的调用代理 -->
    <dubbo:reference id="orderService2.0"
                     interface="com.gupao.vip.mic.dubbo.order.IOrderServices"
                     protocol="dubbo"
                      version="2.0">
        <dubbo:method name="doOrder" async="true"/>
    </dubbo:reference>
</beans>

 调用doOrder方法需耗时很久,故暂时不能返回结果,但不影响主线程的运行。

doOrder方法延迟3秒,main方法延迟4秒,如果同步运行,至少需要7秒,但采用异步调用doOrder方法,实际需要的时间

5秒左右,故效率大大提高。

public class App 
{
    public static void main( String[] args ) throws IOException, ExecutionException, InterruptedException {

        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("order-consumer.xml");
        DoOrderRequest request = new DoOrderRequest();
        request.setName("fangxinde");
        //异步操作前记录时间
        long startTime = System.currentTimeMillis();
        IOrderServices services02=  (IOrderServices)context.getBean("orderService2.0");
        //异步调用doOrder(假设此调用需耗时很久,故暂时不需返回结果)
        services02.doOrder(request);
        Thread.sleep(4000);
        Future<DoOrderResponse> future = RpcContext.getContext().getFuture();
        DoOrderResponse result = future.get();
        //调用结束后记录时间
        long endTime = System.currentTimeMillis();
        System.out.println(result+"====>获取结果耗时时间:"+(endTime-startTime)+"s");
        //System.in.read();
    }
}

 

运行结果:

DoOrderResponse{data=版本测试, code='0002', memo='处理成功,版本2'}====>获取结果耗时时间:5393s

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值