main
HuangHai 3 months ago
parent 3dcb88e9a7
commit 6ebc284874

@ -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);}

@ -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"));
}

@ -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<String, String> 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);
}
}
/**
* POSTJSON
*
* @param url URL
* @param jsonBody JSON
* @param headers
* @return
*/
public static String postJson(String url, String jsonBody, Map<String, String> 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<String, String> formData, Map<String, String> 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<String, String> 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<String, String> 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<String, String> 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);
}
}

@ -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:

@ -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:

Loading…
Cancel
Save