Comprehensive Guide to Configuring Druid DataSource in Spring Boot
This article provides a detailed tutorial on Druid, the Alibaba‑developed Java database connection pool, covering its core concepts, Maven dependencies, extensive Spring Boot configuration options, filter setup, monitoring UI, SQL and slow‑query logging, ad removal techniques, and programmatic access to monitoring data.
1. Basic Concepts
Druid is a Java‑based database connection pool praised for its powerful monitoring and extensibility; while HikariCP may be slightly faster, Druid offers richer features such as StatFilter, WallFilter, and log4j2 integration.
2. Adding Dependencies
<!-- Alibaba Druid datasource -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.23</version>
</dependency>
<!-- MySQL 8 driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Log4j2 for logging -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- MyBatis starter, excluding default HikariCP -->
<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. Configuration Properties
Configure the Druid datasource in application.yml , setting JDBC basics, pool sizes, validation query, test flags, prepared‑statement caching, and enabling built‑in filters ( stat,wall,log4j2 ).
spring:
datasource:
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
slf4j:
enabled: true
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.14. Monitoring Pages
After starting the application, access /druid/login.html to view the built‑in monitoring UI, which includes DataSource basic info, SQL statistics, URL statistics, Spring AOP monitoring, SQL firewall, session details, and a JSON API.
5. SQL Monitoring & Slow‑Query Logging
Enable WebStatFilter to collect request‑level SQL data and configure StatFilter to log slow queries (default threshold 2 seconds).
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: 1000
filter:
stat:
enabled: true
db-type: mysql
log-slow-sql: true
slow-sql-millis: 20006. Removing Druid’s Footer Advertisement
Two approaches are provided: (1) manually comment out the this.buildFooter(); line in the JAR’s common.js ; (2) register a custom servlet filter that rewrites the resource, stripping the ad HTML.
@Configuration
@ConditionalOnWebApplication
@AutoConfigureAfter(DruidDataSourceAutoConfigure.class)
@ConditionalOnProperty(name = "spring.datasource.druid.stat-view-servlet.enabled", havingValue = "true", matchIfMissing = true)
public class RemoveDruidAdConfig {
@Bean
public FilterRegistrationBean removeDruidAdFilterRegistrationBean(DruidStatProperties properties) {
// ... create filter that reads common.js, removes ad snippets, writes back ...
}
}7. Programmatic Access to Monitoring Data
Use DruidStatManagerFacade (e.g., getDataSourceStatDataList() ) or expose a custom @RestController to return the statistics as JSON.
@RestController
@RequestMapping("/druid")
public class DruidStatController {
@GetMapping("/stat")
public Object druidStat() {
return DruidStatManagerFacade.getInstance().getDataSourceStatDataList();
}
}Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.