Master Druid: Configure, Monitor, and Optimize Spring Boot Data Sources
This guide explains how to integrate Alibaba's Druid connection pool into Spring Boot, covering basic concepts, Maven dependencies, essential configuration properties, built‑in filters, monitoring pages, SQL and slow‑query logging, Spring AOP monitoring, ad removal, and programmatic access to Druid statistics.
1. Basic Concepts
Druid is Alibaba's open‑source Java database connection pool designed for monitoring and extensibility. It offers richer monitoring features than HikariCP, which is slightly faster but less feature‑rich.
Druid provides built‑in filters such as StatFilter for statistics, WallFilter for SQL injection protection, and Log4j2 for logging.
2. Adding Maven Dependencies
<!-- Alibaba Druid starter -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.23</version>
</dependency>
<!-- MySQL driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Log4j2 for SQL logging -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- Exclude default HikariCP when using MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>3. Configuring Druid Properties
All Druid settings can be placed in application.yml. Key sections include JDBC basics, pool size, timeout, validation query, prepared‑statement caching, and filter activation.
spring:
datasource:
username: xxx
password: xxx
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=Asia/Shanghai
platform: mysql
type: com.alibaba.druid.pool.DruidDataSource
druid:
initial-size: 5
minIdle: 10
max-active: 20
max-wait: 60000
time-between-eviction-runs-millis: 2000
min-evictable-idle-time-millis: 600000
max-evictable-idle-time-millis: 900000
validationQuery: select 1
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
keepAlive: true
aop-patterns: "com.springboot.template.dao.*"
filters: stat,wall,log4j2
filter:
stat:
enabled: true
db-type: mysql
log-slow-sql: true
slow-sql-millis: 2000
web-stat-filter:
enabled: true
url-pattern: "/*"
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
session-stat-enable: true
session-stat-max-count: 1000
stat-view-servlet:
enabled: true
url-pattern: /druid/*
reset-enable: false
login-username: root
login-password: 123
allow: 127.0.0.1These properties map to
com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatPropertiesand
org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.
3.1 Configuring Filters
Filters can be enabled via spring.datasource.druid.filters or configured individually, e.g.:
# StatFilter
spring.datasource.druid.filter.stat.enabled=true
spring.datasource.druid.filter.stat.db-type=h2
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=2000
# WallFilter
spring.datasource.druid.filter.wall.enabled=true
spring.datasource.druid.filter.wall.db-type=h2
spring.datasource.druid.filter.wall.config.delete-allow=false
spring.datasource.druid.filter.wall.config.drop-table-allow=false4. SQL Monitoring
The WebStatFilter collects web‑related SQL statistics such as execution time, request count, and accessed tables.
spring:
datasource:
druid:
web-stat-filter:
enabled: true
url-pattern: "/*"
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
session-stat-enable: true
session-stat-max-count: 10005. Slow SQL Recording
Enable slow‑SQL logging by configuring the StatFilter:
spring:
datasource:
druid:
filter:
stat:
enabled: true
db-type: mysql
log-slow-sql: true
slow-sql-millis: 2000SQL statements exceeding 2 seconds are logged.
6. Spring AOP Monitoring
Include the AOP starter and set the pointcut pattern to monitor DAO methods:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
spring.datasource.druid.aop-patterns="com.springboot.template.dao.*"7. Removing Druid Footer Ads
Two approaches are provided:
Manually comment out the // this.buildFooter(); line in common.js inside the Druid JAR.
Register a custom filter that intercepts the common.js request and strips the ad HTML via regular expressions.
Both methods work, but editing the JAR source is the most straightforward.
8. Accessing Druid Statistics Programmatically
After enabling StatFilter, you can retrieve monitoring data via DruidStatManagerFacade:
@RestController
@RequestMapping("/druid")
public class DruidStatController {
@GetMapping("/stat")
public Object druidStat() {
return DruidStatManagerFacade.getInstance().getDataSourceStatDataList();
}
}The facade also offers other methods for detailed metrics.
The article concludes with a reminder that the configuration shown works for MySQL; adjust db-type for other databases.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect
Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
