新增 forest插件工具

This commit is contained in:
AprilWind 2024-05-31 15:27:28 +08:00
parent 528b65ae04
commit f37979e60f
10 changed files with 213 additions and 0 deletions

View File

@ -340,6 +340,13 @@
<version>${fastjson.version}</version>
</dependency>
<!-- forest http请求工具 -->
<dependency>
<groupId>com.dtflys.forest</groupId>
<artifactId>forest-spring-boot3-starter</artifactId>
<version>${forest.version}</version>
</dependency>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>ruoyi-system</artifactId>

View File

@ -15,6 +15,7 @@
<module>ruoyi-common-core</module>
<module>ruoyi-common-doc</module>
<module>ruoyi-common-excel</module>
<module>ruoyi-common-forest</module>
<module>ruoyi-common-idempotent</module>
<module>ruoyi-common-job</module>
<module>ruoyi-common-log</module>

View File

@ -40,6 +40,13 @@
<version>${revision}</version>
</dependency>
<!-- forest -->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>ruoyi-common-forest</artifactId>
<version>${revision}</version>
</dependency>
<!-- 幂等 -->
<dependency>
<groupId>org.dromara</groupId>

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>ruoyi-common</artifactId>
<groupId>org.dromara</groupId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ruoyi-common-forest</artifactId>
<description>
ruoyi-common-forest forest框架的http服务
</description>
<!-- 官方文档地址 -->
<url>https://forest.dtflyx.com/pages/1.5.33/intro/</url>
<dependencies>
<!-- RuoYi Common Core-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>ruoyi-common-core</artifactId>
</dependency>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>ruoyi-common-json</artifactId>
</dependency>
<!-- forest框架 https://forest.dtflyx.com/ -->
<dependency>
<groupId>com.dtflys.forest</groupId>
<artifactId>forest-spring-boot3-starter</artifactId>
</dependency>
</dependencies>
</project>

View File

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

View File

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

View File

@ -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<Void> handleForestException(ForestException e, HttpServletRequest request) {
String requestURI = request.getRequestURI();
log.error("请求地址'{}',forest请求第三方异常." + requestURI, e.getMessage());
return R.fail(HttpStatus.HTTP_UNAVAILABLE, "抱歉,服务暂时无法响应您的请求...");
}
}

View File

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

View File

@ -0,0 +1 @@
org.dromara.common.forest.config.ForestConfiguration

View File

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