5.Dubbo2.5.3泛化引用和泛化实现
转载请出自出处:http://www.cnblogs.com/hd3013779515/
1.泛化引用
泛接口调用方式主要用于客户端没有API接口及模型类元的情况,参数及返回值中的所有POJO均用Map表示,通常用于框架集成,比如:实现一个通用的服务测试框架,可通过GenericService调用所有服务实现。
(1)服务提供者
整体结构
模型User.java
package cn.ljh.dubbo.model; public class User {
private int id;
private String username;
private String password; get set ... }
服务接口HelloService.java
package cn.ljh.dubbo.service; import cn.ljh.dubbo.model.User; public interface HelloService {
public String sayHello(String name);
public User getUser(User user);
}
服务接口实现HelloServiceImpl.java
package cn.ljh.dubbo.service.impl; import cn.ljh.dubbo.model.User;
import cn.ljh.dubbo.service.HelloService; public class HelloServiceImpl implements HelloService{ public String sayHello(String name) {
return "Hello World:" + name;
} public User getUser(User user) {
user.setUsername(user.getUsername() + " Response");
return user;
} }
配置文件applicationProvider.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 具体的实现bean -->
<bean id="helloservice" class="cn.ljh.dubbo.service.impl.HelloServiceImpl" />
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="hello-provider" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://192.168.137.130:2181?backup=192.168.137.131:2181,192.168.137.132:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="cn.ljh.dubbo.service.HelloService" ref="helloservice" /> </beans>
服务提供者启动程序HelloServiceTest.java
package cn.ljh.dubbo.service; import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; public class HelloServiceTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"applicationProvider.xml"});
context.start();
System.out.println("服务注册成功");
try {
System.in.read();//让此程序一直跑,表示一直提供服务
} catch (IOException e) {
e.printStackTrace();
}
}
}
(2)服务消费者
整体结构
模型User.java 和 服务接口HelloService.java 在服务消费者中没有。
配置文件applicationConsumer.xml
在dubbo:reference中增加generic
=
"true"
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="hello-consumer" /> <!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://192.168.137.130:2181?backup=192.168.137.131:2181,192.168.137.132:2181" /> <!-- 生成远程服务代理,可以和本地bean一样使用HelloService -->
<dubbo:reference id="helloService" interface="cn.ljh.dubbo.service.HelloService" generic="true"/> </beans>
服务消费者启动程序HelloServiceConsumerTest.java
在程序中使用了泛化接口GenericService和参数map。
package cn.ljh.dubbo.service; import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.alibaba.dubbo.rpc.service.GenericService; public class HelloServiceConsumerTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationConsumer.xml" }); context.start(); //泛接口调用方式主要用于客户端没有API接口及模型类元的情况,参数及返回值中的所有POJO均用Map表示
//用com.alibaba.dubbo.rpc.service.GenericService可以替代所有接口引用
GenericService providerService = (GenericService) context.getBean("helloService"); //基本类型以及Date,List,Map等不需要转换,直接调用
Object result = providerService.$invoke(
"sayHello", new String[] { "java.lang.String" }, new Object[] { "Tom" }); System.out.println(result); // 用Map表示POJO参数,如果返回值为POJO也将自动转成Map
Map<String, Object> user = new HashMap<String, Object>();
user.put("id", "100");
user.put("username", "Lily");
user.put("password", "yyy");
//如果返回POJO将自动转成Map
Object result2 = providerService.$invoke("getUser", new String[]{
"cn.ljh.dubbo.model.User"}, new Object[]{user}); Map<String, Object> user2 = (Map<String, Object>)result2; System.out.println("id:"+user2.get("id")
+ " username:"+user2.get("username")
+ " password:"+user2.get("password")); try {
System.in.read();//让此程序一直跑
} catch (IOException e) {
e.printStackTrace();
}
}
}
(3)测试结果
如下可以看出能够正确的调用和参数转换。
2.泛化实现
泛接口实现方式主要用于服务器端没有API接口及模型类元的情况,参数及返回值中的所有POJO均用Map表示,通常用于框架集成,比如:实现一个通用的远程服务Mock框架,可通过实现GenericService接口处理所有服务请求。
(1)服务提供者
整体结构
模型User.java 和 服务接口HelloService.java 没有。
服务接口实现HelloServiceImpl.java
使用了GenericService接口
package cn.ljh.dubbo.service.impl; import java.util.Map; import com.alibaba.dubbo.rpc.service.GenericException;
import com.alibaba.dubbo.rpc.service.GenericService; public class HelloServiceImpl implements GenericService{ public Object $invoke(String method, String[] parameterTypes, Object[] args)
throws GenericException { if ("sayHello".equals(method)) {
return "Hello World:" + args[0];
}else if ("getUser".equals(method)) {
Map<String, Object> user = (Map<String, Object>)args[0];
user.put("username", user.get("username") + " Response");
return user;
} return null;
}
}
配置文件applicationProvider.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 具体的实现bean -->
<bean id="helloservice" class="cn.ljh.dubbo.service.impl.HelloServiceImpl" />
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="hello-provider" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://192.168.137.130:2181?backup=192.168.137.131:2181,192.168.137.132:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="cn.ljh.dubbo.service.HelloService" ref="helloservice" /> </beans>
服务提供者启动程序HelloServiceTest.java
package cn.ljh.dubbo.service; import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; public class HelloServiceTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"applicationProvider.xml"});
context.start();
System.out.println("服务注册成功");
try {
System.in.read();//让此程序一直跑,表示一直提供服务
} catch (IOException e) {
e.printStackTrace();
}
}
}
(2)服务消费者
整体结构
模型User.java
package cn.ljh.dubbo.model; public class User {
private int id;
private String username;
private String password; get set... }
服务接口HelloService.java
package cn.ljh.dubbo.service; import cn.ljh.dubbo.model.User; public interface HelloService {
public String sayHello(String name);
public User getUser(User user);
}
配置文件applicationConsumer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="hello-consumer" /> <!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://192.168.137.130:2181?backup=192.168.137.131:2181,192.168.137.132:2181" /> <!-- 生成远程服务代理,可以和本地bean一样使用HelloService -->
<dubbo:reference id="helloService" interface="cn.ljh.dubbo.service.HelloService"/> </beans>
服务消费者启动程序HelloServiceConsumerTest.java
package cn.ljh.dubbo.service; import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.ljh.dubbo.model.User; public class HelloServiceConsumerTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationConsumer.xml" }); context.start(); HelloService providerService = (HelloService) context.getBean("helloService"); System.out.println(providerService.sayHello("Tom")); User user = new User();
user.setId(100);
user.setUsername("Lily test");
user.setPassword("yyy"); User user2 = providerService.getUser(user); System.out.println("id:"+user2.getId()
+ " username:"+user2.getUsername()
+ " password:"+user2.getPassword()); try {
System.in.read();//让此程序一直跑
} catch (IOException e) {
e.printStackTrace();
}
}
}
(3)测试结果
如下可以看出能够正确的调用和参数转换。
参考:
http://dubbo.io/User+Guide-zh.htm
5.Dubbo2.5.3泛化引用和泛化实现的更多相关文章
- Dubbo -- 系统学习 笔记 -- 示例 -- 泛化引用
Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 泛化引用 泛接口调用方式主要用于客户端没有API接口及模型类元的情况,参数及返回值 ...
- dubbo之泛化引用
使用泛化调用 泛化接口调用方式主要用于客户端没有 API 接口及模型类元的情况,参数及返回值中的所有 POJO 均用 Map 表示,通常用于框架集成,比如:实现一个通用的服务测试框架,可通过 Gene ...
- dubbo 转
http://blog.csdn.net/zhiguozhu/article/details/50517513 背景 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式 ...
- dubbo 官方参考手册~备案(防止哪天阿里一生气把dubbo给删除了)
首页 || 下载 || 用户指南 || 开发者指南 || 管理员指南 || 培训文档 || 常见问题解答 || 发布记录 || 发展路线 || 社区 E ...
- dubbo用户指南
用户指南 入门 背景 需求 架构 用法 快速启动 服务提供者 服务消费者 依赖 必需依赖 缺省依赖 可选依赖 成熟度 功能成熟度 策略成熟度 配置 Xml配置 属性配置 注解配置 API配置 示例 启 ...
- dubbo用户指南-总结
dubbo用户指南-总结 入门 背景 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 单一应用 ...
- 初识Dubbo 系列之5-Dubbo 成熟度
成熟度 功能成熟度 Feature特征 Maturity成熟度 Strength强度 Problem问题 Advise建议 User用户 并发控制 Tested 并发控制 试用 连接控制 Te ...
- [收藏]Dubbo官方资料
首页 || 下载 || 用户指南 || 开发者指南 || 管理员指南 || 培训文档 || 常见问题解答 || 发布记录 || 发展路线 || 社区 English ...
- Dubble 01 架构模型&start project
Dubbo 01 架构模型 传统架构 All in One 测试麻烦,微小修改 全都得重新测 单体架构也称之为单体系统或者是单体应用.就是一种把系统中所有的功能.模块耦合在一个应用中的架构方式.其优点 ...
随机推荐
- C# Web 数据注解Data Annotations、模型状态ModelState、数据验证
C#中的模型状态与数据注解,为我们提供了很便利的请求数据的验证. 1. ModelState ModelState在进行数据验证的时候很有用的,它是: 1)验证数据,以及保存数据对应的错误信息. 2) ...
- .Net Core使用Socket与树莓派进行通信
前言 去年买的树莓派一直放在抽屉里吃灰,前些阵子Debian 9发布,也不出意外的支持了树莓派. 于是重新拿出读卡器又重新了装上了Debian桌面版系统. 介绍 现在这个东西目前的程度只是了解一下Py ...
- java中递归实现复制多级文件夹
常见的流的用法 递归实现复制多级文件夹 FileInputStream & FileOutputStream String content = null;//用来储存解码后的byte数组 in ...
- NIO学习笔记七:Pipe
Java NIO 管道是2个线程之间的单向数据连接.Pipe有一个source通道和一个sink通道.数据会被写到sink通道,从source通道读取. 这里是Pipe原理的图示: 示例代码 Pipe ...
- C++ 的那些坑 (Day 0)
C++ 的那些坑 (Day 0) 永远的for循环 其实这里要说的并不是for循环本身还是其中的计数变量的类型的选择. std::string s = "abcd" for (st ...
- linux服务器SSH破解预防方法
1.linux服务器通过配置 /etc/hosts.deny 禁止对方IP通过SSH登录我的服务器 vim /etc/hosts.deny 2.不用SSH服务的默认端口22,重新设置一个新端口,最好设 ...
- 微信小程序-上传照片-多张显示
图片就是一个简单的效果 实现 先看wxml和wxss代码 <view class='in-demand'> <view class='dema-title'> <text ...
- 自己来实现一套IOC注解框架
我们自己来实现一套IOC注解框架吧,采用的方式反射加注解和Xutils类似,但我们尽量不写那么麻烦,也不打算采用动态代理,我们扩展一个检测网络的注解,比如没网的时候我们不去执行方法而是给予没有网络的提 ...
- Expo大作战(一)--什么是expo,如何安装expo clinet和xde,xde如何使用
简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...
- [Swift] Swift笔记
开始整理Swift笔记了.打算直接用Playground去写,里面自带的Markup语法和Markdown差不多,显示的效果也不差于博客.而且用Xcode看代码也方便.所以这部分内容不再在博客里记录了 ...