如何配置com.alibaba.druid.pool.DruidDataSource的线程池页面监控
时间: 2024-01-26 17:13:25 浏览: 108
SpringBoot .yml
配置com.alibaba.druid.pool.DruidDataSource的线程池页面监控,可以按照以下步骤进行操作:
1. 在pom.xml文件中添加Druid的依赖:
```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.11</version>
</dependency>
```
2. 在application.properties或application.yml文件中添加Druid的配置信息,例如:
```properties
# 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# Druid配置
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-active=20
spring.datasource.druid.max-wait=60000
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.validation-query=SELECT 1 FROM DUAL
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
spring.datasource.druid.pool-prepared-statements=true
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
spring.datasource.druid.filters=stat,wall,log4j
spring.datasource.druid.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
```
3. 在Spring Boot的启动类上添加`@ServletComponentScan`注解,例如:
```java
@SpringBootApplication
@ServletComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
4. 创建一个继承自`StatViewServlet`的Servlet类,用于配置Druid的监控页面,例如:
```java
@WebServlet(urlPatterns = "/druid/*", initParams = {
@WebInitParam(name = "loginUsername", value = "admin"), // 登录用户名
@WebInitParam(name = "loginPassword", value = "admin"), // 登录密码
@WebInitParam(name = "resetEnable", value = "false") // 禁用HTML页面上的“Reset All”功能
})
public class DruidStatViewServlet extends StatViewServlet {
private static final long serialVersionUID = 1L;
}
```
5. 创建一个继承自`WebStatFilter`的Filter类,用于配置Druid的WebStat过滤器,例如:
```java
@WebFilter(filterName = "druidWebStatFilter", urlPatterns = "/*", initParams = {
@WebInitParam(name = "exclusions", value = "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*") // 忽略资源
})
public class DruidWebStatFilter extends WebStatFilter {
}
```
6. 启动Spring Boot应用程序,访问`http://localhost:8080/druid`即可进入Druid的监控页面。
阅读全文