Integrating Alibaba Druid Connection Pool with Spring Boot: Configuration and Monitoring Guide
This article provides a comprehensive guide on integrating the Alibaba Druid JDBC connection pool into a Spring Boot application, covering its components, powerful monitoring features, password encryption, SQL parsing, Maven and YAML configuration, filter setup, and how to access the Druid monitoring console.
1. Introduction
Druid is an open‑source JDBC connection pool from Alibaba, praised for its monitoring capabilities, performance, and extensibility, and is widely used in production environments.
It consists of three main parts: DruidDriver (provides a filter‑chain plugin system), DruidDataSource (the high‑efficiency connection pool), and SQLParser (SQL syntax analysis).
2. Monitoring Features
Druid includes a built‑in StatFilter that records database access performance, such as execution time, result set hold time, row counts, error counts, and error stack traces. It also aggregates SQL execution time into interval distributions, helping identify slow queries.
The pool statistics cover physical connection creation/destruction, logical connection acquisition/release, wait counts, and PSCache hit rates.
3. Password Encryption
Both DruidDriver and DruidDataSource support the PasswordCallback interface, allowing encrypted passwords instead of plain text in configuration files.
4. SQLParser
Druid’s hand‑written high‑performance SQL parser supports MySQL, Oracle, PostgreSQL, and SQL‑92, enabling visitor‑pattern AST analysis, SQL injection defense (WallFilter), SQL merging, formatting, and sharding.
5. Dependency Integration
Add the Spring Boot starter for Druid in pom.xml :
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>Configure the datasource in application.yaml (example shown below):
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/ds0?characterEncoding=utf-8&useSSL=false
username: root
password: 111111
initial-size: 5
min-idle: 5
max-active: 20
max-wait: 60000
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: SELECT 'x'
test-while-idle: true
test-on-borrow: false
test-on-return: false
pool-prepared-statements: false
max-pool-prepared-statement-per-connection-size: -1
use-global-data-source-stat: true
connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
filters: stat,wall,log4j6. Detailed Configuration Options
The table below summarizes the most common Druid properties, their default values, and descriptions (e.g., maxActive – maximum pool size, validationQuery – SQL used to test connection health, filters – plugin aliases such as stat , wall , log4j ).
7. Available Filters
Common filter aliases include:
stat – com.alibaba.druid.filter.stat.StatFilter
mergeStat – com.alibaba.druid.filter.stat.MergeStatFilter
encoding – com.alibaba.druid.filter.encoding.EncodingConvertFilter
log4j – com.alibaba.druid.filter.logging.Log4jFilter
wall – com.alibaba.druid.wall.WallFilter
8. Druid Monitoring Bean Configuration
Register the monitoring servlet and web filter via Java configuration:
@Bean
public ServletRegistrationBean druidServlet() {
ServletRegistrationBean bean = new ServletRegistrationBean();
bean.setServlet(new StatViewServlet());
bean.addUrlMappings("/druid/*");
Map
params = new HashMap<>();
params.put("loginUsername", "admin");
params.put("loginPassword", "admin");
params.put("resetEnable", "false");
params.put("allow", ""); // allow all IPs
bean.setInitParameters(params);
return bean;
}
@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
bean.addUrlPatterns("/*");
bean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return bean;
}Alternatively, configure the same settings in application.properties :
# Druid monitoring login
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=admin
# Enable slow‑SQL logging
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=2000
# Exclude static resources from web‑stat filter
spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*9. Accessing the Monitoring Console
Start the Spring Boot application and open http://127.0.0.1:8090/druid/sql.html . Log in with the configured username and password (default admin/admin ) to view real‑time statistics such as JDK version, driver info, JVM metrics, datasource details, SQL execution counts, time distribution, and more.
10. Source Code
The example project is available at:
https://github.com/aalansehaiyang/spring-boot-bulking
Module: spring-boot-bulking-druidFull-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
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.