相关资料

官方文档

项目结构

代码示例

  • [EchoTestApp]
@RestController
@SpringBootApplication
@ImportResource("classpath:/consumer.xml")
public class EchoTestApp {
@Autowired
private ClientService clientService; @GetMapping("/hi/{name}")
public String hello(@PathVariable(name = "name") String name) throws InterruptedException, ExecutionException, TimeoutException {
return clientService.echo(name); } public static void main(String[] args) {
System.getProperties().put("server.port", 7070);
SpringApplication.run(EchoTestApp.class, args);
} @Configuration
@EnableDubbo(scanBasePackages = "consumer")
@PropertySource("classpath:/dubbo-consumer.properties")
static public class ConsumerConfiguration { }
}

和之前的区别在于 @ImportResource("classpath:/consumer.xml") 引入dubbo的xml配置

至于为什么用xml呢?因为没有找到 dubbo 事件通知 api 的参考示例

  • [consumer.xml]
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <dubbo:application name="annotation-consumer"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/> <dubbo:reference id="echoService" check="false" interface="com.xh.dubbo.learn.lesson2.api.IEchoService">
<dubbo:method name="echo" async="true" onreturn="notify.onReturn" onthrow="notify.onThrow"/>
</dubbo:reference> <bean class="com.xh.dubbo.learn.lesson2.service.ClientService" id="clientService">
<property name="echoService" ref="echoService"></property>
</bean>
</beans>
  • [dubbo-consumer.properties] 注释掉里面的所有配置,应为不能和上面的重复

  • [ClientService] 注释掉里面的重复配置

package com.xh.dubbo.learn.lesson2.service;

import com.xh.dubbo.learn.lesson2.api.IEchoService;
import org.apache.dubbo.rpc.RpcContext; import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; /*@Service*/
public class ClientService {
/**
* xml和注解不能同时定义
*/
/* @Reference*/
private IEchoService echoService; public String echo(String msg) throws ExecutionException, InterruptedException, TimeoutException { String result = echoService.echo(msg);// 这里的返回值为空,请不要使用
Future<String> future = RpcContext.getContext().getFuture();
// 业务线程可以开始做其他事情
System.out.println("start do other thing...");
//Thread.sleep(100);
System.out.println("print result:" + result);
System.out.println("other thing is done"); result = future.get(3000, TimeUnit.MILLISECONDS); // 阻塞需要获取异步结果时,也可以使用 get(timeout, unit) 设置超时时间
return result == null ? "error result" : result;
} public IEchoService getEchoService() {
return echoService;
} public void setEchoService(IEchoService echoService) {
this.echoService = echoService;
}
}
  • [INotify]
public interface INotify {
void onReturn(String returnStr, String arg); void onThrow(Throwable ex, String arg);
}
  • [NotifyImpl]
@Component("notify")
public class NotifyImpl implements INotify {
public void onReturn(String returnStr, String arg) {
System.out.println("do something onReturn");
System.out.println(returnStr);
System.out.println(arg); } public void onThrow(Throwable ex, String arg) {
System.out.println("do something onThrow");
System.out.println(ex.getMessage());
System.out.println(arg); }
}

需要注意的是以上方法的参数的类型和个数需要和配置文件中的比如 notify.onReturn 一致,只是前面多了返回值或者异常

输出

控制台

start do other thing...

print result:null

other thing is done

do something onReturn

echo: 哈哈哈

哈哈哈

浏览器

dubbo学习笔记四(异步调用)的更多相关文章

  1. MongoDB 学习笔记四 C#调用MongoDB

    驱动 下载 https://github.com/mongodb/mongo-csharp-driver/downloads 项目地址: https://github.com/mongodb/mong ...

  2. go微服务框架kratos学习笔记四(kratos warden-quickstart warden-direct方式client调用)

    目录 go微服务框架kratos学习笔记四(kratos warden-quickstart warden-direct方式client调用) warden direct demo-server gr ...

  3. java之jvm学习笔记四(安全管理器)

    java之jvm学习笔记四(安全管理器) 前面已经简述了java的安全模型的两个组成部分(类装载器,class文件校验器),接下来学习的是java安全模型的另外一个重要组成部分安全管理器. 安全管理器 ...

  4. Typescript 学习笔记四:回忆ES5 中的类

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

  5. muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制

    目录 muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制 eventfd的使用 eventfd系统函数 使用示例 EventLoop对eventfd的封装 工作时序 runInLoo ...

  6. python3.4学习笔记(四) 3.x和2.x的区别,持续更新

    python3.4学习笔记(四) 3.x和2.x的区别 在2.x中:print html,3.x中必须改成:print(html) import urllib2ImportError: No modu ...

  7. 零拷贝详解 Java NIO学习笔记四(零拷贝详解)

    转 https://blog.csdn.net/u013096088/article/details/79122671 Java NIO学习笔记四(零拷贝详解) 2018年01月21日 20:20:5 ...

  8. kvm虚拟化学习笔记(四)之kvm虚拟机日常管理与配置

    KVM虚拟化学习笔记系列文章列表----------------------------------------kvm虚拟化学习笔记(一)之kvm虚拟化环境安装http://koumm.blog.51 ...

  9. ASP.NET MVC 学习笔记-7.自定义配置信息 ASP.NET MVC 学习笔记-6.异步控制器 ASP.NET MVC 学习笔记-5.Controller与View的数据传递 ASP.NET MVC 学习笔记-4.ASP.NET MVC中Ajax的应用 ASP.NET MVC 学习笔记-3.面向对象设计原则

    ASP.NET MVC 学习笔记-7.自定义配置信息   ASP.NET程序中的web.config文件中,在appSettings这个配置节中能够保存一些配置,比如, 1 <appSettin ...

随机推荐

  1. 用js脚本实现链接

    使用json实现分页的时候,扩展了一下如何使用JS现实链接,其实很简单,但是之前我没用过... 就以分页的上一页,下一页来举例: <script type="text/javascri ...

  2. 《视觉SLAM十四讲》第1讲

    目录 一 视觉SLAM 注:原创不易,转载请务必注明原作者和出处,感谢支持! 一 视觉SLAM 什么是视觉SLAM? SLAM是Simultaneous Localization and Mappin ...

  3. vue.js中$watch的用法示例

    Vue.js 提供了一个方法 watch,它用于观察Vue实例上的数据变动.对应一个对象,键是观察表达式,值是对应回调.值也可以是方法名,或者是对象,包含选项. 在实例化时为每个键调用 $watch( ...

  4. JavaFx入门(一)

    JavaFx和Swing的对比: javaFX确实比swing好看些,但没有swing的事件按钮等写法爽快,特别是使用eclipse的matisse开发视图,托拉拽的方式.可javaFX不只是有swi ...

  5. 八十:memcached之安装与参数

    Memcached是一个高并发的内存键值对缓存系统,它的主要作用是将数据库查询结果,内容,以及其它一些耗时的计算结果缓存到系统内存中,从而加速Web应用程序的响应速度. 官网:http://memca ...

  6. gl-transitions 【68个转场效果图】

    angular.glsl Bounce.glsl BowTieHorizontal.glsl BowTieVertical.glsl burn.glsl ButterflyWaveScrawler.g ...

  7. es6语法图片切换demo

    git@github.com:qq719862911/ImageDemo.git

  8. django使用session来保存用户登录状态

    先建好登录用的model,其次理解使用cookie和session的原理,一个在本机保存,一个在服务器保存 使用session好处,可以设置登录过期的时间, 编写views中login的函数 def ...

  9. Kubernetes中的PV和PVC

    K8S引入了一组叫作Persistent Volume Claim(PVC)和Persistent Volume(PV)的API对象,大大降低了用户声明和使用持久化Volume的门槛.在Pod的Vol ...

  10. CentOS7 中arp欺骗,如何让你玩游戏的室友早点睡觉?

    环境:CentOS7 Linux安装arpspoof进行arp攻击实验 arpspoof是dsniff的一个组件,主要用于进行arp欺骗使用,所以说我们需要安装dsniff,虽然有很多看起来很容易,但 ...