这是一个企业级的动态线程池管理系统,基于Spring Boot构建,提供运行时动态调整线程池参数、实时监控、多种通知渠道等核心功能。该系统解决了传统线程池配置无法动态调整的问题,提高了系统的可运维性和资源利用效率。
- 🚀 动态配置:运行时动态调整线程池核心参数,无需重启应用
- 📊 实时监控:集成Micrometer,提供丰富的监控指标
- 📢 多渠道通知:支持钉钉、邮件等多种通知方式(SPI扩展)
- 🔄 配置持久化:基于Redis的配置存储和消息通信
- 🏗️ 模块化设计:8个独立模块,各司其职,易于扩展和维护
- ⚡ 高性能队列:自定义可变容量阻塞队列,支持动态扩缩容
┌─────────────────────────────────────────────────────────────────┐
│ 动态线程池管理系统架构图 │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Admin │ │ Monitoring │ │ Notify │ │ SPI │ │
│ │ 管理端 │ │ 监控模块 │ │ 通知模块 │ │ 扩展机制 │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ Spring Boot Starter (核心模块) │ │
│ ├─────────────────────────────────────────────────────────────┤ │
│ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ │
│ │ │ Redis │ │ Service │ │ Queue │ │ │
│ │ │ Registry │ │ 业务逻辑 │ │ 可变队列 │ │ │
│ │ └─────────────┘ └─────────────┘ └─────────────┘ │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Application │ │ Thread │ │ Redis │ │ Metrics │ │
│ │ 应用层 │ │ Pool │ │ 缓存 │ │ 监控 │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘ │
└─────────────────────────────────────────────────────────────────┘
| 模块名称 | 职责描述 | 关键功能 |
|---|---|---|
| dynamic-thread-pool-spring-boot-starter | 核心启动器 | 自动配置、核心服务、Redis集成 |
| dynamic-thread-pool-admin | 管理控制台 | REST API、配置管理界面 |
| dynamic-thread-pool-monitoring | 监控指标 | Micrometer集成、指标收集 |
| dynamic-thread-pool-notify-spi | 通知SPI框架 | 通知抽象、工厂模式 |
| dynamic-thread-pool-notify-dingding | 钉钉通知 | 钉钉机器人集成 |
| dynamic-thread-pool-notify-mail | 邮件通知 | SMTP邮件发送 |
| dynamic-thread-pool-core-entity | 核心实体 | 数据模型定义 |
| dynamic-thread-pool-test | 测试应用 | 使用示例、集成测试 |
位置: dynamic-thread-pool-spring-boot-starter
核心职责:
- 线程池配置查询和管理
- 动态参数调整
- 状态信息收集
关键方法:
// 查询所有线程池列表
List<ThreadPoolConfigEntity> queryThreadPoolList();
// 查询指定线程池配置
ThreadPoolConfigEntity queryThreadPoolConfigByName(String threadPoolName);
// 更新线程池配置
void updateThreadPoolConfig(ThreadPoolConfigEntity config);位置: dynamic-thread-pool-spring-boot-starter
核心特性:
- 动态调整队列容量
- 线程安全的容量变更
- 基于双锁策略的高性能实现
关键特性:
// 动态设置队列容量
public void setCapacity(int capacity);
// 获取剩余容量
public int remainingCapacity();位置: dynamic-thread-pool-spring-boot-starter
核心功能:
- 配置信息持久化存储
- 跨应用配置同步
- 实时消息推送
存储结构:
THREAD_POOL_CONFIG_LIST_KEY: 线程池列表缓存 (List)THREAD_POOL_CONFIG_PARAMETER_LIST_KEY_{appName}_{poolName}: 线程池参数缓存 (String)
位置: dynamic-thread-pool-monitoring
监控指标:
dynamic-thread-pool.corePoolSize: 核心线程数dynamic-thread-pool.maximumPoolSize: 最大线程数dynamic-thread-pool.activeCount: 活跃线程数dynamic-thread-pool.poolSize: 当前线程池大小dynamic-thread-pool.queueSize: 队列任务数dynamic-thread-pool.taskCount: 总任务数dynamic-thread-pool.completedTaskCount: 完成任务数dynamic-thread-pool.rejectTaskCount: 拒绝任务数
设计模式: SPI (Service Provider Interface)
支持的通知类型:
- 钉钉通知 (
dynamic-thread-pool-notify-dingding) - 邮件通知 (
dynamic-thread-pool-notify-mail)
扩展机制:
// 自定义通知实现
public class CustomNotify implements Notify {
@Override
public String getType() {
return "custom";
}
@Override
public void sendNotify(String message) {
// 自定义通知逻辑
}
}在您的Spring Boot项目中添加核心依赖:
<dependency>
<groupId>cn.ling.middleware</groupId>
<artifactId>dynamic-thread-pool-spring-boot-starter</artifactId>
<version>1.0</version>
</dependency>@Configuration
public class ThreadPoolConfig {
@Bean("dynamicThreadPool")
public ThreadPoolExecutor dynamicThreadPool() {
return new ThreadPoolExecutor(
5, // 核心线程数
20, // 最大线程数
60, // 空闲线程存活时间
TimeUnit.SECONDS,
new LinkedBlockingQueue<>(100), // 阻塞队列
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.CallerRunsPolicy() // 拒绝策略
);
}
}# application.yml
spring:
application:
name: your-app-name
data:
redis:
host: localhost
port: 6379
database: 0
# 动态线程池配置
dynamic:
thread:
pool:
config:
notify:
type: dingding # 通知类型:dingding/mail# 启动管理应用
cd dynamic-thread-pool-admin
mvn spring-boot:run# 查询线程池列表
curl -X GET "http://localhost:8089/api/v1/dynamic/thread/pool/query_thread_pool_list"
# 查询特定线程池配置
curl -X GET "http://localhost:8089/api/v1/dynamic/thread/pool/query_thread_pool_config?appName=your-app&threadPoolName=dynamicThreadPool"
# 动态调整线程池配置
curl -X POST "http://localhost:8089/api/v1/dynamic/thread/pool/update_thread_pool_config" \
-H "Content-Type: application/json" \
-d '{
"appName": "your-app",
"threadPoolName": "dynamicThreadPool",
"corePoolSize": 10,
"maximumPoolSize": 30
}'dynamic:
thread:
pool:
config:
notify:
type: dingding
dingding:
webhook: "https://oapi.dingtalk.com/robot/send?access_token=xxx"
secret: "your-secret"dynamic:
thread:
pool:
config:
notify:
type: mail
mail:
host: "smtp.qq.com"
port: 587
username: "your-email@qq.com"
password: "your-password"
from: "your-email@qq.com"
to: ["admin@company.com"]management:
endpoints:
web:
exposure:
include: "health,info,prometheus,metrics"
metrics:
export:
prometheus:
enabled: true1. 应用启动 → 2. 自动发现线程池 → 3. 注册监控指标
↓ ↓ ↓
4. 定时上报 → 5. Redis缓存配置 → 6. 管理端查询配置
↓ ↓ ↓
7. 用户调整 → 8. Redis发布消息 → 9. 应用接收更新
↓ ↓ ↓
10. 动态调整 → 11. 任务迁移 → 12. 通知管理员
详细流程说明:
- 应用启动阶段: Spring Boot自动配置扫描所有
ThreadPoolExecutorBean - 指标注册阶段: 为每个线程池注册Micrometer监控指标
- 定时上报阶段: 每20秒上报线程池列表,每5秒更新监控指标
- 配置存储阶段: 线程池配置持久化到Redis,支持跨实例同步
- 动态调整阶段: 通过管理API调整参数,实时生效
- 平滑迁移阶段: 新旧线程池交替,零停机迁移待处理任务
- 通知提醒阶段: 通过SPI机制通知相关人员配置变更
- JDK: 17+
- Redis: 6.0+
- Maven: 3.6+
- Redis集群部署
# Redis配置优化
redis-cli config set tcp-keepalive 300
redis-cli config set timeout 300
redis-cli config set maxmemory 1gb
redis-cli config set maxmemory-policy allkeys-lru- 应用部署
# 打包
mvn clean package -DskipTests
# 启动
java -jar dynamic-thread-pool-admin/target/dynamic-thread-pool-admin-1.0.jar \
--spring.profiles.active=prod \
--server.port=8089- 监控集成
# Prometheus配置
scrape_configs:
- job_name: 'dynamic-thread-pool'
static_configs:
- targets: ['your-app:8080']
metrics_path: '/actuator/prometheus'- 实现Notify接口:
public class WeChatNotify implements Notify {
@Override
public String getType() {
return "wechat";
}
@Override
public void sendNotify(String message) {
// 企业微信通知实现
}
@Override
public String getConfigPrefix() {
return "dynamic.thread.pool.config.notify.wechat";
}
}- 创建SPI配置文件:
# resources/META-INF/services/cn.ling.Notify
cn.ling.middleware.notify.WeChatNotify
- 配置属性:
dynamic:
thread:
pool:
config:
notify:
type: wechat
wechat:
webhook: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx"Q: 线程池配置不生效?
A: 检查Redis连接和应用名称配置,确保spring.application.name已设置
Q: 监控指标不显示?
A: 确认已引入spring-boot-starter-actuator依赖,并暴露相关端点
Q: 通知不发送? A: 检查通知配置和网络连接,确认SPI实现正确注册
-
Redis优化:
- 使用连接池配置
- 设置合理的过期时间
- 监控内存使用情况
-
线程池调优:
- 根据业务负载调整核心线程数
- 设置合理的队列容量
- 选择合适的拒绝策略
-
监控调优:
- 调整上报频率避免过度开销
- 使用采样率控制指标收集粒度
欢迎提交Issue和Pull Request!请遵循以下步骤:
- Fork项目仓库
- 创建功能分支:
git checkout -b feature/amazing-feature - 提交变更:
git commit -m 'Add amazing feature' - 推送分支:
git push origin feature/amazing-feature - 提交Pull Request
本项目采用 Apache 2.0 开源协议。
⭐ 如果这个项目对你有帮助,请给个Star支持一下!