diff --git a/dsRes/src/main/java/com/dsideal/resource/Config/GatewayConfig.java b/dsRes/src/main/java/com/dsideal/resource/Config/GatewayConfig.java new file mode 100644 index 00000000..c564a63a --- /dev/null +++ b/dsRes/src/main/java/com/dsideal/resource/Config/GatewayConfig.java @@ -0,0 +1,23 @@ +package com.dsideal.resource.Config; + +import com.dsideal.resource.ResApplication; +import com.jfinal.kit.Prop; + +public class GatewayConfig { + private static final Prop prop = ResApplication.PropKit; + + // 超时配置 + public static final int CONNECT_TIMEOUT = prop.getInt("gateway.timeout.connect", 10000); + public static final int READ_TIMEOUT = prop.getInt("gateway.timeout.read", 30000); + public static final int WRITE_TIMEOUT = prop.getInt("gateway.timeout.write", 30000); + + // 连接池配置 + public static final int MAX_CONNECTIONS = prop.getInt("gateway.connection.max", 5); + public static final int KEEP_ALIVE_DURATION = prop.getInt("gateway.connection.keep-alive", 300); + + // 安全配置 + public static final String[] ALLOWED_ORIGINS = prop.get("gateway.security.cors.allowed-origins", "*").split(","); + public static final String[] ALLOWED_METHODS = prop.get("gateway.security.cors.allowed-methods", "GET,POST,OPTIONS").split(","); + public static final String[] ALLOWED_HEADERS = prop.get("gateway.security.cors.allowed-headers", "Content-Type,Authorization,Cookie").split(","); + public static final boolean ALLOW_CREDENTIALS = prop.getBoolean("gateway.security.cors.allow-credentials", true); + public static final int MAX_AGE = prop.getInt("gateway.security.cors.max-age", 3600);} \ No newline at end of file diff --git a/dsRes/src/main/java/com/dsideal/resource/ResApplication.java b/dsRes/src/main/java/com/dsideal/resource/ResApplication.java index cbf7fe91..929e58cb 100644 --- a/dsRes/src/main/java/com/dsideal/resource/ResApplication.java +++ b/dsRes/src/main/java/com/dsideal/resource/ResApplication.java @@ -38,14 +38,16 @@ public class ResApplication extends JFinalConfig { */ public static Prop PropKit; - @Override - public void configConstant(Constants me) { - //使用LogBack - me.setLogFactory(new LogBackLogFactory()); + static { //加载配置文件 String configFile = "application_{?}.yaml".replace("{?}", getEnvPrefix()); PropKit = new YamlProp(configFile); + } + @Override + public void configConstant(Constants me) { + //使用LogBack + me.setLogFactory(new LogBackLogFactory()); // 设置静态根目录为上传根目录 me.setBaseUploadPath(PropKit.get("uploadTempPath")); } diff --git a/dsRes/src/main/java/com/dsideal/resource/Util/HttpClient.java b/dsRes/src/main/java/com/dsideal/resource/Util/HttpClient.java new file mode 100644 index 00000000..17d84a96 --- /dev/null +++ b/dsRes/src/main/java/com/dsideal/resource/Util/HttpClient.java @@ -0,0 +1,181 @@ +package com.dsideal.resource.Util; + +import com.dsideal.resource.Config.GatewayConfig; +import okhttp3.*; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +public class HttpClient { + private static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient.Builder() + .connectTimeout(GatewayConfig.CONNECT_TIMEOUT, TimeUnit.MILLISECONDS) + .readTimeout(GatewayConfig.READ_TIMEOUT, TimeUnit.MILLISECONDS) + .writeTimeout(GatewayConfig.WRITE_TIMEOUT, TimeUnit.MILLISECONDS) + .connectionPool(new ConnectionPool(GatewayConfig.MAX_CONNECTIONS, + GatewayConfig.KEEP_ALIVE_DURATION, + TimeUnit.SECONDS)) + .build(); + + /** + * 发送GET请求 + * + * @param url 请求URL + * @param headers 请求头 + * @return 响应字符串 + */ + public static String get(String url, Map headers) { + Request.Builder builder = new Request.Builder().url(url); + + // 添加请求头 + if (headers != null) { + headers.forEach(builder::addHeader); + } + + try (Response response = OK_HTTP_CLIENT.newCall(builder.build()).execute()) { + if (!response.isSuccessful()) { + throw new IOException("请求失败: " + response); + } + ResponseBody body = response.body(); + return body != null ? body.string() : null; + } catch (IOException e) { + throw new RuntimeException("GET请求异常: " + url, e); + } + } + + /** + * 发送POST请求(JSON格式) + * + * @param url 请求URL + * @param jsonBody JSON请求体 + * @param headers 请求头 + * @return 响应字符串 + */ + public static String postJson(String url, String jsonBody, Map headers) { + MediaType JSON = MediaType.parse("application/json; charset=utf-8"); + RequestBody body = RequestBody.create(jsonBody, JSON); + + Request.Builder builder = new Request.Builder() + .url(url) + .post(body); + + // 添加请求头 + if (headers != null) { + headers.forEach(builder::addHeader); + } + + try (Response response = OK_HTTP_CLIENT.newCall(builder.build()).execute()) { + if (!response.isSuccessful()) { + throw new IOException("请求失败: " + response); + } + ResponseBody responseBody = response.body(); + return responseBody != null ? responseBody.string() : null; + } catch (IOException e) { + throw new RuntimeException("POST请求异常: " + url, e); + } + } + + /** + * 发送POST请求(表单格式) + * + * @param url 请求URL + * @param formData 表单数据 + * @param headers 请求头 + * @return 响应字符串 + */ + public static String postForm(String url, Map formData, Map headers) { + FormBody.Builder formBuilder = new FormBody.Builder(); + + // 添加表单数据 + if (formData != null) { + formData.forEach(formBuilder::add); + } + + Request.Builder builder = new Request.Builder() + .url(url) + .post(formBuilder.build()); + + // 添加请求头 + if (headers != null) { + headers.forEach(builder::addHeader); + } + + try (Response response = OK_HTTP_CLIENT.newCall(builder.build()).execute()) { + if (!response.isSuccessful()) { + throw new IOException("请求失败: " + response); + } + ResponseBody responseBody = response.body(); + return responseBody != null ? responseBody.string() : null; + } catch (IOException e) { + throw new RuntimeException("POST请求异常: " + url, e); + } + } + + /** + * 发送POST请求(文件上传) + * + * @param url 请求URL + * @param file 文件 + * @param fileName 文件名 + * @param headers 请求头 + * @return 响应字符串 + */ + public static String postFile(String url, File file, String fileName, Map headers) { + RequestBody requestBody = new MultipartBody.Builder() + .setType(MultipartBody.FORM) + .addFormDataPart("file", fileName, + RequestBody.create(file, MediaType.parse("application/octet-stream"))) + .build(); + + Request.Builder builder = new Request.Builder() + .url(url) + .post(requestBody); + + // 添加请求头 + if (headers != null) { + headers.forEach(builder::addHeader); + } + + try (Response response = OK_HTTP_CLIENT.newCall(builder.build()).execute()) { + if (!response.isSuccessful()) { + throw new IOException("请求失败: " + response); + } + ResponseBody responseBody = response.body(); + return responseBody != null ? responseBody.string() : null; + } catch (IOException e) { + throw new RuntimeException("文件上传异常: " + url, e); + } + } + + public static void main(String[] args) { + // 设置请求头 + Map headers = new HashMap<>(); + headers.put("Authorization", "Bearer token123"); + headers.put("Content-Type", "application/json"); + + // GET请求示例 + String getUrl = "https://api.example.com/users"; + String getResponse = HttpClient.get(getUrl, headers); + System.out.println("GET Response: " + getResponse); + + // POST JSON请求示例 + String postUrl = "https://api.example.com/users"; + String jsonBody = "{\"name\":\"John\",\"age\":30}"; + String postResponse = HttpClient.postJson(postUrl, jsonBody, headers); + System.out.println("POST Response: " + postResponse); + + // POST表单请求示例 + Map formData = new HashMap<>(); + formData.put("username", "john"); + formData.put("password", "123456"); + String formResponse = HttpClient.postForm(postUrl, formData, headers); + System.out.println("Form Response: " + formResponse); + + // 文件上传示例 + File file = new File("test.txt"); + String fileResponse = HttpClient.postFile(postUrl, file, "test.txt", headers); + System.out.println("File Upload Response: " + fileResponse); + } +} \ No newline at end of file diff --git a/dsRes/src/main/resources/application_dev.yaml b/dsRes/src/main/resources/application_dev.yaml index fae44304..04631291 100644 --- a/dsRes/src/main/resources/application_dev.yaml +++ b/dsRes/src/main/resources/application_dev.yaml @@ -25,6 +25,28 @@ minio: secretKey: EiLaKLpLW6OHmjPxvMLBD11Zu3xtV1tdZU9PFVYO bucketName: dsideal url: http://10.10.14.210:9000/dsideal + +# 网关配置 +gateway: + # 超时配置(毫秒) + timeout: + connect: 10000 + read: 30000 + write: 30000 + + # 连接池配置 + connection: + max: 5 + keep-alive: 300 + + # 安全配置 + security: + cors: + allowed-origins: "*" # 或者具体的域名列表,如 "http://localhost:8080,https://your-domain.com" + allowed-methods: "GET,POST,OPTIONS" + allowed-headers: "Content-Type,Authorization,Cookie" + allow-credentials: true + max-age: 3600 # ============================================================== excel: diff --git a/dsRes/src/main/resources/application_pro.yaml b/dsRes/src/main/resources/application_pro.yaml index 0437b7e0..011f3f0b 100644 --- a/dsRes/src/main/resources/application_pro.yaml +++ b/dsRes/src/main/resources/application_pro.yaml @@ -25,6 +25,28 @@ minio: secretKey: EiLaKLpLW6OHmjPxvMLBD11Zu3xtV1tdZU9PFVYO bucketName: dsideal url: http://10.10.14.210:9000/dsideal + +# 网关配置 +gateway: + # 超时配置(毫秒) + timeout: + connect: 10000 + read: 30000 + write: 30000 + + # 连接池配置 + connection: + max: 5 + keep-alive: 300 + + # 安全配置 + security: + cors: + allowed-origins: "*" # 或者具体的域名列表,如 "http://localhost:8080,https://your-domain.com" + allowed-methods: "GET,POST,OPTIONS" + allowed-headers: "Content-Type,Authorization,Cookie" + allow-credentials: true + max-age: 3600 # ============================================================== excel: