dubbo转http方式调用

server/2025/2/26 1:11:19/

业务背景:在当前项目下,所有前端请求均通过外层网关转发到后端这边的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);}
}


http://www.ppmy.cn/server/170651.html

相关文章

在windows下安装windows+Ubuntu16.04双系统(下)

这篇文章的内容主要来源于这篇文章&#xff0c;为正式安装windowsUbuntu16.04双系统部分。在正式安装前&#xff0c;若还没有进行前期准备工作&#xff08;1.分区2.制作启动u盘&#xff09;&#xff0c;见《在windows下安装windowsUbuntu16.04双系统(上)》 二、正式安装Ubuntu …

数据结构:双链表list

list 是 C 标准库中的双向链表容器。 list初始化示例&#xff1a; #include <list>int n 7;std::list<int> lst; // 初始化一个空的双向链表 lststd::list<int> lst(n); // 初始化一个大小为 n 的链表 lst&#xff0c;链表中的值默认都为 0std::list<i…

Adpative Cursor Sharing引发的Oracle故障案例

&#x1f4e2;&#x1f4e2;&#x1f4e2;&#x1f4e3;&#x1f4e3;&#x1f4e3; 作者&#xff1a;IT邦德 中国DBA联盟(ACDU)成员&#xff0c;10余年DBA工作经验 Oracle、PostgreSQL ACE CSDN博客专家及B站知名UP主&#xff0c;全网粉丝10万 擅长主流Oracle、MySQL、PG、高斯…

【Python】打造自己的HTTP server

词汇汇总 CRLF 指的是换行和回车\r\n 教程 ./your_program.sh #启动自己的服务curl -v http://localhost:4221#开启另一个终端 测试HTTP response An HTTP response is made up of three parts, each separated by a CRLF (\r\n): Status line. Zero or more headers, each …

大语言模型(LLM)微调技术笔记

图1&#xff1a;大模型进化树2 大模型微调 在预训练后&#xff0c;大模型可以获得解决各种任务的通用能力。然而&#xff0c;越来越多的研究表明&#xff0c;大语言模型的能力可以根据特定目标进一步调整。 这就是微调技术&#xff0c;目前主要有两种微调大模型的方法1&…

DeepSeek 全面分析报告

引言 DeepSeek 是一款由中国人工智能初创公司 DeepSeek 开发的大型语言模型 (LLM)&#xff0c;于 2025 年 1 月发布&#xff0c;迅速成为全球人工智能领域的一匹黑马。DeepSeek 不仅在性能上可与 OpenAI、Google 等巨头的模型相媲美&#xff0c;而且其训练成本和运行效率都显著…

C++ 设计模式-模板方法模式

文件处理 #include <iostream>// 抽象基类&#xff1a;定义模板方法和抽象步骤 class DataProcessor { public:// 模板方法&#xff08;固定流程&#xff09;void Process() {OpenFile();ProcessData(); // 由子类实现CloseFile();}protected:virtual void ProcessData…

UE5网络通信架构解析

文章目录 前言一、客户端-服务器架构&#xff08;C/S Model&#xff09;二、对等网络架构&#xff08;P2P&#xff0c;非原生支持&#xff09;三、混合架构&#xff08;自定义扩展&#xff09;四、UE5网络核心机制 前言 UE5的网络通信主要基于客户端-服务器&#xff08;C/S&am…