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 测试麻烦,微小修改 全都得重新测 单体架构也称之为单体系统或者是单体应用.就是一种把系统中所有的功能.模块耦合在一个应用中的架构方式.其优点 ...
随机推荐
- MyBatis原理第四篇——statementHandler对象(sqlSession内部核心实现,插件的基础)
首先约定文中将的四大对象是指:executor, statementHandler,parameterHandler,resultHandler对象.(为了方便下面的文章说道四大对象就专指它们) 讲到 ...
- Java 初/中级面试题及答案【详细】
1.Java的HashMap是如何工作的? HashMap是一个针对数据结构的键值,每个键都会有相应的值,关键是识别这样的值. HashMap 基于 hashing 原理,我们通过 put ()和 g ...
- JQ面试问题(转载)
1 你在公司是怎么用jquery的? 答:在项目中是怎么用的是看看你有没有项目经验(根据自己的实际情况来回答) 你用过的选择器啊,动画啊,表单啊,ajax事件等 配置Jquery环境 下载jquery ...
- POJO、JAVABEAN、*O、EJB
POJO: 全称:Plain Old Java Object 解释:纯洁老式的java对象.从任何类继承.也没有实现任何接口,更没有被其它框架侵入的java对象 理解:通常我们常说的实体类 BEAN: ...
- JavaScript数组&类数组转换
一.数组 在JavaScript中数组可以容纳任何类型的值,可以是数字.字符串.对象.甚至其他数组(多为数组) var a = [1,'2',[3]]; a.length;//3 a[0];//1 a ...
- Unable to open debugger port (127.0.0.1:63777): java.net.BindException "Address
困扰了我好久,试过删掉taget文件夹rebuild,不删除Tomcat Server配置手动修改端口号也不行,试过杀掉java进程和重启机器,但是就是没效果. 解决: 删除Tomcat Server ...
- .Net 多线程 异步编程 Await、Async和Task
await和async简介 await和async是在C#5中引入,并且在.NetFramewor4.5以及.NetCore中进行了支持.主要是解决性能瓶颈,并且增强系统的响应能力. msdn关于 ...
- BZOJ2208: [Jsoi2010]连通数(tarjan bitset floyd)
题意 题目链接 Sol 数据水的一批,\(O(n^3)\)暴力可过 实际上只要bitset优化一下floyd复杂度就是对的了(\(O(\frac{n^3}{32})\)) 还可以缩点之后bitset维 ...
- element-ui input组件源码分析整理笔记(六)
input 输入框组件 源码: <template> <div :class="[ type === 'textarea' ? 'el-textarea' : 'el-in ...
- css3的calc()属性
1.calc()是css3的一个新增的功能,用来指定元素的长度,你可以使用calc()给元素的border.margin.pading.font-size和width等属性动态的设置值. 2.calc ...