From f37979e60f747120ee66ae9547a2591f465910c3 Mon Sep 17 00:00:00 2001
From: AprilWind <2100166581@qq.com>
Date: Fri, 31 May 2024 15:27:28 +0800
Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20forest=E6=8F=92=E4=BB=B6?=
=?UTF-8?q?=E5=B7=A5=E5=85=B7?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 7 ++++
ruoyi-common/pom.xml | 1 +
ruoyi-common/ruoyi-common-bom/pom.xml | 7 ++++
ruoyi-common/ruoyi-common-forest/pom.xml | 42 +++++++++++++++++++
.../forest/config/ForestConfiguration.java | 28 +++++++++++++
.../forest/exception/ForestException.java | 25 +++++++++++
.../handler/ForestExceptionHandler.java | 30 +++++++++++++
.../common/forest/utils/ForestUtils.java | 34 +++++++++++++++
...ot.autoconfigure.AutoConfiguration.imports | 1 +
.../src/main/resources/common-forest.yml | 38 +++++++++++++++++
10 files changed, 213 insertions(+)
create mode 100644 ruoyi-common/ruoyi-common-forest/pom.xml
create mode 100644 ruoyi-common/ruoyi-common-forest/src/main/java/org/dromara/common/forest/config/ForestConfiguration.java
create mode 100644 ruoyi-common/ruoyi-common-forest/src/main/java/org/dromara/common/forest/exception/ForestException.java
create mode 100644 ruoyi-common/ruoyi-common-forest/src/main/java/org/dromara/common/forest/handler/ForestExceptionHandler.java
create mode 100644 ruoyi-common/ruoyi-common-forest/src/main/java/org/dromara/common/forest/utils/ForestUtils.java
create mode 100644 ruoyi-common/ruoyi-common-forest/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
create mode 100644 ruoyi-common/ruoyi-common-forest/src/main/resources/common-forest.yml
diff --git a/pom.xml b/pom.xml
index a0912de50..f01461858 100644
--- a/pom.xml
+++ b/pom.xml
@@ -340,6 +340,13 @@
${fastjson.version}
+
+
+ com.dtflys.forest
+ forest-spring-boot3-starter
+ ${forest.version}
+
+
org.dromara
ruoyi-system
diff --git a/ruoyi-common/pom.xml b/ruoyi-common/pom.xml
index 45493d3e9..c2b6c4c0e 100644
--- a/ruoyi-common/pom.xml
+++ b/ruoyi-common/pom.xml
@@ -15,6 +15,7 @@
ruoyi-common-core
ruoyi-common-doc
ruoyi-common-excel
+ ruoyi-common-forest
ruoyi-common-idempotent
ruoyi-common-job
ruoyi-common-log
diff --git a/ruoyi-common/ruoyi-common-bom/pom.xml b/ruoyi-common/ruoyi-common-bom/pom.xml
index f9af9de0f..9ff58dda2 100644
--- a/ruoyi-common/ruoyi-common-bom/pom.xml
+++ b/ruoyi-common/ruoyi-common-bom/pom.xml
@@ -40,6 +40,13 @@
${revision}
+
+
+ org.dromara
+ ruoyi-common-forest
+ ${revision}
+
+
org.dromara
diff --git a/ruoyi-common/ruoyi-common-forest/pom.xml b/ruoyi-common/ruoyi-common-forest/pom.xml
new file mode 100644
index 000000000..8f60e0c7f
--- /dev/null
+++ b/ruoyi-common/ruoyi-common-forest/pom.xml
@@ -0,0 +1,42 @@
+
+
+
+ ruoyi-common
+ org.dromara
+ ${revision}
+
+ 4.0.0
+
+ ruoyi-common-forest
+
+
+ ruoyi-common-forest forest框架的http服务
+
+
+
+ https://forest.dtflyx.com/pages/1.5.33/intro/
+
+
+
+
+
+ org.dromara
+ ruoyi-common-core
+
+
+
+ org.dromara
+ ruoyi-common-json
+
+
+
+
+ com.dtflys.forest
+ forest-spring-boot3-starter
+
+
+
+
+
diff --git a/ruoyi-common/ruoyi-common-forest/src/main/java/org/dromara/common/forest/config/ForestConfiguration.java b/ruoyi-common/ruoyi-common-forest/src/main/java/org/dromara/common/forest/config/ForestConfiguration.java
new file mode 100644
index 000000000..1eeb4ed5a
--- /dev/null
+++ b/ruoyi-common/ruoyi-common-forest/src/main/java/org/dromara/common/forest/config/ForestConfiguration.java
@@ -0,0 +1,28 @@
+package org.dromara.common.forest.config;
+
+import lombok.extern.slf4j.Slf4j;
+import org.dromara.common.core.factory.YmlPropertySourceFactory;
+import org.dromara.common.forest.handler.ForestExceptionHandler;
+import org.springframework.boot.autoconfigure.AutoConfiguration;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.PropertySource;
+
+/**
+ * Forest 配置属性
+ *
+ * @author AprilWind
+ */
+@Slf4j
+@AutoConfiguration
+@PropertySource(value = "classpath:common-forest.yml", factory = YmlPropertySourceFactory.class)
+public class ForestConfiguration {
+
+ /**
+ * Forest异常处理器
+ */
+ @Bean
+ public ForestExceptionHandler forestExceptionHandler() {
+ return new ForestExceptionHandler();
+ }
+
+}
diff --git a/ruoyi-common/ruoyi-common-forest/src/main/java/org/dromara/common/forest/exception/ForestException.java b/ruoyi-common/ruoyi-common-forest/src/main/java/org/dromara/common/forest/exception/ForestException.java
new file mode 100644
index 000000000..0508ee8b9
--- /dev/null
+++ b/ruoyi-common/ruoyi-common-forest/src/main/java/org/dromara/common/forest/exception/ForestException.java
@@ -0,0 +1,25 @@
+package org.dromara.common.forest.exception;
+
+import org.dromara.common.core.exception.base.BaseException;
+
+import java.io.Serial;
+
+/**
+ * Forest第三方请求异常类
+ *
+ * @author AprilWind
+ */
+public class ForestException extends BaseException {
+
+ @Serial
+ private static final long serialVersionUID = 1L;
+
+ public ForestException(String msg) {
+ super("forest", msg);
+ }
+
+ public ForestException(String code, Object[] args) {
+ super("forest", code, args, null);
+ }
+
+}
diff --git a/ruoyi-common/ruoyi-common-forest/src/main/java/org/dromara/common/forest/handler/ForestExceptionHandler.java b/ruoyi-common/ruoyi-common-forest/src/main/java/org/dromara/common/forest/handler/ForestExceptionHandler.java
new file mode 100644
index 000000000..509680ca9
--- /dev/null
+++ b/ruoyi-common/ruoyi-common-forest/src/main/java/org/dromara/common/forest/handler/ForestExceptionHandler.java
@@ -0,0 +1,30 @@
+package org.dromara.common.forest.handler;
+
+import cn.hutool.http.HttpStatus;
+import jakarta.servlet.http.HttpServletRequest;
+import lombok.extern.slf4j.Slf4j;
+import org.dromara.common.core.domain.R;
+import org.dromara.common.forest.exception.ForestException;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+
+/**
+ * Forest异常处理器
+ *
+ * @author AprilWind
+ */
+@Slf4j
+@RestControllerAdvice
+public class ForestExceptionHandler {
+
+ /**
+ * 请求第三方异常
+ */
+ @ExceptionHandler(ForestException.class)
+ public R handleForestException(ForestException e, HttpServletRequest request) {
+ String requestURI = request.getRequestURI();
+ log.error("请求地址'{}',forest请求第三方异常." + requestURI, e.getMessage());
+ return R.fail(HttpStatus.HTTP_UNAVAILABLE, "抱歉,服务暂时无法响应您的请求...");
+ }
+
+}
diff --git a/ruoyi-common/ruoyi-common-forest/src/main/java/org/dromara/common/forest/utils/ForestUtils.java b/ruoyi-common/ruoyi-common-forest/src/main/java/org/dromara/common/forest/utils/ForestUtils.java
new file mode 100644
index 000000000..21976ab9a
--- /dev/null
+++ b/ruoyi-common/ruoyi-common-forest/src/main/java/org/dromara/common/forest/utils/ForestUtils.java
@@ -0,0 +1,34 @@
+package org.dromara.common.forest.utils;
+
+import org.dromara.common.core.utils.StringUtils;
+
+/**
+ * Forest第三方请求工具类
+ *
+ * @author AprilWind
+ */
+public class ForestUtils {
+
+ /**
+ * 生成参数片段
+ *
+ * @param name 参数名
+ * @param value 参数值
+ * @return 带参数的URL片段,如果参数值为空,则返回空字符串
+ */
+ public static String parameter(String name, String value) {
+ return StringUtils.isNotBlank(value) ? "&" + name + "=" + value : "";
+ }
+
+ /**
+ * 生成参数片段
+ *
+ * @param name 参数名称
+ * @param value 参数值
+ * @return 带参数的URL片段,如果参数值为空,则返回空字符串
+ */
+ public static String parameter(String name, Integer value) {
+ return value != null ? "&" + name + "=" + value : "";
+ }
+
+}
diff --git a/ruoyi-common/ruoyi-common-forest/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/ruoyi-common/ruoyi-common-forest/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
new file mode 100644
index 000000000..b7760e5ae
--- /dev/null
+++ b/ruoyi-common/ruoyi-common-forest/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
@@ -0,0 +1 @@
+org.dromara.common.forest.config.ForestConfiguration
diff --git a/ruoyi-common/ruoyi-common-forest/src/main/resources/common-forest.yml b/ruoyi-common/ruoyi-common-forest/src/main/resources/common-forest.yml
new file mode 100644
index 000000000..009b2cc4c
--- /dev/null
+++ b/ruoyi-common/ruoyi-common-forest/src/main/resources/common-forest.yml
@@ -0,0 +1,38 @@
+# 内置配置
+# HTTP 客户端配置
+forest:
+ # 支持okhttp3和httpclient两种后端 HTTP API(默认为 okhttp3)
+ backend: okhttp3
+ # 连接池最大连接数(默认为 500)
+ max-connections: 1000
+ # 每个路由的最大连接数(默认为 500)
+ max-route-connections: 500
+ # 最大请求等待队列大小
+ max-request-queue-size: 100
+ # 最大异步线程数
+ max-async-thread-size: 300
+ # 最大异步线程池队列大小
+ max-async-queue-size: 16
+ # 请求超时时间,单位为毫秒(默认为 3000)
+ timeout: 3000
+ # 连接超时时间,单位为毫秒(默认为 timeout)
+ connect-timeout: 300000
+ # 数据读取超时时间,单位为毫秒(默认为 timeout)
+ read-timeout: 300000
+ # 请求失败后重试次数(默认为 0 次不重试)
+ max-retry-count: 0
+ # 单向验证的HTTPS的默认TLS协议(默认为 TLS)
+ ssl-protocol: TLS
+ # 打开或关闭日志(默认为 true)
+ log-enabled: false
+ # 打开/关闭Forest请求日志(默认为 true)
+ log-request: false
+ # 打开/关闭Forest响应状态日志(默认为 true)
+ log-response-status: false
+ # 打开/关闭Forest响应内容日志(默认为 false)
+ log-response-content: false
+ # 异步模式(默认为 platform)
+ async-mode: platform
+ # 在spring上下文中bean的id,默认值为forestConfiguration,可以在 Spring 中通过 Bean 的名称引用
+ # ForestConfiguration forestConfiguration= SpringUtils.getBean("forestConfiguration");
+ bean-id: forestConfiguration