實現泛化(伺服器端泛化)

實現一個通用的遠端服務 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 日:增強英文文件 (#1798) (95a9f4f6c1c)