初始化项目
This commit is contained in:
45
xtools-app-common/xtools-app-common-sentinel/pom.xml
Normal file
45
xtools-app-common/xtools-app-common-sentinel/pom.xml
Normal file
@@ -0,0 +1,45 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.xujun</groupId>
|
||||
<artifactId>xtools-app-common</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
<artifactId>xtools-app-common-sentinel</artifactId>
|
||||
|
||||
<!-- 依赖 -->
|
||||
<dependencies>
|
||||
<!-- xtools begin -->
|
||||
<!-- xtools-web -->
|
||||
<dependency>
|
||||
<groupId>org.xujun</groupId>
|
||||
<artifactId>xtools-web</artifactId>
|
||||
</dependency>
|
||||
<!-- xtools end -->
|
||||
|
||||
<!-- 项目模块 begin -->
|
||||
<!-- xtools-app-common-log-bus -->
|
||||
<dependency>
|
||||
<groupId>org.xujun</groupId>
|
||||
<artifactId>xtools-app-common-log-bus</artifactId>
|
||||
</dependency>
|
||||
<!-- 项目模块 end -->
|
||||
|
||||
<!-- xtools-cloud-alibaba-sentinel -->
|
||||
<dependency>
|
||||
<groupId>org.xujun</groupId>
|
||||
<artifactId>xtools-cloud-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- servlet-api -->
|
||||
<dependency>
|
||||
<groupId>jakarta.servlet</groupId>
|
||||
<artifactId>jakarta.servlet-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,62 @@
|
||||
package xtools.app.common.sentinel;
|
||||
|
||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.stereotype.Component;
|
||||
import xtools.boot.log.LogBus;
|
||||
import xtools.boot.log.enums.LogBusBaseType;
|
||||
import xtools.cloud.alibaba.sentinel.handler.CustomBlockExceptionHandler;
|
||||
import xtools.core.enums.LogLevel;
|
||||
import xtools.web.HeaderUtils;
|
||||
|
||||
/**
|
||||
* <p>Title : SysBlockExceptionHandler</p>
|
||||
* <p>Description : SysBlockExceptionHandler</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : Windows11</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 1.0.0
|
||||
* @date : 2026/4/14 21:54
|
||||
*/
|
||||
@Component
|
||||
public class SysBlockExceptionHandler implements CustomBlockExceptionHandler {
|
||||
|
||||
/**
|
||||
* 自定义Sentinel阻塞异常处理器
|
||||
*
|
||||
* @param request HttpServletRequest
|
||||
* @param response HttpServletResponse
|
||||
* @param resourceName 资源名称
|
||||
* @param e BlockException
|
||||
* @return 是否默认写出提示信息
|
||||
*/
|
||||
@Override
|
||||
public boolean handle(
|
||||
HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
String resourceName,
|
||||
BlockException e
|
||||
) {
|
||||
// 日志数据
|
||||
JSONObject log = new JSONObject();
|
||||
// IP
|
||||
log.put("ip", HeaderUtils.getIp(request));
|
||||
// URI
|
||||
log.put("uri", HeaderUtils.getAllUri(request));
|
||||
// 用户标识
|
||||
log.put("userAgent", HeaderUtils.getUserAgent(request));
|
||||
// 请求参数
|
||||
log.put("params", HeaderUtils.getParams(request));
|
||||
// 请求来源
|
||||
log.put("referer", HeaderUtils.getReferer(request));
|
||||
// 规则数据
|
||||
log.put("rule", e.getRule());
|
||||
// 保存日志
|
||||
LogBus.init(LogLevel.WARN, LogBusBaseType.SENTINEL).error(e).data(log).save();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user