实现泛化(服务器端泛化)

实现一个通用的远程服务 Mock 框架,可以通过实现 GenericService 接口来处理所有服务请求

功能描述

通用接口实现方法主要用于服务器端没有 API 接口和模型分类器的情况。参数和返回值中的所有 POJO 都用 Map 表示,通常用于框架集成。例如,要实现一个通用的远程服务 Mock 框架,可以 通过实现 GenericService 接口来处理所有服务请求。

使用场景

如何使用

在 Java 代码中实现 GenericService 接口

package com.foo;
public class MyGenericService implements GenericService {
 
    public Object $invoke(String methodName, String[] parameterTypes, Object[] args) throws GenericException {
        if ("sayHello".equals(methodName)) {
            return "Welcome " + args[0];
        }
    }
}

通过 Spring 暴露泛化实现

在 Spring 配置中声明服务的实现

<bean id="genericService" class="com. foo. MyGenericService" />
<dubbo:service interface="com.foo.BarService" ref="genericService" />

通过 API 暴露泛化实现

...
// Use org.apache.dubbo.rpc.service.GenericService to replace all interface implementations
GenericService xxxService = new XxxGenericService();

// This instance is very heavy, and it encapsulates all connections with the registry and service providers, please cache
ServiceConfig<GenericService> service = new ServiceConfig<GenericService>();
// Weakly typed interface name
service.setInterface("com.xxx.XxxService");
service.setVersion("1.0.0");
// point to a generic service implementation
service.setRef(xxxService);
 
// expose and register services
service. export();

最后修改时间:2023 年 1 月 2 日:增强 en 文档 (#1798) (95a9f4f6c1c)