From 952621897475ecb50c8eab389e83a4ae4345e7fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=B7?= <10402852@qq.com> Date: Fri, 20 Sep 2024 10:38:48 +0800 Subject: [PATCH] 'commit' --- ds-gw/pom.xml | 7 ++ .../java/com/dsideal/gw/GwApplication.java | 18 ++++- .../com/dsideal/gw/Handler/RouterHandler.java | 71 ++++++++++++++----- .../main/resources/application_dev.properties | 3 + .../main/resources/application_pro.properties | 5 +- .../target/classes/application_dev.properties | 3 + .../target/classes/application_pro.properties | 5 +- 7 files changed, 91 insertions(+), 21 deletions(-) diff --git a/ds-gw/pom.xml b/ds-gw/pom.xml index 0dbcca00..f674c785 100644 --- a/ds-gw/pom.xml +++ b/ds-gw/pom.xml @@ -58,6 +58,13 @@ commons-lang3 3.12.0 + + + + com.squareup.okhttp3 + okhttp + 4.12.0 + diff --git a/ds-gw/src/main/java/com/dsideal/gw/GwApplication.java b/ds-gw/src/main/java/com/dsideal/gw/GwApplication.java index 550e6270..9eade7d0 100644 --- a/ds-gw/src/main/java/com/dsideal/gw/GwApplication.java +++ b/ds-gw/src/main/java/com/dsideal/gw/GwApplication.java @@ -1,14 +1,17 @@ package com.dsideal.gw; - import cn.hutool.core.io.FileUtil; import com.dsideal.gw.Handler.RouterHandler; import com.dsideal.gw.Util.LogBackLogFactory; import com.jfinal.config.*; +import com.jfinal.kit.Prop; import com.jfinal.kit.PropKit; import com.jfinal.server.undertow.UndertowServer; import com.jfinal.template.Engine; + import java.io.File; +import java.util.HashMap; +import java.util.Map; public class GwApplication extends JFinalConfig { @@ -26,13 +29,23 @@ public class GwApplication extends JFinalConfig { /** * 配置常量 */ + //路由表 + public static Map routeDict = new HashMap<>(); + @Override public void configConstant(Constants me) { //使用LogBack me.setLogFactory(new LogBackLogFactory()); //加载配置文件 String configFile = "application_{?}.properties".replace("{?}", getEnvPrefix()); - PropKit.use(configFile); + Prop prop = PropKit.use(configFile); + + // 获取所有配置项,得到路由表 + for (Map.Entry entry : prop.getProperties().entrySet()) { + if (entry.getKey().toString().startsWith("route_")) { + routeDict.put(entry.getKey().toString().substring(6), entry.getValue().toString()); + } + } } /** @@ -69,6 +82,7 @@ public class GwApplication extends JFinalConfig { me.add(new RouterHandler()); } + /** * 在jfinal启动完成后马上执行 */ diff --git a/ds-gw/src/main/java/com/dsideal/gw/Handler/RouterHandler.java b/ds-gw/src/main/java/com/dsideal/gw/Handler/RouterHandler.java index 42279dc1..abf8d8ad 100644 --- a/ds-gw/src/main/java/com/dsideal/gw/Handler/RouterHandler.java +++ b/ds-gw/src/main/java/com/dsideal/gw/Handler/RouterHandler.java @@ -1,18 +1,19 @@ package com.dsideal.gw.Handler; import cn.hutool.json.JSONObject; +import com.dsideal.gw.GwApplication; import com.jfinal.handler.Handler; +import okhttp3.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; -import java.util.HashMap; -import java.util.Map; public class RouterHandler extends Handler { /** * 功能:输出JSON文本串 + * * @param res * @param jo */ @@ -33,29 +34,65 @@ public class RouterHandler extends Handler { renderJson(res, new JSONObject().put("msg", msg)); } + //OkHttp的实例,单例模式 + private static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient(); + + //OkHttp的回调函数,同步执行 + private void executeRequest(Request request, HttpServletResponse res) throws IOException { + Response response = OK_HTTP_CLIENT.newCall(request).execute(); + if (response.isSuccessful()) { + String responseBody = response.body().string(); + renderJson(res, responseBody); + } else { + renderJson(res, "请求失败!"); + } + } + @Override public void handle(String target, HttpServletRequest req, HttpServletResponse res, boolean[] isHandled) { //可以正确获取到URL的完整路径 String servletPath = req.getServletPath(); + String queryString = req.getQueryString(); + + //路由到哪个微服务 + int firstSlashIndex = servletPath.indexOf('/'); // 找到第一个 '/' 的索引 + int secondSlashIndex = servletPath.indexOf('/', firstSlashIndex + 1); // 从第一个 '/' 之后开始找第二个 '/' 的索引 - JSONObject jo = new JSONObject(); - Map _map = new HashMap<>(); - _map.put("servletPath", servletPath); - _map.put("success", true); - jo.putAll(_map); - - if (req.getMethod().equals("GET")) { - renderJson(res, "现在调用是GET!"); - } else if (req.getMethod().equals("POST")) { - renderJson(res, "现在调用是POST!"); - } else if (req.getContentType() != null && req.getContentType().contains("multipart/form-data")) { - renderJson(res, "现在调用是上传操作!"); + if (firstSlashIndex != -1 && secondSlashIndex != -1) { + String prefix = servletPath.substring(firstSlashIndex + 1, secondSlashIndex); // 截取两个 '/' 之间的内容 + if (!GwApplication.routeDict.containsKey(prefix)) { + renderJson(res, prefix + "前缀没有找到合适的路由表,请检查是否请求正确!"); + } else { + //路由到哪个微服务 + String FORWARD_URL = GwApplication.routeDict.get(prefix); + //处理GET请求 + if (req.getMethod().equals("GET")) { + //参数:queryString + Request request; + if (queryString != null) { + request = new Request.Builder().url(FORWARD_URL + "?" + queryString).get().build(); + } else { + request = new Request.Builder().url(FORWARD_URL).get().build(); + } + try { + executeRequest(request, res); + } catch (IOException e) { + renderJson(res, "请求失败!"+ e); + } + }//处理POST请求 + else if (req.getMethod().equals("POST")) { + // RequestBody body = RequestBody.create(jsonBody, MediaType.parse("application/json; charset=utf-8")); + // Request request = new Request.Builder().url(FORWARD_URL).post(body).build(); + } else { + // MinIO - 服务端签名直传(前端 + 后端 + 效果演示) + //https://blog.csdn.net/CYK_byte/article/details/140254412 + renderJson(res, "系统只支持GET,POST,其它的OPTIONS不支持!文件上传请使用S3协议进行直传,不通过JAVA处理,JAVA只处理授权!"); + } + } } else { - renderJson(res, "系统只支持GET,POST和文件上传,其它的OPTIONS不允许!"); + renderJson(res, "输入的字符串格式不正确,没有找到两个 '/'。"); } - //输出结果 - renderJson(res, jo); //停止filter isHandled[0] = true; } diff --git a/ds-gw/src/main/resources/application_dev.properties b/ds-gw/src/main/resources/application_dev.properties index 47201e3c..8e397d08 100644 --- a/ds-gw/src/main/resources/application_dev.properties +++ b/ds-gw/src/main/resources/application_dev.properties @@ -15,3 +15,6 @@ excelExportTemplatePathSuffix=/ExcelExportTemplate/ # 导入excel 的模板配置路径 ExcelImportTemplatePathSuffix=/ExcelImportTemplate/ +# 基础数据 +route_dsBase= http://ds-base:8001 + diff --git a/ds-gw/src/main/resources/application_pro.properties b/ds-gw/src/main/resources/application_pro.properties index 62447393..4783d4b7 100644 --- a/ds-gw/src/main/resources/application_pro.properties +++ b/ds-gw/src/main/resources/application_pro.properties @@ -13,4 +13,7 @@ CookieMd5SingPwd=DsideaL4r5t6y7u # 导出excel 的模板配置路径 excelExportTemplatePathSuffix=/ExcelExportTemplate/ # 导入excel 的模板配置路径 -ExcelImportTemplatePathSuffix=/ExcelImportTemplate/ \ No newline at end of file +ExcelImportTemplatePathSuffix=/ExcelImportTemplate/ + +# 基础数据 +route_dsBase= http://ds-base:8001 \ No newline at end of file diff --git a/ds-gw/target/classes/application_dev.properties b/ds-gw/target/classes/application_dev.properties index 47201e3c..8e397d08 100644 --- a/ds-gw/target/classes/application_dev.properties +++ b/ds-gw/target/classes/application_dev.properties @@ -15,3 +15,6 @@ excelExportTemplatePathSuffix=/ExcelExportTemplate/ # 导入excel 的模板配置路径 ExcelImportTemplatePathSuffix=/ExcelImportTemplate/ +# 基础数据 +route_dsBase= http://ds-base:8001 + diff --git a/ds-gw/target/classes/application_pro.properties b/ds-gw/target/classes/application_pro.properties index 62447393..4783d4b7 100644 --- a/ds-gw/target/classes/application_pro.properties +++ b/ds-gw/target/classes/application_pro.properties @@ -13,4 +13,7 @@ CookieMd5SingPwd=DsideaL4r5t6y7u # 导出excel 的模板配置路径 excelExportTemplatePathSuffix=/ExcelExportTemplate/ # 导入excel 的模板配置路径 -ExcelImportTemplatePathSuffix=/ExcelImportTemplate/ \ No newline at end of file +ExcelImportTemplatePathSuffix=/ExcelImportTemplate/ + +# 基础数据 +route_dsBase= http://ds-base:8001 \ No newline at end of file