doc/architecture.md
Druid 由四大核心子系统组成,各子系统通过清晰的 API 边界协作:
┌──────────────────────────────────────────────────────────┐
│ Application Layer │
│ (Spring Boot / Direct JDBC) │
└────────────────────────┬─────────────────────────────────┘
│
┌────────────────────────▼─────────────────────────────────┐
│ DruidDataSource │
│ (JDBC Connection Pool Core) │
│ ┌─────────────┐ ┌──────────┐ ┌──────────────────┐ │
│ │ Connection │ │PoolLock │ │KeepAlive/Evict │ │
│ │ Management │ │& Fairness│ │ Threads │ │
│ └─────────────┘ └──────────┘ └──────────────────┘ │
└────────────────────────┬─────────────────────────────────┘
│
┌────────────────────────▼─────────────────────────────────┐
│ Filter Chain │
│ ┌────────┐ ┌──────────┐ ┌──────────┐ ┌─────────────┐ │
│ │ Stat │→│ Wall │→│ Log │→│ Custom │ │
│ │ Filter │ │ Filter │ │ Filter │ │ Filter │ │
│ └────────┘ └──────────┘ └──────────┘ └─────────────┘ │
└────────────────────────┬─────────────────────────────────┘
│
┌────────────────────────▼─────────────────────────────────┐
│ SQL Parser Engine │
│ ┌────────┐ ┌────────┐ ┌─────────┐ ┌─────────────┐ │
│ │ Lexer │→ │ Parser │→ │ AST │→ │ Visitors │ │
│ └────────┘ └────────┘ └─────────┘ └─────────────┘ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Dialect Registry (30 dialects) │ │
│ └─────────────────────────────────────────────────────┘ │
└──────────────────────────────────────────────────────────┘
连接池是 Druid 最核心的组件,负责管理物理数据库连接的生命周期。
核心类: DruidDataSource
关键机制:
连接状态流转:
创建 → 空闲(idle) → 活跃(active) → 空闲(idle) → ... → 销毁
↑ │
└──────────────────────┘
recycle
Filter-Chain 是 Druid 的拦截器机制,采用责任链模式,在 JDBC 操作的关键节点(获取连接、执行 SQL、关闭连接等)插入自定义逻辑。
核心接口: Filter
内置 Filter:
| Filter | 功能 |
|---|---|
StatFilter | 采集 SQL 执行统计(执行次数、耗时、慢 SQL 等) |
WallFilter | SQL 安全防护,基于 AST 分析拦截危险 SQL |
Slf4jLogFilter | 通过 SLF4J 输出 SQL 执行日志 |
Log4jFilter / Log4j2Filter | 通过 Log4j/Log4j2 输出日志 |
CommonsLogFilter | 通过 Commons Logging 输出日志 |
ConfigFilter | 支持配置文件加密解密 |
EncodingConvertFilter | 字符编码转换 |
执行流程:
Application → Filter_1.beforeExecute()
→ Filter_2.beforeExecute()
→ ...
→ JDBC Driver (实际执行)
→ ...
→ Filter_2.afterExecute()
→ Filter_1.afterExecute()
→ Application
SQL 解析器是 Druid 的另一核心子系统,提供将 SQL 文本解析为结构化 AST 的能力。
处理流水线:
SQL Text → Lexer(词法分析) → Token Stream → Parser(语法分析) → AST → Visitor(遍历/转换)
各阶段说明:
SQLStatement、SQLExpr、SQLTableSource方言注册机制:
DbType → SQLParserUtils.createParser() → Dialect-specific Parser
→ SQLUtils.createOutputVisitor() → Dialect-specific Visitor
每种方言通过 META-INF/druid/parser/<dialect>/ 下的配置文件注册关键字、数据类型和特性开关。
数据采集: StatFilter 在 Filter-Chain 中拦截所有 JDBC 操作,记录:
数据暴露:
DruidStatManagerFacade — 编程接口,获取统计数据StatViewServlet — Web 监控页面(/druid/index.html)DruidStatManagerFacade 输出 JSON 对接外部监控DruidDataSource # 连接池核心类
├── CreateConnectionThread (内部类) # 异步创建连接线程
├── DestroyConnectionThread (内部类) # 连接回收线程
└── LogStatsThread (内部类) # 定期日志统计线程
DruidConnectionHolder # 连接持有者,包装物理连接
DruidPooledConnection # 池化连接,应用层使用
Filter (interface)
└── FilterAdapter # Filter 适配器基类
├── WallFilter # 安全防火墙 Filter
├── EncodingConvertFilter # 字符编码转换 Filter
├── ConfigFilter # 配置加密解密 Filter
└── FilterEventAdapter # 事件适配器
├── StatFilter # 统计 Filter
└── LogFilter (abstract) # 日志 Filter 基类
├── Slf4jLogFilter
├── Log4jFilter
├── Log4j2Filter
└── CommonsLogFilter
SQLStatement (AST root)
├── SQLSelectStatement # SELECT 语句
├── SQLInsertStatement # INSERT 语句
├── SQLUpdateStatement # UPDATE 语句
├── SQLDeleteStatement # DELETE 语句
├── SQLCreateTableStatement # CREATE TABLE 语句
└── ... (100+ statement types)
Druid is composed of four core subsystems that collaborate through well-defined API boundaries:
┌──────────────────────────────────────────────────────────┐
│ Application Layer │
│ (Spring Boot / Direct JDBC) │
└────────────────────────┬─────────────────────────────────┘
│
┌────────────────────────▼─────────────────────────────────┐
│ DruidDataSource │
│ (JDBC Connection Pool Core) │
│ ┌─────────────┐ ┌──────────┐ ┌──────────────────┐ │
│ │ Connection │ │PoolLock │ │KeepAlive/Evict │ │
│ │ Management │ │& Fairness│ │ Threads │ │
│ └─────────────┘ └──────────┘ └──────────────────┘ │
└────────────────────────┬─────────────────────────────────┘
│
┌────────────────────────▼─────────────────────────────────┐
│ Filter Chain │
│ ┌────────┐ ┌──────────┐ ┌──────────┐ ┌─────────────┐ │
│ │ Stat │→│ Wall │→│ Log │→│ Custom │ │
│ │ Filter │ │ Filter │ │ Filter │ │ Filter │ │
│ └────────┘ └──────────┘ └──────────┘ └─────────────┘ │
└────────────────────────┬─────────────────────────────────┘
│
┌────────────────────────▼─────────────────────────────────┐
│ SQL Parser Engine │
│ ┌────────┐ ┌────────┐ ┌─────────┐ ┌─────────────┐ │
│ │ Lexer │→ │ Parser │→ │ AST │→ │ Visitors │ │
│ └────────┘ └────────┘ └─────────┘ └─────────────┘ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Dialect Registry (30 dialects) │ │
│ └─────────────────────────────────────────────────────┘ │
└──────────────────────────────────────────────────────────┘
The connection pool is Druid's core component, managing the lifecycle of physical database connections.
Core class: DruidDataSource
Key mechanisms:
The Filter-Chain implements the Chain of Responsibility pattern, intercepting JDBC operations at key points (connection acquisition, SQL execution, connection close, etc.).
Core interface: Filter
Built-in Filters:
| Filter | Purpose |
|---|---|
StatFilter | Collects SQL execution statistics (count, duration, slow SQL) |
WallFilter | SQL injection protection via AST analysis |
Slf4jLogFilter | SLF4J-based SQL execution logging |
Log4jFilter / Log4j2Filter | Log4j/Log4j2-based logging |
CommonsLogFilter | Commons Logging-based logging |
ConfigFilter | Configuration encryption/decryption |
EncodingConvertFilter | Character encoding conversion |
The SQL parser processes SQL text into a structured AST:
SQL Text → Lexer → Token Stream → Parser → AST → Visitor
SQLStatement, SQLExpr, SQLTableSource)Collection: StatFilter intercepts all JDBC operations to record execution counts, durations, slow SQL, pool states, and transaction stats.
Exposure: DruidStatManagerFacade (programmatic API), StatViewServlet (Web UI at /druid/index.html), or custom JSON endpoints.