使用泛化调用
泛化接口调用方式主要用于客户端没有 API 接口及模型类元的情况,参数及返回值中的所有 POJO 均用 Map 表示,通常用于框架集成,比如:实现一个通用的服务测试框架,可通过 GenericService 调用所有服务实现。
通过 Spring 使用泛化调用
在 Spring 配置申明 generic="true" :
在 Java 代码获取 barService 并开始泛化调用:
GenericService barService = (GenericService) applicationContext.getBean("barService");
Object result = barService.$invoke("sayHello", new String[] { "java.lang.String" }, new Object[] { "World" });
通过 API 方式使用泛化调用
import com.alibaba.dubbo.rpc.service.GenericService; ...
// 引用远程服务
// 该实例很重量,里面封装了所有与注册中心及服务提供方连接,请缓存
ReferenceConfig reference = new ReferenceConfig();
// 弱类型接口名
reference.setInterface("com.xxx.XxxService");
reference.setVersion("1.0.0");
// 声明为泛化接口
reference.setGeneric(true);
// 用com.alibaba.dubbo.rpc.service.GenericService可以替代所有接口引用
GenericService genericService = reference.get();
// 基本类型以及Date,List,Map等不需要转换,直接调用
Object result = genericService.$invoke("sayHello", new String[] {"java.lang.String"}, new Object[] {"world"});
// 用Map表示POJO参数,如果返回值为POJO也将自动转成Map
Map person = new HashMap();
person.put("name", "xxx");
person.put("password", "yyy");
// 如果返回POJO将自动转成
Map Object result = genericService.$invoke("findPerson", new String[] {"com.xxx.Person"}, new Object[]{person});
有关泛化类型的进一步解释
假设存在 POJO 如:
package com.xxx;
public class PersonImpl implements Person {
private String name;
private String password;
public String getName() {
return name;
public void setName(String name) {
this.name = name;
public String getPassword() {
return password;
public void setPassword(String password) {
this.password = password;
则 POJO 数据:
Person person = new PersonImpl();
person.setName("xxx");
person.setPassword("yyy");
可用下面 Map 表示:
Map map = new HashMap();
// 注意:如果参数类型是接口,或者List等丢失泛型,可通过class属性指定类型。 map.put("class", "com.xxx.PersonImpl");
map.put("name", "xxx");
map.put("password", "yyy");
实现泛化调用
泛接口实现方式主要用于服务器端没有API接口及模型类元的情况,参数及返回值中的所有 POJO均用Map表示,通常用于框架集成,比如:实现一个通用的远程服务Mock框架,可通 过实现GenericService接口处理所有服务请求。
在 Java 代码中实现 GenericService 接口:
package com.foo;
public class MyGenericService implements GenericService {
public Object $invoke(String methodName, String[] parameterTypes, Object[] args) throwsGenericException {
if ("sayHello".equals(methodName)) {
return "Welcome " + args[0];
通过 Spring 暴露泛化实现
在 Spring 配置申明服务的实现:
通过 API 方式暴露泛化实现
// 用com.alibaba.dubbo.rpc.service.GenericService可以替代所有接口实现
GenericService xxxService = new XxxGenericService();
// 该实例很重量,里面封装了所有与注册中心及服务提供方连接,请缓存
ServiceConfig service = new ServiceConfig();
// 弱类型接口名
service.setInterface("com.xxx.XxxService");
service.setVersion("1.0.0");
// 指向一个通用服务实现
service.setRef(xxxService);
// 暴露及注册服务
service.export();
回声测试
回声测试用于检测服务是否可用,回声测试按照正常请求流程执行,能够测试整个调用是否 通畅,可用于监控。
所有服务自动实现 EchoService 接口,只需将任意服务引用强制转型为 EchoService ,即可 使用。
Spring 配置:
代码:
// 远程服务引用
MemberService memberService = ctx.getBean("memberService");
EchoService echoService = (EchoService) memberService; // 强制转型为EchoService
// 回声测试可用性
String status = echoService.$echo("OK");
assert(status.equals("OK"));
特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.