dubbo转http方式调用

发布于:2025-02-26 ⋅ 阅读:(18) ⋅ 点赞:(0)

业务背景:在当前项目下,所有前端请求均通过外层网关转发到后端这边的dubbo服务,现计划去掉网关层,由前端直接http调用后端dubbo。

解决方案:在前端调用方式不变的前提下,后端服务新建controller层(原后端服务无任何controller),做统一请求兼容转发

package cn.***********.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.utils.ReferenceConfigCache;
import org.apache.dubbo.rpc.service.GenericService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.List;
import java.util.Map;

/**
 * Dubbo调用controller
 * @Version:1.0
 * @Desc
 */
@Slf4j
@RestController
@RequestMapping("/api/test/")
public class GenericController {

    @PostMapping("/{service}/{method}")
    public Object invoke(HttpServletRequest request,
                         @PathVariable("service") String service,
                         @PathVariable("method") String method) {
        Object result;
        try {
            GenericService tarService = this.getTargetService(service);
            if(tarService ==  null){
                throw new RuntimeException("获取service失败");
            }
            // 1.解析传入参数
            String paramStr = this.parseInputStream(request);
            // 获取目标接口的 Class 对象
            Class<?> serviceInterface = Class.forName(service);

            // 获取方法的参数类型
            String[] parameterTypes = this.getMethodParameterTypes(serviceInterface, method);

            // 将 JSON 字符串转换为目标参数值
            Object[] parameters = this.convertParameters(paramStr, parameterTypes);

            // 调用方法
            result = tarService.$invoke(method, parameterTypes, parameters);

        } catch (Exception e) {
            log.error("服务调用失败, service={},method={}",service, method, e);
            throw new ErrorCodeException("000000", "系统异常,请稍后重试");
        }
        return result;
    }

    /**
     * 获取请求文本
     *
     * @return
     */
    public String parseInputStream(HttpServletRequest request) {
        try {
            ServletInputStream inputStream = request.getInputStream();
            StringBuilder content = new StringBuilder();
            int size = request.getContentLength();
            byte[] b = new byte[size];
            int lens;
            while ((lens = inputStream.read(b)) > 0) {
                content.append(new String(b, 0, lens, "utf-8"));
            }
            return content.toString();
        } catch (IOException e) {
            log.error("请求报文解析失败", e);
            throw new RuntimeException("请求报文解析异常");
        }
    }

    /**
     * 获取目标dubbo服务
     * @params [service]
     * @return org.apache.dubbo.rpc.service.GenericService
     * @desc
     */
    private GenericService getTargetService(String service){
        // 应用信息
        ApplicationConfig application = SpringBeanUtils.getBean(ApplicationConfig.class);
        // 注册中心发现
        RegistryConfig registry = SpringBeanUtils.getBean(RegistryConfig.class);
        // 引用远程服务
        ReferenceConfig<GenericService> reference = new ReferenceConfig<>();
        reference.setApplication(application);
        reference.setRegistry(registry);
        reference.setProtocol("dubbo");
        reference.setInterface(service);
        reference.setTimeout(10000);
        reference.setRetries(0);
        // 声明为泛化接口
        reference.setGeneric(true);
        ReferenceConfigCache cache = ReferenceConfigCache.getCache();
        GenericService genericService = cache.get(reference);
        if(genericService == null){
            genericService = reference.get();
        }
        return genericService;
    }

    /**
     * 将 JSON 字符串转换为目标参数值
     * @params [paramStr, parameterTypes]
     * @return java.lang.Object[]
     * @author wu.zeng
     * @date 2025/2/21 13:44
     * @desc
     */
    private Object[] convertParameters(String paramStr, String[] parameterTypes) {
        Object[] parameters = new Object[parameterTypes.length];
        for (int i = 0; i < parameterTypes.length; i++) {
            String paramType = parameterTypes[i];
            switch (paramType) {
                case "java.lang.String":
                    parameters[i] = paramStr;
                    break;
                case "java.lang.Integer":
                    parameters[i] = Integer.parseInt(paramStr);
                    break;
                case "java.lang.Long":
                    parameters[i] = Long.parseLong(paramStr);
                    break;
                case "java.util.Map":
                    parameters[i] = JSON.parseObject(paramStr, new TypeReference<Map<String, Object>>() {});
                    break;
                case "java.util.List":
                    parameters[i] = JSON.parseObject(paramStr, new TypeReference<List<Object>>() {});
                    break;
                default:
                    // 如果是自定义类型,使用反射或 JSON 反序列化
                    try {
                        Class<?> clazz = Class.forName(paramType);
                        parameters[i] = JSON.parseObject(paramStr, clazz);
                    } catch (ClassNotFoundException e) {
                        throw new RuntimeException("不支持的类型: " + paramType, e);
                    }
            }
        }
        return parameters;
    }

    /*
     * 获取目标方法的参数类型
     * @params [serviceInterface, methodName]
     * @return java.lang.String[]
     * @desc
     */
    public String[] getMethodParameterTypes(Class<?> serviceInterface, String methodName) {
        for (Method method : serviceInterface.getMethods()) {
            // 由于不支持重载,因此这里可以根据 methodName 来匹配方法
            if (method.getName().equals(methodName)) {
                Parameter[] parameters = method.getParameters();
                String[] parameterTypes = new String[parameters.length];
                for (int i = 0; i < parameters.length; i++) {
                    parameterTypes[i] = parameters[i].getType().getName();
                }
                return parameterTypes;
            }
        }
        throw new RuntimeException("方法未找到: " + methodName);
    }
}