转载请出自出处: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泛化引用和泛化实现的更多相关文章

  1. Dubbo -- 系统学习 笔记 -- 示例 -- 泛化引用

    Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 泛化引用 泛接口调用方式主要用于客户端没有API接口及模型类元的情况,参数及返回值 ...

  2. dubbo之泛化引用

    使用泛化调用 泛化接口调用方式主要用于客户端没有 API 接口及模型类元的情况,参数及返回值中的所有 POJO 均用 Map 表示,通常用于框架集成,比如:实现一个通用的服务测试框架,可通过 Gene ...

  3. dubbo 转

      http://blog.csdn.net/zhiguozhu/article/details/50517513 背景 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式 ...

  4. dubbo 官方参考手册~备案(防止哪天阿里一生气把dubbo给删除了)

          首页  ||  下载  ||  用户指南  ||  开发者指南  ||  管理员指南  ||  培训文档  ||  常见问题解答  ||  发布记录  ||  发展路线  ||  社区 E ...

  5. dubbo用户指南

    用户指南 入门 背景 需求 架构 用法 快速启动 服务提供者 服务消费者 依赖 必需依赖 缺省依赖 可选依赖 成熟度 功能成熟度 策略成熟度 配置 Xml配置 属性配置 注解配置 API配置 示例 启 ...

  6. dubbo用户指南-总结

    dubbo用户指南-总结 入门 背景 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 单一应用 ...

  7. 初识Dubbo 系列之5-Dubbo 成熟度

    成熟度 功能成熟度 Feature特征 Maturity成熟度 Strength强度 Problem问题 Advise建议 User用户 并发控制 Tested 并发控制   试用   连接控制 Te ...

  8. [收藏]Dubbo官方资料

    首页  ||  下载  ||  用户指南  ||  开发者指南  ||  管理员指南  ||  培训文档  ||  常见问题解答  ||  发布记录  ||  发展路线  ||  社区 English ...

  9. Dubble 01 架构模型&start project

    Dubbo 01 架构模型 传统架构 All in One 测试麻烦,微小修改 全都得重新测 单体架构也称之为单体系统或者是单体应用.就是一种把系统中所有的功能.模块耦合在一个应用中的架构方式.其优点 ...

随机推荐

  1. oracleHelper 操作帮助类

    using System; using System.Configuration; using System.Data; using System.Collections; using Oracle. ...

  2. angularjs 2.0 简单入门1

    一:首先要写json文件,并下载所有的包 1,在任意目录下新建文件夹 命名为angular2Dome,也可以使用命令  mkdir angular2Dome 回车. 2,在angular2Dome文件 ...

  3. Idea无法运行Maven项目

    导入项目到tomcat的时候要选择Arifact 如果maven项目没有这个选项, <groupId>com.bihang</groupId> <artifactId&g ...

  4. Java - fail-fast机制

    Java提高篇(三四)-----fail-fast机制 在JDK的Collection中我们时常会看到类似于这样的话: 例如,ArrayList: 注意,迭代器的快速失败行为无法得到保证,因为一般来说 ...

  5. RabbitMQ安装教程

    最近几天在学习Spring Cloud,在学习Spring Cloud Config配置刷新使用Spring Cloud Bus时,其中用到消息代理组件RabbitMQ,在安装RabbitMQ的过程查 ...

  6. MySQL设计之三范式的理解

    转自:https://blog.csdn.net/wangqyoho/article/details/52900585 设计关系数据库时,遵从不同的规范要求,设计出合理的关系型数据库,这些不同的规范要 ...

  7. Linux常用基本命令wc-统计文件的字节,字符,行数

    wc命令 作用:统计文件的字节,单词,行数 用法:wc [option] [file] -c:统计字节 ghostwu@dev:~/linux/uniq$ cat ghostwu.txt 192.16 ...

  8. 启动HDFS时datanode无法启动的坑

    启动HDFS 启动hdfs,进入sbin目录,也可以执行./start-all.sh - $cd /app/hadoop/hadoop-2.2.0/sbin - $./start-dfs.sh 在此之 ...

  9. js-权威指南学习笔记20

    第二十章 客户端存储 1.客户端存储有一下几种形式:Web存储.cookie.IE userData.离线Web应用.Web数据库.文件系统API. 2.Web存储标准所描述的API包含localSt ...

  10. 分布式配置中心 携程(apollo)

    1.传统配置文件与分布式配置文件区别 传统配置文件:如果修改了配置文件,需要重新打包发布,重新发布服务,而且每个环境的变更配置文件,比较繁琐. 分布式配置文件:将配置文件注册到配置中心上去,可以使用分 ...