初始化项目
This commit is contained in:
+28
@@ -0,0 +1,28 @@
|
||||
package xtools.cloud.alibaba.sentinel;
|
||||
|
||||
import org.springframework.context.annotation.Import;
|
||||
import xtools.boot.core.utils.ModuleLoadUtils;
|
||||
import xtools.cloud.alibaba.sentinel.selector.CloudAlibabaSentinelImportSelector;
|
||||
|
||||
/**
|
||||
* <p>Title : CloudAlibabaSentinelConfiguration</p>
|
||||
* <p>Description : CloudAlibabaSentinelConfiguration</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/01/01 09:30
|
||||
*/
|
||||
@Import(CloudAlibabaSentinelImportSelector.class)
|
||||
public class CloudAlibabaSentinelConfiguration {
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
*/
|
||||
public CloudAlibabaSentinelConfiguration() {
|
||||
ModuleLoadUtils.loadSuccess(CloudAlibabaSentinelConfiguration.class);
|
||||
}
|
||||
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package xtools.cloud.alibaba.sentinel.handler;
|
||||
|
||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* <p>Title : CustomBlockExceptionHandler</p>
|
||||
* <p>Description : CustomBlockExceptionHandler</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:40
|
||||
*/
|
||||
public interface CustomBlockExceptionHandler {
|
||||
|
||||
/**
|
||||
* 自定义Sentinel阻塞异常处理器
|
||||
*
|
||||
* @param request HttpServletRequest
|
||||
* @param response HttpServletResponse
|
||||
* @param resourceName 资源名称
|
||||
* @param e BlockException
|
||||
* @return 是否默认写出提示信息
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
boolean handle(
|
||||
HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
String resourceName,
|
||||
BlockException e
|
||||
) throws Exception;
|
||||
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package xtools.cloud.alibaba.sentinel.handler;
|
||||
|
||||
import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.callback.BlockExceptionHandler;
|
||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import xtools.boot.api.enums.ResultType;
|
||||
import xtools.boot.api.model.dto.Result;
|
||||
import xtools.boot.core.utils.SpringContextUtils;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* <p>Title : SentinelBlockExceptionHandler</p>
|
||||
* <p>Description : SentinelBlockExceptionHandler</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:29
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class SentinelBlockExceptionHandler implements BlockExceptionHandler {
|
||||
|
||||
/**
|
||||
* Sentinel阻塞异常处理器
|
||||
*
|
||||
* @param request HttpServletRequest
|
||||
* @param response HttpServletResponse
|
||||
* @param resourceName 资源名称
|
||||
* @param e BlockException
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
@Override
|
||||
public void handle(
|
||||
HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
String resourceName,
|
||||
BlockException e
|
||||
) throws Exception {
|
||||
// 自定义阻塞异常处理器
|
||||
CustomBlockExceptionHandler beanDefNull = SpringContextUtils.getBeanDefNull(CustomBlockExceptionHandler.class);
|
||||
if (Objects.nonNull(beanDefNull)) {
|
||||
boolean handle = beanDefNull.handle(request, response, resourceName, e);
|
||||
if (!handle) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
log.warn("阻塞异常处理器,resourceName={}", resourceName, e);
|
||||
}
|
||||
// 默认输出JSON提示信息
|
||||
response.setHeader("Content-Type", "text/plain;charset=UTF-8");
|
||||
response.getWriter().write(JSONObject.toJSONString(new Result<>(ResultType.TOO_MANY_REQUESTS, null)));
|
||||
}
|
||||
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package xtools.cloud.alibaba.sentinel.selector;
|
||||
|
||||
import org.jspecify.annotations.NonNull;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
|
||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
|
||||
/**
|
||||
* <p>Title : CloudAlibabaSentinelImportSelector</p>
|
||||
* <p>Description : CloudAlibabaSentinelImportSelector</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/01/01 09:30
|
||||
*/
|
||||
public class CloudAlibabaSentinelImportSelector implements ImportBeanDefinitionRegistrar {
|
||||
|
||||
/**
|
||||
* 根据给定的注释元数据,根据需要注册bean
|
||||
*
|
||||
* @param importingClassMetadata AnnotationMetadata
|
||||
* @param registry BeanDefinitionRegistry
|
||||
*/
|
||||
@Override
|
||||
public void registerBeanDefinitions(@NonNull AnnotationMetadata importingClassMetadata, @NonNull BeanDefinitionRegistry registry) {
|
||||
// 构建扫描对象
|
||||
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry, true);
|
||||
// 扫描包下路径
|
||||
scanner.scan("xtools.cloud.alibaba.sentinel");
|
||||
}
|
||||
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
xtools.cloud.alibaba.sentinel.CloudAlibabaSentinelConfiguration
|
||||
Reference in New Issue
Block a user