文章目录
- Druid
- 概述
- 使用
- 问题解决
- Spring监控不生效
- 方式1:修改yml的配置写法
- 方式2:参考DruidSpringAopConfiguration自行注入@Bean,灵活(更建议)
Druid
概述
官网: https://github.com/alibaba/druid
文档: https://github.com/alibaba/druid/wiki
基础配置列表: https://github.com/alibaba/druid/wiki/DruidDataSource配置属性列表
参考配置: https://github.com/alibaba/druid/wiki/DruidDataSource配置
依赖: https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter
内置过滤器的名字参考: com\alibaba\druid\1.2.15\druid-1.2.15.jar!\META-INF\druid-filter.properties
内置过滤器的名字参考:com\alibaba\druid\1.2.15\druid-1.2.15.jar!\META-INF\druid-filter.properties
配置读取类:com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
Druid支持的数据类型:com.alibaba.druid.DbType
使用
Druid管理访问: http://127.0.0.1:9999/druid/
pom.yml
<!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.15</version>
</dependency>
application.yml
spring:
datasource:
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
druid:
web-stat-filter:
enabled: true
exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
session-stat-enable: true
profile-enable: true
url-pattern: /*
# web页面开启
stat-view-servlet:
enabled: true
login-username: root
login-password: root
url-pattern: /druid/*
filters: stat,wall,mergeStat
# 配置初始化大小、最小、最大
min-idle: 10
initial-size: 5
max-active: 20
# 配置获取连接等待超时的时间
max-wait: 6000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
time-between-eviction-runs-millis: 10000
# 配置一个连接在池中最小生存的时间,单位是毫秒
min-evictable-idle-time-millis: 600000
max-evictable-idle-time-millis: 1000000
validation-query: SELECT 1 FROM DUAL
test-while-idle: true
test-on-borrow: false
test-on-return: false
keep-alive: true
phy-max-use-count: 100
db-type: mysql
# 坑,无排除选项,且值必须是逗号分割的字符串数组,建议参考com.alibaba.druid.spring.boot.autoconfigure.stat.DruidSpringAopConfiguration自行配置注入相关Bean
#aop-patterns: "work.linruchang.*,work.linruchang.config.*,"
SpringConfig.java - 配置Druid的Spring监控
@Configuration
public class SpringConfig implements WebMvcConfigurer {
@Bean
public DruidStatInterceptor druidStatInterceptor() {
DruidStatInterceptor dsInterceptor = new DruidStatInterceptor();
return dsInterceptor;
}
@Bean
public JdkRegexpMethodPointcut druidStatPointcut() {
JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
pointcut.setPattern("work.linruchang.*");//根据实际包名
pointcut.setExcludedPatterns("work.linruchang.kafkaUiLite.conf.*", ".*config.*");
return pointcut;
}
@Bean
public DefaultPointcutAdvisor druidStatAdvisor(DruidStatInterceptor druidStatInterceptor, JdkRegexpMethodPointcut druidStatPointcut) {
DefaultPointcutAdvisor defaultPointAdvisor = new DefaultPointcutAdvisor();
defaultPointAdvisor.setPointcut(druidStatPointcut);
defaultPointAdvisor.setAdvice(druidStatInterceptor);
return defaultPointAdvisor;
}
}
问题解决
Spring监控不生效
方式1:修改yml的配置写法
参考类: com.alibaba.druid.spring.boot.autoconfigure.stat.DruidSpringAopConfiguration
个人怀疑:@ConditionalOnProperty仅能识别到字符串,不能识别yml形式的数组语法
applicaiton.yml == 正确写法
spring:
datasource:
druid:
# 坑,缺少排除选项,且值必须是逗号分割的字符串数组才行,个人建议还是使用自行设置JdkRegexpMethodPointcut拦截切面
aop-patterns: "work.linruchang.*,work.linruchang.config.*"
applicaiton.yml == 错误写法
spring:
datasource:
druid:
# 坑,无排除选项,且值必须是逗号分割的字符串数组,虽然通过@Value能获取到数组,但是不能被@ConditionalOnProperty注解识别到
aop-patterns:
- "work.linruchang.*"
- "work.linruchang.config.*"
方式2:参考DruidSpringAopConfiguration自行注入@Bean,灵活(更建议)
@Configuration
public class SpringConfig implements WebMvcConfigurer {
@Bean
public DruidStatInterceptor druidStatInterceptor() {
DruidStatInterceptor dsInterceptor = new DruidStatInterceptor();
return dsInterceptor;
}
@Bean
public JdkRegexpMethodPointcut druidStatPointcut() {
JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
pointcut.setPattern("work.linruchang.*");//根据实际包名
pointcut.setExcludedPatterns("work.linruchang.kafkaUiLite.conf.*", ".*config.*");
return pointcut;
}
@Bean
public DefaultPointcutAdvisor druidStatAdvisor(DruidStatInterceptor druidStatInterceptor, JdkRegexpMethodPointcut druidStatPointcut) {
DefaultPointcutAdvisor defaultPointAdvisor = new DefaultPointcutAdvisor();
defaultPointAdvisor.setPointcut(druidStatPointcut);
defaultPointAdvisor.setAdvice(druidStatInterceptor);
return defaultPointAdvisor;
}
}