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>