淘宝SOA框架dubbo学习(7)--异步调用

2024-06-10 15:58

本文主要是介绍淘宝SOA框架dubbo学习(7)--异步调用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

转自:https://my.oschina.net/hanshubo/blog/378111

图片来源:点击打开链接

整个异步过程图片描述的很清楚,下面来看看代码:

一、服务提供者

1、服务提供者接口

[java]  view plain  copy
  1. package com.test.dubboser;  
  2.   
  3. public interface ServiceDemo2 {  
  4. public Person getPerson(String str,int age);  
  5. }  


2、Person 类

[java]  view plain  copy
  1. package com.test.dubboser;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class Person implements Serializable {  
  6. /** 
  7.      *  
  8.      */  
  9.     private static final long serialVersionUID = 8661104133888956335L;  
  10. private int age;  
  11. private String name;  
  12. public Person(){}  
  13. public Person(int age ,String name){  
  14.     this.age= age;  
  15.     this.name=name;  
  16. }  
  17. public int getAge() {  
  18.     return age;  
  19. }  
  20. public void setAge(int age) {  
  21.     this.age = age;  
  22. }  
  23. public String getName() {  
  24.     return name;  
  25. }  
  26. public void setName(String name) {  
  27.     this.name = name;  
  28. }  
  29.   
  30. @Override  
  31. public String  toString(){  
  32.     StringBuffer  buffer= new StringBuffer();  
  33.     buffer.append("name:"+name+"\t");  
  34.     buffer.append("age:"+age);  
  35.     return buffer.toString();  
  36.       
  37. }  
  38. }  

3、服务提供者接口实现类

[java]  view plain  copy
  1. package com.test.dubboser;  
  2.   
  3. public class ServiceImp2 implements ServiceDemo2{  
  4.   
  5.     public Person getPerson(String str,int age) {  
  6.        Person person=new Person();  
  7.        person.setName(str);  
  8.        person.setAge(age);  
  9.        return person;  
  10.     }  
  11. }  


4、配置文件

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.         http://code.alibabatech.com/schema/dubbo  
  8.         http://code.alibabatech.com/schema/dubbo/dubbo.xsd  
  9.         ">       
  10.      <!-- 提供方应用信息,用于计算依赖关系,这个和client没必要一致 -->  
  11.     <dubbo:application name="hello-world-app-my" />  
  12.     <!-- 多注册中心配置 -->  
  13.    <dubbo:registry  protocol="zookeeper"  address="192.168.0.102:2181"/>  
  14.      <!-- 用dubbo协议在20880端口暴露服务 -->  
  15.     <dubbo:protocol name="dubbo" port="20880"/>    
  16.      <!-- 声明需要暴露的服务接口 -->  
  17.     <dubbo:service  interface="com.test.dubboser.ServiceDemo"  
  18.         ref="demoService"/>   
  19.     <dubbo:service  interface="com.test.dubboser.ServiceDemo2"  
  20.         ref="demoService2"/>   
  21.     <dubbo:service  interface="com.test.dubboser.CacheService"  
  22.         ref="cacheService"/>   
  23.     <!--和本地bean一样实现服务 -->  
  24.     <bean id="demoService" class="com.test.dubboser.ServiceImp"/>  
  25.     <bean id="demoService2" class="com.test.dubboser.ServiceImp2"/>  
  26.     <!-- 结果缓存 -->  
  27.     <bean id="cacheService" class="com.test.dubboser.CacheServiceImp"/>  
  28.       
  29. </beans>  

二、服务消费者

1、配置文件

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.         http://code.alibabatech.com/schema/dubbo  
  8.         http://code.alibabatech.com/schema/dubbo/dubbo.xsd  
  9.         ">       
  10.     <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->  
  11.     <dubbo:application name="consumer-of-helloworld-app-my" />       
  12.     <!-- 使用zookeeper广播注册中心暴露发现服务地址 -->  
  13.     <dubbo:registry  protocol="zookeeper"  address="192.168.0.102:2181"/>    
  14.     <!--获取服务  -->  
  15.     <dubbo:reference id="demoServicemy"    interface="com.test.dubboser.ServiceDemo"/>  
  16.     <!--异步功能实现-->  
  17.     <dubbo:reference id="demoServicemy2"   interface="com.test.dubboser.ServiceDemo2">  
  18.       <dubbo:method name="getPerson" async="true" />  
  19.     </dubbo:reference>  
  20.     <!--结果缓存配置  -->  
  21.     <dubbo:reference id="cacheService"   interface="com.test.dubboser.CacheService"  cache="true"/>  
  22. </beans>  
注意这里的这一行,实现异步配置

[html]  view plain  copy
  1. <dubbo:reference id="demoServicemy2"   interface="com.test.dubboser.ServiceDemo2">  
  2.      <dubbo:method name="getPerson" async="true" />  
  3.    </dubbo:reference>  

2、消费者代码

[java]  view plain  copy
  1. package com.test.dubbocli;  
  2.   
  3. import java.util.concurrent.ExecutionException;  
  4. import java.util.concurrent.Future;  
  5.   
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  7.   
  8. import com.alibaba.dubbo.rpc.RpcContext;  
  9. import com.test.dubboser.CacheService;  
  10. import com.test.dubboser.Person;  
  11. import com.test.dubboser.ServiceDemo;  
  12. import com.test.dubboser.ServiceDemo2;  
  13.   
  14. public class Main {  
  15.   
  16.     public static void main(String[] args) throws InterruptedException, ExecutionException {  
  17.         run();  
  18.     }  
  19.      public static void run() throws InterruptedException, ExecutionException{  
  20.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationConsumer.xml" });  
  21.         context.start();  
  22.         //ServiceDemo demoServer = (ServiceDemo) context.getBean("demoServicemy");  
  23.         ServiceDemo2 demoServer2 = (ServiceDemo2) context.getBean("demoServicemy2");  
  24.         /*ServiceDemo demoServer3 = (ServiceDemo) context.getBean("demoServicemy3");*/  
  25.         /*String str=demoServer.say("java ---->>>");*/  
  26.         //调用后立即返回null  
  27.         Person person=demoServer2.getPerson("www"13);  
  28.         System.err.println("立即返回的为null:"+person);  
  29.         //拿到调用的Future引用,当结果返回后,会被通知和设置到此Future。  
  30.         Future<Person> pFuture = RpcContext.getContext().getFuture();  
  31.         //如果Person已返回,直接拿到返回值,否则线程wait,等待Person返回后,线程会被notify唤醒。  
  32.         person = pFuture.get();  
  33.         System.out.println("返回的有值"+person);  
  34.         System.out.println(person);  
  35.      }  
  36. }  

3、运行结果

[html]  view plain  copy
  1. 立即返回的为null:null  
  2. future中获取值:name:www age:13  

三、异步返回值,和异步无返回值

你也可以设置是否等待消息发出:(异步总是不等待返回)

1、sent="true" 等待消息发出,消息发送失败将抛出异常。

2、sent="false" 不等待消息发出,将消息放入IO队列,即刻返回。

[html]  view plain  copy
  1. <dubbo:reference id="demoServicemy2"   interface="com.test.dubboser.ServiceDemo2">  
  2.       <dubbo:method name="getPerson" async="true" sent="true" />  
  3.     </dubbo:reference>  
3、 如果你只是想异步,完全忽略返回值,可以配置return="false",以减少Future对象的创建和管理成本:

[html]  view plain  copy
  1. <dubbo:reference id="demoServicemy2"   interface="com.test.dubboser.ServiceDemo2">  
  2.       <dubbo:method name="getPerson" async="true" return="false" />  
  3.     </dubbo:reference>  
设置了return =“false”后我们就获取不到Future对象,当然就获取不到返回值,这样就只有异步调用了服务端方法而没有返回值,执行的流程也就是最开始图形中的1和2 这两步,没有了其他的步骤所以速度也就比较快……


这篇关于淘宝SOA框架dubbo学习(7)--异步调用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1048608

相关文章

重新对Java的类加载器的学习方式

《重新对Java的类加载器的学习方式》:本文主要介绍重新对Java的类加载器的学习方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍1.1、简介1.2、符号引用和直接引用1、符号引用2、直接引用3、符号转直接的过程2、加载流程3、类加载的分类3.1、显示

Python 异步编程 asyncio简介及基本用法

《Python异步编程asyncio简介及基本用法》asyncio是Python的一个库,用于编写并发代码,使用协程、任务和Futures来处理I/O密集型和高延迟操作,本文给大家介绍Python... 目录1、asyncio是什么IO密集型任务特征2、怎么用1、基本用法2、关键字 async1、async

Spring框架中@Lazy延迟加载原理和使用详解

《Spring框架中@Lazy延迟加载原理和使用详解》:本文主要介绍Spring框架中@Lazy延迟加载原理和使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、@Lazy延迟加载原理1.延迟加载原理1.1 @Lazy三种配置方法1.2 @Component

嵌入式Linux驱动中的异步通知机制详解

《嵌入式Linux驱动中的异步通知机制详解》:本文主要介绍嵌入式Linux驱动中的异步通知机制,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、异步通知的核心概念1. 什么是异步通知2. 异步通知的关键组件二、异步通知的实现原理三、代码示例分析1. 设备结构

Java调用Python的四种方法小结

《Java调用Python的四种方法小结》在现代开发中,结合不同编程语言的优势往往能达到事半功倍的效果,本文将详细介绍四种在Java中调用Python的方法,并推荐一种最常用且实用的方法,希望对大家有... 目录一、在Java类中直接执行python语句二、在Java中直接调用Python脚本三、使用Run

Python如何调用指定路径的模块

《Python如何调用指定路径的模块》要在Python中调用指定路径的模块,可以使用sys.path.append,importlib.util.spec_from_file_location和exe... 目录一、sys.path.append() 方法1. 方法简介2. 使用示例3. 注意事项二、imp

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Redis消息队列实现异步秒杀功能

《Redis消息队列实现异步秒杀功能》在高并发场景下,为了提高秒杀业务的性能,可将部分工作交给Redis处理,并通过异步方式执行,Redis提供了多种数据结构来实现消息队列,总结三种,本文详细介绍Re... 目录1 Redis消息队列1.1 List 结构1.2 Pub/Sub 模式1.3 Stream 结

使用Python实现一个优雅的异步定时器

《使用Python实现一个优雅的异步定时器》在Python中实现定时器功能是一个常见需求,尤其是在需要周期性执行任务的场景下,本文给大家介绍了基于asyncio和threading模块,可扩展的异步定... 目录需求背景代码1. 单例事件循环的实现2. 事件循环的运行与关闭3. 定时器核心逻辑4. 启动与停