dubbo学习笔记四(异步调用)
相关资料
项目结构
代码示例
- [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学习笔记四(异步调用)的更多相关文章
- MongoDB 学习笔记四 C#调用MongoDB
驱动 下载 https://github.com/mongodb/mongo-csharp-driver/downloads 项目地址: https://github.com/mongodb/mong ...
- go微服务框架kratos学习笔记四(kratos warden-quickstart warden-direct方式client调用)
目录 go微服务框架kratos学习笔记四(kratos warden-quickstart warden-direct方式client调用) warden direct demo-server gr ...
- java之jvm学习笔记四(安全管理器)
java之jvm学习笔记四(安全管理器) 前面已经简述了java的安全模型的两个组成部分(类装载器,class文件校验器),接下来学习的是java安全模型的另外一个重要组成部分安全管理器. 安全管理器 ...
- Typescript 学习笔记四:回忆ES5 中的类
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制
目录 muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制 eventfd的使用 eventfd系统函数 使用示例 EventLoop对eventfd的封装 工作时序 runInLoo ...
- python3.4学习笔记(四) 3.x和2.x的区别,持续更新
python3.4学习笔记(四) 3.x和2.x的区别 在2.x中:print html,3.x中必须改成:print(html) import urllib2ImportError: No modu ...
- 零拷贝详解 Java NIO学习笔记四(零拷贝详解)
转 https://blog.csdn.net/u013096088/article/details/79122671 Java NIO学习笔记四(零拷贝详解) 2018年01月21日 20:20:5 ...
- kvm虚拟化学习笔记(四)之kvm虚拟机日常管理与配置
KVM虚拟化学习笔记系列文章列表----------------------------------------kvm虚拟化学习笔记(一)之kvm虚拟化环境安装http://koumm.blog.51 ...
- 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 ...
随机推荐
- linux 之 pthread_create 实现类的成员函数做参数
在C++的类中,普通成员函数不能作为pthread_create的线程函数,如果要作为pthread_create中的线程函数,必须是static ! 在C语言中,我们使用pthread_create ...
- RestAssured
配置MAVEN <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-ass ...
- Vue常见的框架
1. Element:一套为开发者,设计师和产品经理准备的基于Vue 2.0的桌面端组件库 地址:https://element.eleme.cn/#/zh-CN 2.iview:主要服务于PC界面的 ...
- eslint 检查单个文件的错误
问题: 批量检查时,没有针对性,想针对单个文件进行检查 解决办法:./node_modules/.bin/eslint your file
- UDP打洞原理介绍
NAT穿越模块的设计与实现 Internet的快速发展以及IPv4地址数量的不足使得NAT设备得到了大规模的应用,然而这也给越来越多的端到端通信也带来了不少的麻烦.一般来说,NAT设备允许内网内主机 ...
- mips调试
0x01 环境搭建 由于我们通常的操作系统指令集都是x86的,所以无法跑MIPS程序.这时候就需要装QEMU来模拟,QEMU通过源码编译较为复杂,我们又没有特殊的需求,所以直接使用ubuntu的APT ...
- c++ STL之unordered_set
unordered_set的特点: 自身值就是主键,所以值唯一并且不可修改 基于hash表的无序排列 unordered_set基于哈希表,是无序的. 在一个 unordered_set 容器中,元素 ...
- 使用Navicat为Oracle新增用户
步骤请参考帖子https://www.cnblogs.com/franson-2016/p/5925593.html 需要注意的是新增用户时不能使用小写,否则不能登录,之前新增一个小写的用户名,授予c ...
- 12 mysql性能抖动
12 mysql性能抖动 sql语句为什么变”慢”了 在介绍WAL机制时,innodb在处理更新语句的时候,只做了写日志这一个磁盘操作,就是redo log,在更新内存写完redo log之后,就返回 ...
- ubuntu 16.04 server 扩容(LVM)磁盘
因为发现我的本地server出现磁盘满了的情况 所以进行lvm的扩容 1 查看磁盘情况 df -h 原本发现 /dev/mapper/ubuntu1604--vg-root 这个磁盘满了 所以要进行扩 ...