dubbo使用异步方式调用对象

来源:互联网 发布:淘宝客api怎么用 编辑:程序博客网 时间:2024/05/16 10:41

相对比与前一个小节来说,异步调用的功能也是很实用的,现在异步化的操作是越来越多了,异步化的好处也是比较明显的,可以加快后台的处理效率,做到代码直接的解耦,Dubbo就是一个支持异步调用的RPC框架

 

 

3.2.1 异步调用的场景

假设系统A,远程调用B系统的某个方法,这个方法与数据库的交互很多,逻辑相对复杂,正常的代码执行的时间是3秒,A系统调用完B系统之后,还需要做一些其他的逻辑操作,这个代码耗时可能需要4秒,等这个3秒的逻辑做完之后,根据B系统返回的结果再做一些其他的操作,那么同步调用的时间是3秒+4秒 = 7秒,那么一次操作的时间就是7秒


异步访问B系统:

3.2.2 同步调用的实现:

接口实现(providerconsumer端都需要

[java] view plain copy
  1. package org.bazinga.service;  
  2.   
  3. public interface AsyncInvokeService {  
  4.       
  5.     public Integer getResult();  
  6.   
  7. }  

接口实现,我们默认线程sleep三秒,3秒代表代码复杂的逻辑操作和数据库的交互的时间

[java] view plain copy
  1. package org.bazinga.service.impl;  
  2.   
  3. import org.bazinga.service.AsyncInvokeService;  
  4.   
  5. public class AsyncInvokeServiceImpl implements AsyncInvokeService {  
  6.   
  7.     public Integer getResult() {  
  8.           
  9.         try {  
  10.             Thread.sleep(3000l); //模拟复杂的逻辑操作时间和数据库交互的时间消耗  
  11.         } catch (InterruptedException e) {  
  12.             e.printStackTrace();  
  13.         }  
  14.         return 1;  
  15.     }  
  16.   
  17.   
  18. }  

在不开启异步调用的配置的时候,spring的配置文件和普通配置是一样的spring-dubbo-provider-async.xml,需要注意的是我们线程故意睡了3秒,这边我们配置timeout的时间为4秒,否则就会调用超时

[html] view plain copy
  1. <?xml version="1.1" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans    
  5.        http://www.springframework.org/schema/beans/spring-beans.xsd    
  6.        http://code.alibabatech.com/schema/dubbo    
  7.        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">  
  8.          
  9.     <dubbo:application owner="lyncc" name="bazinga-app" />  
  10.     <!--zookeeper注册中心 -->  
  11.     <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>   
  12.       
  13.     <dubbo:protocol name ="dubbo" port="20880" />  
  14.     <!-- 发布这个服务  调用超时是4秒,支持异步调用-->  
  15.     <dubbo:service  protocol="dubbo"  timeout="4000"  interface ="org.bazinga.service.AsyncInvokeService" ref="asyncInvokeService"/>         
  16.     <!-- 和本地bean一样实现服务 -->  
  17.     <bean id="asyncInvokeService" class="org.bazinga.service.impl.AsyncInvokeServiceImpl" />  
  18.       
  19. </beans>  
消费端的spring配置文件spring-dubbo-consumer-async.xml:

[html] view plain copy
  1. <?xml version="1.1" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans    
  5.        http://www.springframework.org/schema/beans/spring-beans.xsd    
  6.        http://code.alibabatech.com/schema/dubbo    
  7.        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">  
  8.          
  9.     <dubbo:application owner="lyncc" name="bazinga-consumer" />  
  10.     <!--zookeeper注册中心 -->  
  11.     <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>   
  12.       
  13.     <dubbo:reference id="asyncInvokeService" interface="org.bazinga.service.AsyncInvokeService"/>   
  14.       
  15. </beans>  

服务提供者端的测试类:

[java] view plain copy
  1. package org.bazinga.service.test;  
  2.   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4.   
  5. public class DubboxProviderAsyncService {  
  6.   
  7.     public static void main(String[] args) throws InterruptedException {  
  8.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
  9.                 "spring-dubbo-provider-async.xml");  
  10.         context.start();  
  11.         Thread.sleep(2000000l);  
  12.     }  
  13.   
  14. }  

服务消费者端的测试类:

[java] view plain copy
  1. package org.bazinga.service.test;  
  2.   
  3. import org.bazinga.service.AsyncInvokeService;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class DubboConsumerAsyncService {  
  7.   
  8.     public static void main(String[] args) throws InterruptedException {  
  9.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
  10.                 "spring-dubbo-consumer-async.xml");  
  11.         context.start();  
  12.   
  13.         long beginTime = System.currentTimeMillis();  
  14.   
  15.         for (int count = 0; count < 10; count++) { // 调用10次  
  16.              AsyncInvokeService asyncInvokeService =  
  17.              (AsyncInvokeService)context.getBean("asyncInvokeService");  
  18.              Integer result = asyncInvokeService.getResult(); //wait 返回结果 等待3秒  
  19.               
  20.              Thread.sleep(4000l); //模拟本地复杂的逻辑操作,耗时4秒  
  21.               
  22.              Integer localcalcResult = 2;//本地经过4秒处理得到的计算数据是2  
  23.               
  24.              System.out.println(result + localcalcResult);//根据远程调用返回的结果和本地操作的值,得到结果集  
  25.   
  26.         }  
  27.         System.out.println("call 10 times,cost time is "  
  28.                 + (System.currentTimeMillis() - beginTime));  
  29.   
  30.         Thread.sleep(2000000l);  
  31.     }  
  32. }  

先启动DubboxProviderAsyncService,然后再启动DubboConsumerAsyncService的main函数:

消费端的控制台打印信息是:



运行没有问题,但是调用10次,一共耗时71秒,假如改成异步调用,我们不需要等待调用返回的结果,而是在用的时候,再去获取值的话,这样会大大的提高执行的速度

3.2.3异步调用实现

异步调用的实现,很简单,现在我们修改一下配置文件,使其支持异步调用,其实配置相对比较简单,只需要在调用端的spring的配置文件中加上async=”true”,注意一定是在调用端配置该关键字,异步调用,顾名思义,就是需要告之调用者,调用之后不需要等待(Note:修改的是调用的spring配置文件spring-dubbo-consumer-async.xml

[html] view plain copy
  1. <?xml version="1.1" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans    
  5.        http://www.springframework.org/schema/beans/spring-beans.xsd    
  6.        http://code.alibabatech.com/schema/dubbo    
  7.        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">  
  8.          
  9.     <dubbo:application owner="lyncc" name="bazinga-consumer" />  
  10.     <!--zookeeper注册中心 -->  
  11.     <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>   
  12.       
  13.     <dubbo:reference id="asyncInvokeService" interface="org.bazinga.service.AsyncInvokeService" async="true"/>   
  14.       
  15. </beans>  
其实就是加了一个标签的支持,async="true",这样就支持了异步的调用了

修改消费者的测试代码,配置文件的修改只是告诉Dubbo,调用者会进行异步调用,但如何异步调用,还是需要调用者自己去实现的,实现依赖于RpcContext:

[java] view plain copy
  1. package org.bazinga.service.test;  
  2.   
  3. import java.util.concurrent.Future;  
  4.   
  5. import org.bazinga.service.AsyncInvokeService;  
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  7.   
  8. import com.alibaba.dubbo.rpc.RpcContext;  
  9.   
  10. public class DubboConsumerAsyncService {  
  11.   
  12.     public static void main(String[] args) throws InterruptedException {  
  13.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
  14.                 "spring-dubbo-consumer-async.xml");  
  15.         context.start();  
  16.   
  17.         long beginTime = System.currentTimeMillis();  
  18.   
  19.         for (int count = 0; count < 10; count++) { // 调用10次  
  20. //           AsyncInvokeService asyncInvokeService =  
  21. //           (AsyncInvokeService)context.getBean("asyncInvokeService");  
  22. //           Integer result = asyncInvokeService.getResult(); //wait 返回结果 等待3秒  
  23. //            
  24. //           Thread.sleep(4000l); //模拟本地复杂的逻辑操作,耗时4秒  
  25. //            
  26. //           Integer localcalcResult = 2;//本地经过4秒处理得到的计算数据是2  
  27. //            
  28. //           System.out.println(result + localcalcResult);//根据远程调用返回的结果和本地操作的值,得到结果集  
  29.                
  30.             AsyncInvokeService asyncInvokeService = (AsyncInvokeService) context  
  31.             .getBean("asyncInvokeService");  
  32.             Integer remotingResult = asyncInvokeService.getResult(); // 不等待  
  33.               
  34.             Thread.sleep(4000l); // 模拟本地复杂的逻辑操作,耗时4秒  
  35.               
  36.             Future<Integer> future = RpcContext.getContext().getFuture();  
  37.             try {  
  38.             remotingResult = future.get();  
  39.             } catch (java.util.concurrent.ExecutionException e) {  
  40.             e.printStackTrace();  
  41.               
  42.             }  
  43.             Integer localcalcResult = 2;// 本地经过4秒处理得到的计算数据是2  
  44.               
  45.             System.out.println(remotingResult + localcalcResult);// 根据远程调用返回的结果和本地操作的值,得到结果集  
  46.   
  47.         }  
  48.         System.out.println("call 10 times,cost time is "  
  49.                 + (System.currentTimeMillis() - beginTime));  
  50.   
  51.         Thread.sleep(2000000l);  
  52.     }  
  53. }  

关键代码就是:

[java] view plain copy
  1. Integer remotingResult = asyncInvokeService.getResult(); // 不等待  
这行代码不会阻塞至服务提供者端把数据返回,而是直接返回,然后睡了4秒,这个4秒,模仿的是本地的逻辑操作,这是其实远程的逻辑也在执行,这样就可以并行操作了,最后调用:

[java] view plain copy
  1. Future<Integer> future = RpcContext.getContext().getFuture();  
获取到远程调用异步返回的结果,完成最后的操作,我们可以看啊看控制台打印的结果:




可以看出结果没有变,仍旧是3,但是调用时间变成了41秒,异步调用的好处就可以显示出来了

 

 

3.2.4本章小结

 

异步调用是一个很实用的功能,在一些特定的开发业务场景下,能发挥很大的作用,Dubbo对异步调用支持的比较完善,并且方便开发人员适用,需要一个简单的标签就可以完成该功能了,方便大家的对该功能的掌握。

原创粉丝点击