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
原创粉丝点击