初始化项目

This commit is contained in:
2026-04-21 15:45:20 +08:00
parent 16d7cb6531
commit 0baba59d57
21 changed files with 1131 additions and 19 deletions

43
.gitignore vendored
View File

@@ -1,20 +1,29 @@
# ---> Actionscript
# Build and Release Folders
bin-debug/
bin-release/
[Oo]bj/
[Bb]in/
# ---> Project
# Other files and folders
# logs
**/logs/
# ai
.claude/settings.local.json
# ---> Maven
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar
# ---> Eclipse
.settings/
.project
.classpath
# Executables
*.swf
*.air
*.ipa
*.apk
# Project files, i.e. `.project`, `.actionScriptProperties` and `.flexProperties`
# should NOT be excluded as they contain compiler settings and other important
# information for Eclipse / Flash Builder.
# ---> Idea
.idea/
*.iml

457
README.md
View File

@@ -1,3 +1,456 @@
# xtools-cloud
# xtools-cloud 项目设计文档
低调大师工具箱,SpringCloud工具模块,适配JDK25
## 一、功能和用途
### 1.1 项目概述
- **项目名称**xtools-cloud
- **项目版本**5.0.0
- **父POM**org.xujun:xtools-parent-cloud:5.0.0
- **项目定位**:低调大师工具箱 Spring Cloud 微服务工具模块,适配 JDK 25提供微服务基础设施集成能力
- **技术特点**
- 采用最新的 JDK 25 版本,充分利用新特性
- 基于 Spring Boot 4.0.5 构建,支持自动配置和快速开发
- 集成 Spring Cloud 2025.1.1 微服务框架
- 支持 Nacos 3.1.1 服务注册与发现及配置中心
- 集成 Sentinel 1.8.9 实现流量控制和熔断降级
- 使用 Spring Cloud LoadBalancer 实现客户端负载均衡
- 集成微服务调用日志追踪,支持跨服务链路追踪
### 1.2 核心功能
```mermaid
graph TB
subgraph xtools-cloud 微服务工具模块
A1[Nacos服务注册与发现]
A2[Nacos配置中心]
A3[Sentinel流量控制]
A4[Sentinel熔断降级]
A5[REST远程调用]
A6[调用日志追踪]
A7[客户端负载均衡]
A8[自定义限流异常处理]
end
```
**核心功能详细说明**
- **Nacos 服务注册与发现**:基于 Spring Cloud Alibaba Nacos Discovery 实现微服务自动注册与发现
- **Nacos 配置中心**:基于 Spring Cloud Alibaba Nacos Config 实现动态配置管理
- **Sentinel 流量控制**:集成 Sentinel 实现请求限流、熔断降级,支持 Nacos 持久化规则配置
- **REST 远程调用**:基于 Spring RestClient 封装,提供统一的微服务间 HTTP 调用能力
- **调用日志追踪**:通过拦截器实现微服务调用链路的请求/响应日志记录
- **客户端负载均衡**:集成 Spring Cloud LoadBalancer 实现客户端侧负载均衡
- **自定义限流异常处理**:支持自定义 Sentinel 限流异常处理器,可灵活扩展
```mermaid
mindmap
root((xtools-cloud<br/>微服务工具模块))
xtools-cloud-alibaba
Nacos
服务注册与发现
配置中心
Sentinel
流量控制
熔断降级
自定义异常处理
xtools-cloud-call
REST远程调用
RestClient封装
请求拦截器
响应缓冲
调用追踪
链路日志
日志总线集成
负载均衡
Spring Cloud LoadBalancer
```
## 二、项目结构设计
### 2.1 整体架构
```mermaid
graph TD
xtools_cloud[xtools-cloud<br/>微服务工具父模块] --> xtools_cloud_alibaba[xtools-cloud-alibaba<br/>Alibaba微服务组件]
xtools_cloud --> xtools_cloud_call[xtools-cloud-call<br/>微服务调用模块]
xtools_cloud_alibaba --> nacos[xtools-cloud-alibaba-nacos<br/>Nacos配置]
xtools_cloud_alibaba --> sentinel[xtools-cloud-alibaba-sentinel<br/>Sentinel配置]
xtools_cloud_call -.->|依赖| xtools_boot_core[xtools-boot-core<br/>核心模块]
xtools_cloud_call -.->|依赖| xtools_boot_log[xtools-boot-log<br/>日志模块]
nacos -.->|依赖| xtools_boot_core
sentinel -.->|依赖| xtools_boot_core
```
### 2.2 模块职责
| 模块 | 职责 |
|------|------|
| xtools-cloud | 父模块,管理子模块和公共属性配置 |
| xtools-cloud-alibaba | Alibaba 微服务组件聚合模块 |
| xtools-cloud-alibaba-nacos | Nacos 服务注册与发现、配置中心自动配置 |
| xtools-cloud-alibaba-sentinel | Sentinel 流量控制与熔断降级自动配置 |
| xtools-cloud-call | 微服务间 REST 调用封装,包含拦截器、日志追踪、负载均衡 |
### 2.3 包结构设计
```
xtools.cloud.alibaba.nacos
└── CloudAlibabaNacosConfiguration.java # Nacos自动配置入口
xtools.cloud.alibaba.sentinel
├── CloudAlibabaSentinelConfiguration.java # Sentinel自动配置入口
├── handler
│ ├── CustomBlockExceptionHandler.java # 自定义限流异常处理接口
│ └── SentinelBlockExceptionHandler.java # Sentinel限流异常处理器
└── selector
└── CloudAlibabaSentinelImportSelector.java # Sentinel Bean扫描注册器
xtools.cloud.call
├── CloudClientConfiguration.java # 调用模块自动配置入口
├── config
│ └── RestClientConfig.java # RestClient自定义配置
├── interceptor
│ ├── BufferedClientHttpResponse.java # 响应体缓冲包装器
│ ├── CallInterceptor.java # 调用拦截器接口
│ └── ClientHttpInterceptor.java # HTTP调用拦截器实现
└── selector
└── CloudClientImportSelector.java # 调用模块Bean扫描注册器
```
### 2.4 自动加载机制
```mermaid
flowchart TD
A[应用启动] --> B[xtools-boot-core 自动加载]
B --> C{检测模块依赖}
C -->|引入 xtools-cloud-alibaba-nacos| D[加载 Nacos 自动配置]
C -->|引入 xtools-cloud-alibaba-sentinel| E[加载 Sentinel 自动配置]
C -->|引入 xtools-cloud-call| F[加载 Call 自动配置]
D --> D1[注册 Nacos Discovery]
D --> D2[注册 Nacos Config]
E --> E1[注册 Sentinel Bean]
E --> E2[注册限流异常处理器]
E --> E3[扫描 sentinel 包下组件]
F --> F1[注册 RestClientCustomizer]
F --> F2[注册 ClientHttpInterceptor]
F --> F3[扫描 call 包下组件]
```
## 三、项目功能设计
### 3.1 Nacos 集成设计
**功能说明**
- **服务注册与发现**:基于 `spring-cloud-starter-alibaba-nacos-discovery` 实现微服务自动注册到 Nacos 注册中心,并支持服务发现
- **配置中心**:基于 `spring-cloud-starter-alibaba-nacos-config` 实现从 Nacos 配置中心动态加载配置
**模块加载机制**
```mermaid
flowchart TD
A[CloudAlibabaNacosConfiguration] --> B[构造方法调用]
B --> C[ModuleLoadUtils.loadSuccess<br/>记录模块加载成功]
```
`CloudAlibabaNacosConfiguration` 作为自动配置入口类,通过 Spring Boot 的自动装配机制加载。模块本身无需额外配置,依赖 Spring Cloud Alibaba Nacos Starter 提供的默认自动配置。
### 3.2 Sentinel 集成设计
**功能说明**
- **流量控制**:基于 `spring-cloud-starter-alibaba-sentinel` 实现请求流量控制
- **熔断降级**Sentinel 提供的熔断降级能力,保护微服务稳定性
- **规则持久化**:通过 `sentinel-datasource-nacos` 将限流规则持久化到 Nacos
- **自定义异常处理**:提供 `CustomBlockExceptionHandler` 接口,支持业务自定义限流异常处理逻辑
**限流异常处理流程**
```mermaid
flowchart TD
A[请求触发Sentinel限流] --> B[SentinelBlockExceptionHandler.handle]
B --> C{是否存在自定义处理器?}
C -->|存在| D[调用 CustomBlockExceptionHandler]
D --> E{返回值是否为 true?}
E -->|true - 已处理| F[直接返回<br/>不输出默认信息]
E -->|false - 需默认处理| G[输出默认JSON提示<br/>TOO_MANY_REQUESTS]
C -->|不存在| H[记录warn日志]
H --> G
```
**模块加载机制**
```mermaid
flowchart TD
A[CloudAlibabaSentinelConfiguration] --> B["@Import\nCloudAlibabaSentinelImportSelector"]
B --> C[ImportBeanDefinitionRegistrar]
C --> D[ClassPathBeanDefinitionScanner]
D --> E["扫描 xtools.cloud.alibaba.sentinel 包"]
E --> F[注册 SentinelBlockExceptionHandler]
E --> G[注册 CustomBlockExceptionHandler<br/>如业务有实现]
```
### 3.3 微服务调用设计
**功能说明**
- **REST 远程调用**:基于 Spring RestClient 封装,提供统一的微服务间 HTTP 调用能力
- **请求拦截**:通过 `ClientHttpInterceptor` 实现请求拦截,自动注入日志追踪信息和微服务标识
- **响应缓冲**:通过 `BufferedClientHttpResponse` 包装响应体,支持响应体重复读取
- **调用日志追踪**:记录请求和响应的完整日志信息,通过 LogBus 日志总线输出
- **负载均衡**:集成 `spring-cloud-starter-loadbalancer` 实现客户端负载均衡
- **扩展拦截**:提供 `CallInterceptor` 接口,支持业务自定义请求前置处理
**调用拦截流程**
```mermaid
flowchart TD
A[发起REST远程调用] --> B[ClientHttpInterceptor.intercept]
B --> C{CallInterceptor 是否存在?}
C -->|存在| D[执行 before 方法<br/>自定义前置处理]
C -->|不存在| E[跳过自定义处理]
D --> E
E --> F[注入请求头<br/>日志追踪ID + 微服务标识]
F --> G[记录请求日志<br/>URI + 请求头 + 请求体]
G --> H[执行HTTP请求]
H --> I[读取并缓冲响应体]
I --> J[记录响应日志<br/>耗时 + 状态码 + 响应体]
J --> K[返回 BufferedClientHttpResponse]
```
**模块加载机制**
```mermaid
flowchart TD
A[CloudClientConfiguration] --> B["@Import\nCloudClientImportSelector"]
B --> C[ImportBeanDefinitionRegistrar]
C --> D[ClassPathBeanDefinitionScanner]
D --> E["扫描 xtools.cloud.call 包"]
E --> F[注册 RestClientConfig]
F --> G[注册 RestClientCustomizer]
F --> H[注册 ClientHttpInterceptor]
```
### 3.4 跨服务链路追踪设计
**追踪信息传递**
```mermaid
flowchart LR
A[服务A<br/>发起调用] --> B[ClientHttpInterceptor]
B --> C[获取 LogTrack]
C --> D["请求头注入<br/>LOG_TRACK + CLOUD标识"]
D --> E[服务B<br/>接收请求]
E --> F[日志过滤组件<br/>提取追踪信息]
F --> G[日志记录<br/>关联同一链路ID]
```
**追踪字段说明**
| 字段 | 说明 |
|------|------|
| LOG_TRACK | Base64 编码的日志追踪信息包含链路ID等 |
| CLOUD | 标识当前请求为微服务间调用 |
| UID | 用户ID透传 |
| AUTHORIZATION | 认证令牌(透传) |
## 四、编码规范设计
### 4.1 命名规范
**类命名**
- **配置类**:以 `Configuration` 结尾,如 `CloudClientConfiguration``RestClientConfig`
- **选择器类**:以 `ImportSelector` 结尾,如 `CloudClientImportSelector`
- **拦截器接口**:以 `Interceptor` 结尾,如 `CallInterceptor`
- **拦截器实现**:以具体功能命名,如 `ClientHttpInterceptor`
- **异常处理器**:以 `Handler``ExceptionHandler` 结尾
- **包装器**:以具体功能 + 原始类型命名,如 `BufferedClientHttpResponse`
**方法命名**
- 回调方法:`handle``before`
- 生命周期:`registerBeanDefinitions``loadSuccess`
- 工厂方法:`restClientCustomizer``clientHttpInterceptor`
### 4.2 注释规范
**类注释格式**
```java
/**
* <p>Title : 类名称</p>
* <p>Description : 类描述</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
*/
```
**方法注释格式**
```java
/**
* 方法描述
*
* @param param 参数说明
* @return 返回值说明
* @throws Exception 异常说明
*/
```
### 4.3 代码风格
- 使用 Lombok 简化代码(`@Slf4j``@AllArgsConstructor`
- 使用 JSpecify `@NonNull` 注解标注非空参数
- 使用 `@Resource` 进行依赖注入
- 使用 Spring `@Configuration``@Bean``@Component` 注解
- 统一使用 `@Import` + `ImportBeanDefinitionRegistrar` 模式实现自动配置
- 通过 `ModuleLoadUtils.loadSuccess()` 记录模块加载状态
- 使用 `Objects.nonNull()` 进行空值判断
- 接口定义 default 方法提供默认实现
### 4.4 设计规范
- **模块化设计**:每个功能模块独立成子模块,通过 Maven 依赖引入
- **自动装配**:使用 `@Import` + `ImportBeanDefinitionRegistrar` 实现包扫描和自动配置
- **接口扩展**:核心功能提供接口(如 `CallInterceptor``CustomBlockExceptionHandler`),支持业务自定义扩展
- **包装器模式**`BufferedClientHttpResponse` 包装原始响应,解决响应体只能读取一次的问题
### 4.5 安全规范
- 请求头中透传认证信息AUTHORIZATION、UID保持微服务间调用安全
- 通过 LogBus 日志总线记录调用日志,便于安全审计
- 使用 Sentinel 流量控制防止服务过载
## 五、项目依赖设计
### 5.1 核心框架依赖
| 依赖 | 版本 | 用途 |
|------|------|------|
| Spring Boot | 4.0.5 | 应用框架 |
| Spring Framework | 7.0.6 | 核心框架 |
| Spring Cloud | 2025.1.1 | 微服务框架 |
| Spring Cloud Alibaba | 2025.1.0.0 | 微服务组件 |
| Spring Cloud LoadBalancer | 2025.1.1 | 客户端负载均衡 |
### 5.2 微服务组件依赖
| 依赖 | 版本 | 用途 |
|------|------|------|
| Nacos Client | 3.1.1 | 服务注册与发现、配置中心 |
| Sentinel | 1.8.9 | 流量控制与熔断降级 |
### 5.3 xtools 内部框架依赖
| 依赖 | 版本 | 用途 |
|------|------|------|
| xtools-boot-core | 5.0.0 | 核心工具模块ModuleLoadUtils 等) |
| xtools-boot-log | 5.0.0 | 日志模块LogBus、LogTrack 等) |
| xtools-core | 5.0.0 | 基础核心模块(枚举、常量等) |
| xtools-boot-api | 5.0.0 | API 模块Result、常量等 |
### 5.4 工具库依赖来自父POM管理
| 依赖 | 版本 | 用途 |
|------|------|------|
| Lombok | 1.18.44 | 代码简化 |
| MapStruct | 1.6.3 | 对象映射 |
| FastJSON2 | 2.0.60 | JSON 处理 |
| Jackson | 2.21.2 | JSON 处理Spring Boot 管理) |
| JSpecify | - | 空值注解 |
### 5.5 Web 相关依赖
| 依赖 | 版本 | 用途 |
|------|------|------|
| Jakarta Servlet API | 6.1.0 | Servlet 规范provided |
## 六、技术选型说明
### 6.1 JDK 25
- **选择原因**:采用最新 JDK 版本,利用性能优化、改进的垃圾回收器、增强的并发工具等新特性
- **要求**:项目最低 JDK 版本为 25
### 6.2 Spring Boot 4.0.5
- **Spring Framework 版本**7.0.6
- **主要特性**
- 支持 JDK 17+,充分利用现代 JDK 特性
- 支持 RestClient 作为新的 HTTP 客户端
- 响应式编程支持
- 内置可观测性支持
- 虚拟线程支持
- 改进的自动配置机制
### 6.3 Spring Cloud 2025.1.1
- **选择原因**:与 Spring Boot 4.0.5 配套的 Spring Cloud 版本
- **主要特性**
- 基于 Spring Framework 7.0.x
- 支持服务注册与发现
- 客户端负载均衡
- 配置管理
- 熔断器模式
### 6.4 Spring Cloud Alibaba 2025.1.0.0
- **Nacos 版本**3.1.1 - 服务注册与发现、配置中心
- **Sentinel 版本**1.8.9 - 流量控制与熔断降级
- **选择原因**:国内主流微服务解决方案,与 Spring Cloud 无缝集成
### 6.5 RestClient
- **选择原因**Spring Boot 4.x 推荐的 HTTP 客户端,替代 RestTemplate
- **主要特性**
- 流式 API 设计
- 支持拦截器机制
- 支持自定义配置RestClientCustomizer
- 原生支持请求/响应日志记录
### 6.6 其他重要依赖版本
| 依赖 | 版本 | 用途 |
|------|------|------|
| Spring Boot Admin | 4.0.3 | 应用监控 |
| Druid | 1.2.28 | 数据库连接池 |
| MyBatis Spring Boot Starter | 4.0.1 | MyBatis 集成 |
| MyBatis-Plus | 3.5.16 | ORM 增强工具 |
| Knife4j | 4.5.0 | API 文档增强 |
| SpringDoc OpenAPI | 3.0.3 | OpenAPI 文档 |
| Swagger Annotations | 2.2.48 | API 注解 |
| XXL-Job | 3.4.0 | 分布式任务调度 |
| Velocity | 2.4.1 | 模板引擎 |
| Easy Captcha | 1.6.2 | 验证码生成 |
| BouncyCastle | 1.84 | 加密库 |
| OSHI | 6.11.1 | 系统监控 |
| Fesod Sheet | 2.0.1-incubating | Excel 处理 |
| Caffeine | 3.2.3 | 本地缓存 |
| Java JWT | 4.5.1 | JWT 令牌 |
| Elasticsearch Client | 9.2.6 | ES 客户端 |
| Spring AMQP | 4.0.2 | RabbitMQ 集成 |
| Netty | 4.2.12.Final | 网络框架 |
| AspectJ | 1.9.25.1 | AOP 支持 |
| Commons Lang3 | 3.19.0 | 通用工具 |
---
**文档版本**v1.0
**编写日期**2026-04-16
**项目版本**5.0.0
**父POM版本**xtools-parent-cloud:5.0.0
**JDK版本**25
**维护团队**xujun.org

30
pom.xml Normal file
View File

@@ -0,0 +1,30 @@
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>xtools-cloud</artifactId>
<packaging>pom</packaging>
<name>xtools-cloud</name>
<description>低调大师工具箱,SpringCloud工具模块,适配JDK25</description>
<!-- 父pom -->
<parent>
<groupId>org.xujun</groupId>
<artifactId>xtools-parent-cloud</artifactId>
<version>5.0.0</version>
<relativePath/>
</parent>
<!-- 子模块 -->
<modules>
<module>xtools-cloud-alibaba</module>
<module>xtools-cloud-call</module>
</modules>
<!-- 属性配置 -->
<properties>
<!-- JDK 版本号 -->
<java.version>25</java.version>
</properties>
</project>

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

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

View File

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

View File

@@ -0,0 +1 @@
xtools.cloud.alibaba.nacos.CloudAlibabaNacosConfiguration

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -0,0 +1 @@
xtools.cloud.alibaba.sentinel.CloudAlibabaSentinelConfiguration

35
xtools-cloud-call/pom.xml Normal file
View 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</artifactId>
<version>5.0.0</version>
</parent>
<artifactId>xtools-cloud-call</artifactId>
<!-- 依赖 -->
<dependencies>
<!-- xtools-boot begin -->
<!-- xtools-boot-core -->
<dependency>
<groupId>org.xujun</groupId>
<artifactId>xtools-boot-core</artifactId>
</dependency>
<!-- xtools-boot-log -->
<dependency>
<groupId>org.xujun</groupId>
<artifactId>xtools-boot-log</artifactId>
</dependency>
<!-- xtools-boot end -->
<!-- Loadbalancer -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,28 @@
package xtools.cloud.call;
import org.springframework.context.annotation.Import;
import xtools.boot.core.utils.ModuleLoadUtils;
import xtools.cloud.call.selector.CloudClientImportSelector;
/**
* <p>Title : CloudClientConfiguration</p>
* <p>Description : CloudClientConfiguration</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(CloudClientImportSelector.class)
public class CloudClientConfiguration {
/**
* 构造方法
*/
public CloudClientConfiguration() {
ModuleLoadUtils.loadSuccess(CloudClientConfiguration.class);
}
}

View File

@@ -0,0 +1,45 @@
package xtools.cloud.call.config;
import org.springframework.boot.restclient.RestClientCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import xtools.cloud.call.interceptor.ClientHttpInterceptor;
/**
* <p>Title : RestClientConfig</p>
* <p>Description : RestClientConfig</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/2/26 15:36
*/
@Configuration
public class RestClientConfig {
/**
* RestClient自定义配置
*
* @return RestClient自定义配置
*/
@Bean
public RestClientCustomizer restClientCustomizer() {
return restClientBuilder -> {
// 配置拦截器
restClientBuilder.requestInterceptor(clientHttpInterceptor());
};
}
/**
* 创建拦截器
*
* @return 拦截器
*/
@Bean
public ClientHttpInterceptor clientHttpInterceptor() {
return new ClientHttpInterceptor();
}
}

View File

@@ -0,0 +1,60 @@
package xtools.cloud.call.interceptor;
import lombok.AllArgsConstructor;
import org.jspecify.annotations.NonNull;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.client.ClientHttpResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* <p>Title : BufferedClientHttpResponse</p>
* <p>Description : BufferedClientHttpResponse</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/2/25 10:24
*/
@AllArgsConstructor
public class BufferedClientHttpResponse implements ClientHttpResponse {
private final ClientHttpResponse original;
private final byte[] body;
@NonNull
@Override
public InputStream getBody() {
// 返回新的 ByteArrayInputStream,可以重复读取
return new ByteArrayInputStream(body);
}
@NonNull
@Override
public HttpHeaders getHeaders() {
return original.getHeaders();
}
@NonNull
@Override
public HttpStatusCode getStatusCode() throws IOException {
return original.getStatusCode();
}
@NonNull
@Override
public String getStatusText() throws IOException {
return original.getStatusText();
}
@Override
public void close() {
original.close();
}
}

View File

@@ -0,0 +1,27 @@
package xtools.cloud.call.interceptor;
import org.jspecify.annotations.NonNull;
import org.springframework.http.HttpRequest;
/**
* <p>Title : CallInterceptor</p>
* <p>Description : CallInterceptor</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/2/27 14:13
*/
public interface CallInterceptor {
/**
* 请求之前
*
* @param request 请求
*/
default void before(@NonNull HttpRequest request) {
}
}

View File

@@ -0,0 +1,100 @@
package xtools.cloud.call.interceptor;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.jspecify.annotations.NonNull;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.StreamUtils;
import xtools.boot.api.constant.BootCommonConstant;
import xtools.boot.api.model.dto.log.LogTrack;
import xtools.boot.log.LogBus;
import xtools.boot.log.enums.LogBusBaseType;
import xtools.boot.log.holder.LogTrackHolder;
import xtools.core.enums.LogLevel;
import java.io.IOException;
import java.util.Objects;
/**
* <p>Title : ClientHttpInterceptor</p>
* <p>Description : ClientHttpInterceptor</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/2/26 15:37
*/
@Slf4j
public class ClientHttpInterceptor implements ClientHttpRequestInterceptor {
@Resource
private CallInterceptor callInterceptor;
/**
* 拦截请求
*
* @param request 请求
* @param body 请求体
* @param execution 执行器
* @return 响应
* @throws IOException 异常
*/
@Override
@NonNull
public ClientHttpResponse intercept(
@NonNull HttpRequest request,
byte @NonNull [] body,
@NonNull ClientHttpRequestExecution execution
) throws IOException {
// 请求前执行
if (Objects.nonNull(callInterceptor)) {
callInterceptor.before(request);
}
// 获取请求头信息
HttpHeaders headers = request.getHeaders();
// 获取日志追踪信息
LogTrack logTrack = LogTrackHolder.get();
// 添加日志追踪信息
headers.add(BootCommonConstant.LOG_TRACK, LogTrackHolder.getBase64(logTrack));
// 添加微服务请求标识
headers.add(BootCommonConstant.CLOUD, String.valueOf(true));
// 创建请求日志
JSONObject reqLog = new JSONObject();
reqLog.put("uri", request.getURI());
reqLog.put(BootCommonConstant.UID, headers.get(BootCommonConstant.UID));
reqLog.put(BootCommonConstant.AUTHORIZATION, headers.get(BootCommonConstant.AUTHORIZATION));
reqLog.put("method", request.getMethod().name());
reqLog.put("body", new String(body));
LogBus.init(LogLevel.INFO, LogBusBaseType.CLOUD_REQUEST, logTrack).data(reqLog).save();
// 执行请求
long startTime = System.currentTimeMillis();
ClientHttpResponse originalResponse = execution.execute(request, body);
long execTime = System.currentTimeMillis() - startTime;
// 读取响应体
byte[] responseBody = StreamUtils.copyToByteArray(originalResponse.getBody());
String respBody = new String(responseBody);
// 创建响应日志
JSONObject respLog = new JSONObject();
respLog.put("execTime", execTime);
respLog.put("statusCode", originalResponse.getStatusCode());
respLog.put("body", JSON.isValid(respBody) ? JSON.parseObject(respBody) : respBody);
LogBus.init(LogBusBaseType.CLOUD_RESPONSE).data(respLog).save();
// 返回响应
return new BufferedClientHttpResponse(originalResponse, responseBody);
}
}

View File

@@ -0,0 +1,36 @@
package xtools.cloud.call.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 : CloudClientImportSelector</p>
* <p>Description : CloudClientImportSelector</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 CloudClientImportSelector 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.call");
}
}

View File

@@ -0,0 +1 @@
xtools.cloud.call.CloudClientConfiguration