初始化项目
This commit is contained in:
20
xtools-cloud-alibaba/pom.xml
Normal file
20
xtools-cloud-alibaba/pom.xml
Normal file
@@ -0,0 +1,20 @@
|
||||
<?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-cloud</artifactId>
|
||||
<version>5.0.0</version>
|
||||
</parent>
|
||||
<packaging>pom</packaging>
|
||||
<artifactId>xtools-cloud-alibaba</artifactId>
|
||||
|
||||
<!-- 子模块 -->
|
||||
<modules>
|
||||
<module>xtools-cloud-alibaba-nacos</module>
|
||||
<module>xtools-cloud-alibaba-sentinel</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
35
xtools-cloud-alibaba/xtools-cloud-alibaba-nacos/pom.xml
Normal file
35
xtools-cloud-alibaba/xtools-cloud-alibaba-nacos/pom.xml
Normal file
@@ -0,0 +1,35 @@
|
||||
<?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-cloud-alibaba</artifactId>
|
||||
<version>5.0.0</version>
|
||||
</parent>
|
||||
<artifactId>xtools-cloud-alibaba-nacos</artifactId>
|
||||
|
||||
<!-- 依赖 -->
|
||||
<dependencies>
|
||||
<!-- xtools-boot begin -->
|
||||
<!-- xtools-boot-core -->
|
||||
<dependency>
|
||||
<groupId>org.xujun</groupId>
|
||||
<artifactId>xtools-boot-core</artifactId>
|
||||
</dependency>
|
||||
<!-- xtools-boot end -->
|
||||
|
||||
<!-- Nacos Discovery(发现中心) -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
<!-- Nacos Config(配置中心) -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,25 @@
|
||||
package xtools.cloud.alibaba.nacos;
|
||||
|
||||
import xtools.boot.core.utils.ModuleLoadUtils;
|
||||
|
||||
/**
|
||||
* <p>Title : CloudAlibabaNacosConfiguration</p>
|
||||
* <p>Description : CloudAlibabaNacosConfiguration</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 CloudAlibabaNacosConfiguration {
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
*/
|
||||
public CloudAlibabaNacosConfiguration() {
|
||||
ModuleLoadUtils.loadSuccess(CloudAlibabaNacosConfiguration.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
xtools.cloud.alibaba.nacos.CloudAlibabaNacosConfiguration
|
||||
43
xtools-cloud-alibaba/xtools-cloud-alibaba-sentinel/pom.xml
Normal file
43
xtools-cloud-alibaba/xtools-cloud-alibaba-sentinel/pom.xml
Normal file
@@ -0,0 +1,43 @@
|
||||
<?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-cloud-alibaba</artifactId>
|
||||
<version>5.0.0</version>
|
||||
</parent>
|
||||
<artifactId>xtools-cloud-alibaba-sentinel</artifactId>
|
||||
|
||||
<!-- 依赖 -->
|
||||
<dependencies>
|
||||
<!-- xtools-boot begin -->
|
||||
<!-- xtools-boot-core -->
|
||||
<dependency>
|
||||
<groupId>org.xujun</groupId>
|
||||
<artifactId>xtools-boot-core</artifactId>
|
||||
</dependency>
|
||||
<!-- xtools-boot end -->
|
||||
|
||||
<!-- Sentinel(熔断,链路监控) -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Sentinel使用Nacos持久化配置 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-datasource-nacos</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- servlet-api -->
|
||||
<dependency>
|
||||
<groupId>jakarta.servlet</groupId>
|
||||
<artifactId>jakarta.servlet-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
@@ -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)));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
xtools.cloud.alibaba.sentinel.CloudAlibabaSentinelConfiguration
|
||||
Reference in New Issue
Block a user