dubbo--异步调用服务接口

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fangxinde/article/details/87260741

 服务提供端:接口方法延迟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

猜你喜欢

转载自blog.csdn.net/fangxinde/article/details/87260741