Dubbo泛化如何引用
这篇文章主要介绍“Dubbo泛化如何引用”,在日常操作中,相信很多人在Dubbo泛化如何引用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Dubbo泛化如何引用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于做网站、成都网站设计、洛浦网络推广、重庆小程序开发、洛浦网络营销、洛浦企业策划、洛浦品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联公司为所有大学生创业者提供洛浦建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com
泛接口调用方式主要用于客户端没有API接口及模型类元的情况,参数及返回值中的所用POJO均使用Map表示,通常用于框架集成,比如:实现一个通用的服务测试框架,可通过GenericService调用所有服务实现。
实现代码如下:
服务器端代码:
package com.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.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.dubbo.service;
import com.dubbo.entity.User;
public interface IDubboGenQService {
public User queryUser(Integer id);
public void saveUser(User user);
}
package com.dubbo.service.impl;
import com.dubbo.entity.Computer;
import com.dubbo.entity.User;
import com.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配置:
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"> ref="dubboGenQService" interface="com.dubbo.service.IDubboGenQService" protocol="dubbo" cluster="failover" />
dubbo 提供者启动
public class dubboServerStart {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo.xml");
context.start();
System.in.read();
}
}
客户端实现:
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 DubboClientStart {
public static void main(String[] args) throws IOException {
ApplicationContext ctx=new ClassPathXmlApplicationContext("dubbo.xml");
GenericService genericService=(GenericService) ctx.getBean("dubboGenQService");
Object resultUser = genericService.$invoke("queryUser",
new String[] { "java.lang.Integer"},
new Object[] { 1 });
HashMap
for(Map.Entry
if(entry1.getValue() instanceof HashMap){
HashMap
System.out.println(entry1.getKey());
for(Map.Entry
System.out.println("\t"+entry.getValue());
}
}else{
System.out.println(entry1.getKey()+" "+entry1.getValue());
}
}
//演示用户数据的封装
Map
user.put("name", "张三");
HashMap
value.put("name", "张三的电脑");
user.put("computer", value);
genericService.$invoke("saveUser", new String[]{"com.dubbo.entity.User"},
new Object[]{user});
}
}
dubbo.xml配置:
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"> id="dubboGenQService" interface="com.dubbo.service.IDubboGenQService" protocol="dubbo" cache="lru" generic="true" />
到此,关于“Dubbo泛化如何引用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!
当前标题:Dubbo泛化如何引用
转载源于:http://azwzsj.com/article/jpohic.html