Dubbo泛化引用
来源:互联网 发布:信息管理系统源码 编辑:程序博客网 时间:2024/06/05 20:58
泛接口调用方式主要用于客户端没有API接口及模型类元的情况,参数及返回值中的所用POJO均使用Map表示,通常用于框架集成,比如:实现一个通用的服务测试框架,可通过GenericService调用所有服务实现。
实现代码如下:
服务器端代码:
package com.yncp.dubbo.entity;import java.io.Serializable;public class Computer implements Serializable{ private static final long serialVersionUID = 1L; private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
package com.yncp.dubbo.entity;import java.io.Serializable;public class User implements Serializable { private static final long serialVersionUID = 1L; private Integer id; private String name; private Computer computer; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Computer getComputer() { return computer; } public void setComputer(Computer computer) { this.computer = computer; }}
package com.yncp.dubbo.service;import com.yncp.dubbo.entity.User;public interface IDubboGenQService { public User queryUser(Integer id); public void saveUser(User user);}
package com.yncp.dubbo.service.impl;import com.yncp.dubbo.entity.Computer;import com.yncp.dubbo.entity.User;import com.yncp.dubbo.service.IDubboGenQService;public class DubboGenQServiceImpl implements IDubboGenQService { public User queryUser(Integer id) { User user=new User(); user.setId(id); user.setName("张三"+id); Computer computer=new Computer(); computer.setId(id); computer.setName("张三的电脑"); user.setComputer(computer); return user; } public void saveUser(User user) { System.out.println("用户保存了"); System.out.println(user.getName()); System.out.println(user.getComputer().getName()); }}
dubbo.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"> <!-- 指定web服务名字 --> <dubbo:application name="DubboGenQ"/> <!-- 声明服务注册中心 --> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/> <!-- 指定传输层通信协议 --> <dubbo:protocol name="dubbo" port="20880"/> <!-- 暴露你的服务地址 --> <dubbo:service ref="dubboGenQService" interface="com.yncp.dubbo.service.IDubboGenQService" protocol="dubbo" cluster="failover" /> </beans>
–客户端实现:
import java.io.IOException;import java.util.HashMap;import java.util.Map;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.alibaba.dubbo.rpc.service.GenericService;public class DubboStart { public static void main(String[] args) throws IOException { ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); GenericService genericService=(GenericService) ctx.getBean("dubboGenQService"); Object resultUser = genericService.$invoke("queryUser", new String[] { "java.lang.Integer"}, new Object[] { 1 }); HashMap<String, Object> userMap=(HashMap<String, Object>) resultUser; for(Map.Entry<String, Object> entry1: userMap.entrySet()){ if(entry1.getValue() instanceof HashMap){ HashMap<String, Object> computerMap=(HashMap<String, Object>) entry1.getValue(); System.out.println(entry1.getKey()); for(Map.Entry<String, Object> entry: computerMap.entrySet()){ System.out.println("\t"+entry.getValue()); } }else{ System.out.println(entry1.getKey()+" "+entry1.getValue()); } } //演示用户数据的封装 Map<String, Object> user = new HashMap<String, Object>(); user.put("name", "张三"); HashMap<String, String> value = new HashMap<String, String>(); value.put("name", "张三的电脑"); user.put("computer", value); genericService.$invoke("saveUser", new String[]{"com.yncp.dubbo.entity.User"}, new Object[]{user}); }}
dubbo。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"> <!-- 指定web服务名字 --> <dubbo:application name="DubboGenQ"/> <!-- 声明服务注册中心 --> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/> <!-- 指定传输层通信协议 因为在同一台机器测试,所以端口不一样--> <dubbo:protocol name="dubbo" port="20881"/> <!-- 暴露你的服务地址 --> <dubbo:reference id="dubboGenQService" interface="com.yncp.dubbo.service.IDubboGenQService" protocol="dubbo" cache="lru" generic="true" /> </beans>
1 0
- Dubbo-泛化引用
- Dubbo泛化引用
- dubbo泛化调用和泛化引用
- 11.dubbo结果缓存、泛化引用、泛化实现
- Dubbo泛化实现
- DUBBO 泛化调用
- dubbo泛化调用
- dubbo泛化处理(转)
- dubbo泛化调用 http接口 随意调用dubbo服务
- 14.2.2泛化的java引用
- Class对象以及泛化的Class引用
- dubbo maven 引用
- dubbo服务的引用
- dubbo发布与引用
- [Dubbo源码分析]Dubbo暴露与引用
- java编程思想学习笔记 泛化的class引用
- hsf dubbo学习六--泛化,回声测试,上下文信息,隐式传参,异步调用,本地调用
- 分享一个自己写的使用dubbo泛化机制+接口编程的消费客户端jar包
- 深入理解Java内存模型
- 更改ProgressDialog的样式
- Windos 7 64位下通过VS2013配置caffe模型,并使用MATLAB调用训练好的ImgaeNet接口做测试
- android activity之间传递对象(Serializable 和 Parcelable)
- Android数据持久化——SharedPreferences
- Dubbo泛化引用
- 在Markdown Pad中输入表格
- 如何在mysql存储过程中处理select语句返回的多行结果(用游标)
- androidの设计的布局在阿拉伯语下界面错乱的解决方法 总汇
- MapReduce Join
- NSInvocation使用
- C/C++网络编程总结
- js 将json字符串转换为json对象的方法解析
- POJ 2750Potted Flower 线段树+dp 动态求环中的最大连续子序列和