Dubbo泛化实现

24 篇文章 0 订阅
21 篇文章 0 订阅

dubbo泛化实现方式主要用于服务器端没有API借口及横型类元的情况,参数及返回值中的所有POJO均使用Map表示,通常用于框架集成。比如:实现一个通用的远程服务,可通过实现GenenricService接口实现所有服务请求。
如如下服务实现:

package com.yncp.dubbo.service;
import java.util.Date;
import com.alibaba.dubbo.rpc.service.GenericException;
import com.alibaba.dubbo.rpc.service.GenericService;

public class IDubboGenService implements GenericService{

    public Object $invoke(String method, String[] parameterTypes, Object[] args)
            throws GenericException { 
        System.out.println("方法:"+method);
        System.out.println("parameterTypes:");
        for (Object type : parameterTypes) {
            System.out.println("\t"+type);
        }
        System.out.println("args:");
        for (Object arg : args) {
            System.out.println("\t"+arg);
        }

        System.out.println("args:"+args);
        return new Date();
    }

}

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="DubboGen"/>
    <!-- 声明服务注册中心 -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
    <!-- 指定传输层通信协议 -->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!-- 暴露你的服务地址 -->
    <dubbo:service 
        ref="dubboGenService" 
        interface="com.yncp.dubbo.service.IDubboGenService"
        protocol="dubbo"
    />
 </beans>

客户端实现代码如下:

package com.yncp.dubbo.entity;

public class User {
    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.service;
import com.yncp.dubbo.entity.User;

public interface IDubboGenService {
    public Object sum(int x,int y);
    public Object saveUser(User user);
}
import java.io.IOException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.yncp.dubbo.entity.User;
import com.yncp.dubbo.service.IDubboGenService;


public class DubboStart {
    public static void main(String[] args) throws IOException {

       ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");

       IDubboGenService dubboGenService=(IDubboGenService) ctx.getBean("dubboGenService");
       Object date= dubboGenService.sum(1, 3);
       System.out.println(date.toString());

       User user=new User();
       user.setId(1);
       user.setName("张三");
       Object obj= dubboGenService.saveUser(user);
       System.out.println(obj.toString());


    }
}

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="DubboGen_ref"/>
    <!-- 声明服务注册中心 -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>    
    <!-- 指定传输层通信协议 -->
    <dubbo:protocol name="dubbo" port="20881"/>
    <dubbo:protocol name="rmi" port="1010"/>    
    <!-- 暴露你的服务地址 -->
    <dubbo:reference 
        id="dubboGenService" 
        interface="com.yncp.dubbo.service.IDubboGenService"
        protocol="dubbo" 
        >
    </dubbo:reference>
 </beans>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值