欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

dubbo轉(zhuǎn)http方式調(diào)用方式

 更新時間:2025年10月22日 08:35:42   作者:安之若素^  
當(dāng)前項(xiàng)目計(jì)劃去掉網(wǎng)關(guān)層,前端將直接通過HTTP調(diào)用后端dubbo服務(wù),為此,后端需新增controller層,實(shí)現(xiàn)請求兼容和統(tǒng)一轉(zhuǎn)發(fā),保證前端調(diào)用方式不變

業(yè)務(wù)背景

在當(dāng)前項(xiàng)目下,所有前端請求均通過外層網(wǎng)關(guān)轉(zhuǎn)發(fā)到后端這邊的dubbo服務(wù),現(xiàn)計(jì)劃去掉網(wǎng)關(guān)層,由前端直接http調(diào)用后端dubbo。

解決方案

在前端調(diào)用方式不變的前提下,后端服務(wù)新建controller層(原后端服務(wù)無任何controller),做統(tǒng)一請求兼容轉(zhuǎn)發(fā):

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調(diào)用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.解析傳入?yún)?shù)
            String paramStr = this.parseInputStream(request);
            // 獲取目標(biāo)接口的 Class 對象
            Class<?> serviceInterface = Class.forName(service);

            // 獲取方法的參數(shù)類型
            String[] parameterTypes = this.getMethodParameterTypes(serviceInterface, method);

            // 將 JSON 字符串轉(zhuǎn)換為目標(biāo)參數(shù)值
            Object[] parameters = this.convertParameters(paramStr, parameterTypes);

            // 調(diào)用方法
            result = tarService.$invoke(method, parameterTypes, parameters);

        } catch (Exception e) {
            log.error("服務(wù)調(diào)用失敗, service={},method={}",service, method, e);
            throw new ErrorCodeException("000000", "系統(tǒng)異常,請稍后重試");
        }
        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("請求報文解析異常");
        }
    }

    /**
     * 獲取目標(biāo)dubbo服務(wù)
     * @params [service]
     * @return org.apache.dubbo.rpc.service.GenericService
     * @desc
     */
    private GenericService getTargetService(String service){
        // 應(yīng)用信息
        ApplicationConfig application = SpringBeanUtils.getBean(ApplicationConfig.class);
        // 注冊中心發(fā)現(xiàn)
        RegistryConfig registry = SpringBeanUtils.getBean(RegistryConfig.class);
        // 引用遠(yuǎn)程服務(wù)
        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 字符串轉(zhuǎn)換為目標(biāo)參數(shù)值
     * @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;
    }

    /*
     * 獲取目標(biāo)方法的參數(shù)類型
     * @params [serviceInterface, methodName]
     * @return java.lang.String[]
     * @desc
     */
    public String[] getMethodParameterTypes(Class<?> serviceInterface, String methodName) {
        for (Method method : serviceInterface.getMethods()) {
            // 由于不支持重載,因此這里可以根據(jù) 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);
    }
}

總結(jié)

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解MyBatis XML配置解析

    詳解MyBatis XML配置解析

    這篇文章主要介紹了詳解MyBatis XML配置解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java編程實(shí)現(xiàn)從給定范圍內(nèi)隨機(jī)N個不重復(fù)數(shù)生成隨機(jī)數(shù)的方法小結(jié)

    Java編程實(shí)現(xiàn)從給定范圍內(nèi)隨機(jī)N個不重復(fù)數(shù)生成隨機(jī)數(shù)的方法小結(jié)

    這篇文章主要介紹了Java編程實(shí)現(xiàn)從給定范圍內(nèi)隨機(jī)N個不重復(fù)數(shù)生成隨機(jī)數(shù)的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了java根據(jù)指定范圍生成不重復(fù)隨機(jī)數(shù)的相關(guān)操作技巧,需要的朋友可以參考下
    2017-04-04
  • Activiti開發(fā)環(huán)境的配置

    Activiti開發(fā)環(huán)境的配置

    本篇文章主要內(nèi)容介紹了Activiti開發(fā)環(huán)境的配置,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • java設(shè)計(jì)模式之單例模式

    java設(shè)計(jì)模式之單例模式

    這篇文章主要為大家詳細(xì)介紹了java設(shè)計(jì)模式之單例模式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • springboot整合mybatis流程詳解

    springboot整合mybatis流程詳解

    這篇文章主要為大家詳細(xì)介紹了springboot整合mybatisplus的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-05-05
  • JavaGUI模仿QQ聊天功能完整版

    JavaGUI模仿QQ聊天功能完整版

    這篇文章主要為大家詳細(xì)介紹了JavaGUI模仿QQ聊天功能完整版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • 使用RestTemplate 調(diào)用遠(yuǎn)程接口上傳文件方式

    使用RestTemplate 調(diào)用遠(yuǎn)程接口上傳文件方式

    這篇文章主要介紹了使用RestTemplate 調(diào)用遠(yuǎn)程接口上傳文件方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Kubernetes k8s集群之包管理器Helm方式

    Kubernetes k8s集群之包管理器Helm方式

    這篇文章主要介紹了Kubernetes k8s集群之包管理器Helm方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Java中Map集合的常用方法(非常詳細(xì)!)

    Java中Map集合的常用方法(非常詳細(xì)!)

    Java中的Map是一種鍵值對存儲的數(shù)據(jù)結(jié)構(gòu),它提供了快速查找和訪問數(shù)據(jù)的能力,下面這篇文章主要給大家介紹了關(guān)于Java中Map集合的常用方法,需要的朋友可以參考下
    2024-01-01
  • Java?web開發(fā)環(huán)境的搭建超完整步驟

    Java?web開發(fā)環(huán)境的搭建超完整步驟

    這篇文章主要介紹了如何安裝和配置IDEA?2020.1.1?X64版本軟件,包括創(chuàng)建Java?Web項(xiàng)目、配置Tomcat、部署Tomcat?API以及創(chuàng)建和配置Servlet,通過這些步驟,新手可以快速搭建起Javaweb開發(fā)環(huán)境,需要的朋友可以參考下
    2024-11-11

最新評論