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

本文详细介绍了Dubbo框架中的异步调用机制。包括服务提供者与消费者的配置、异步调用的代码实现及运行结果展示。此外还探讨了异步返回值和无返回值的不同配置方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自: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 这两步,没有了其他的步骤所以速度也就比较快……


### 关于ArcGIS License Server无法启动的解决方案 当遇到ArcGIS License Server无法启动的情况,可以从以下几个方面排查并解决问题: #### 1. **检查网络配置** 确保License Server所在的计算机能够被其他客户端正常访问。如果是在局域网环境中部署了ArcGIS Server Local,则需要确认该环境下的网络设置是否允许远程连接AO组件[^1]。 #### 2. **验证服务状态** 检查ArcGIS Server Object Manager (SOM) 的运行情况。通常情况下,在Host SOM机器上需将此服务更改为由本地系统账户登录,并重启相关服务来恢复其正常工作流程[^2]。 #### 3. **审查日志文件** 查看ArcGIS License Manager的日志记录,寻找任何可能指示错误原因的信息。这些日志可以帮助识别具体是什么阻止了许可服务器的成功初始化。 #### 4. **权限问题** 确认用于启动ArcGIS License Server的服务账号具有足够的权限执行所需操作。这包括但不限于读取/写入特定目录的权利以及与其他必要进程通信的能力。 #### 5. **软件版本兼容性** 保证所使用的ArcGIS产品及其依赖项之间存在良好的版本匹配度。不一致可能会导致意外行为完全失败激活license server的功能。 #### 示例代码片段:修改服务登录身份 以下是更改Windows服务登录凭据的一个简单PowerShell脚本例子: ```powershell $serviceName = "ArcGISServerObjectManager" $newUsername = ".\LocalSystemUser" # 替换为实际用户名 $newPassword = ConvertTo-SecureString "" -AsPlainText -Force Set-Service -Name $serviceName -StartupType Automatic New-ServiceCredential -ServiceName $serviceName -Account $newUsername -Password $newPassword Restart-Service -Name $serviceName ``` 上述脚本仅作为示范用途,请依据实际情况调整参数值后再实施。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值