How to Configure Druid DataSource and Monitoring in Spring Boot
This guide walks you through adding Alibaba's Druid connection pool to a Spring Boot project, configuring its properties, enabling built‑in monitoring via Actuator, and accessing the Druid dashboard to observe datasource and SQL performance metrics.
Configure Druid DataSource
First, add the Druid Spring Boot starter to pom.xml:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>Then, set the connection details in application.properties using the spring.datasource.druid prefix:
spring.datasource.druid.url=jdbc:mysql://localhost:3306/test
spring.datasource.druid.username=root
spring.datasource.druid.password=
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.DriverConfigure the pool parameters (initialSize, maxActive, maxWait, etc.) as needed:
spring.datasource.druid.initialSize=10
spring.datasource.druid.maxActive=20
spring.datasource.druid.maxWait=60000
spring.datasource.druid.minIdle=1
spring.datasource.druid.timeBetweenEvictionRunsMillis=60000
spring.datasource.druid.minEvictableIdleTimeMillis=300000
spring.datasource.druid.testWhileIdle=true
spring.datasource.druid.testOnBorrow=true
spring.datasource.druid.testOnReturn=false
spring.datasource.druid.poolPreparedStatements=true
spring.datasource.druid.maxOpenPreparedStatements=20
spring.datasource.druid.validationQuery=SELECT 1
spring.datasource.druid.validation-query-timeout=500
spring.datasource.druid.filters=statThese settings control the size, timeout, validation, and monitoring behavior of the Druid pool.
Enable Druid Monitoring
Add the Actuator starter to pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>Configure the monitoring servlet in application.properties:
spring.datasource.druid.stat-view-servlet.enabled=true
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
spring.datasource.druid.stat-view-servlet.reset-enable=true
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=adminCreate a REST controller to exercise the datasource (example shown below):
@Data
@AllArgsConstructor
@RestController
public class UserController {
private UserService userService;
@PostMapping("/user")
public int create(@RequestBody User user) {
return userService.create(user.getName(), user.getAge());
}
@GetMapping("/user/{name}")
public List<User> getByName(@PathVariable String name) {
return userService.getByName(name);
}
@DeleteMapping("/user/{name}")
public int deleteByName(@PathVariable String name) {
return userService.deleteByName(name);
}
@GetMapping("/user/count")
public int getAllUsers() {
return userService.getAllUsers();
}
@DeleteMapping("/user/all")
public int deleteAllUsers() {
return userService.deleteAllUsers();
}
}Run the application and open http://localhost:8080/druid/. Log in with the credentials configured above to view the Druid dashboard.
Key Monitoring Pages
DataSource : Shows pool size, active connections, and usage statistics.
SQL Monitoring : Lists executed SQL statements, their frequencies, execution times, and row counts.
SQL Firewall : Provides statistics for SQL injection protection when the wall filter is enabled (add spring.datasource.druid.filters=stat,wall).
Note that the monitoring data reflects the current application instance, not the entire database server.
Code Repository
All examples are available in the chapter3-3 directory of the following repositories:
GitHub: https://github.com/dyc87112/SpringBoot-Learning/
Gitee: https://gitee.com/didispace/SpringBoot-Learning/
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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
