Druid Spring Boot Starter
用于帮助你在 Spring Boot 项目中轻松集成 Druid 数据库连接池和监控。
引入依赖
vte-dal 模块下的 pom.xml 下加入以下依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.23</version>
</dependency>
增加 Druid 配置
application-dev.properties 增加监控配置
#druid servlet 监控页面开启
spring.datasource.druid.stat-view-servlet.enabled=true
其他配置采用默认配置或者使用配置类进行配置注入
- 配置注入类
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class DruidConfig {
//用户登录配置
/**
* @description 注册一个StatViewServlet,进行druid监控页面配置
* @return servlet registration bean
*/
@Bean
public ServletRegistrationBean druidStatViewServlet() {
//先配置管理后台的servLet,访问的入口为/druid/
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(
new StatViewServlet(), "/druid/*");
// IP白名单 (没有配置或者为空,则允许所有访问)
servletRegistrationBean.addInitParameter("allow", "localhost,127.0.0.1");
// IP黑名单 (存在共同时,deny优先于allow)
servletRegistrationBean.addInitParameter("deny", "");
servletRegistrationBean.addInitParameter("loginUsername", "admin");
servletRegistrationBean.addInitParameter("loginPassword", "admin");
servletRegistrationBean.addInitParameter("resetEnable", "false");
return servletRegistrationBean;
}
/**
* @description 注册一个过滤器,允许页面正常浏览
* @return filter registration bean
*/
@Bean
public FilterRegistrationBean druidStatFilter(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(
new WebStatFilter());
// 添加过滤规则.
filterRegistrationBean.addUrlPatterns("/*");
// 添加不需要忽略的格式信息.
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}
}
监控相关配置
数据源 DbConfiguration 配置中引入对应的拦截器接口,对于多数据源,注意配置不同的 bean(name =”XXXX”)
,参见代码中的“druid 监控配置拦截器加入”部分代码,主要进行 SQL 的记录和监控。
@Bean(name = "dataSource", initMethod = "init", destroyMethod = "close")
public DruidDataSource dataSource() throws Exception {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setUrl(dbProperties.getUrl());
druidDataSource.setUsername(dbProperties.getUsername());
druidDataSource.setPassword(dbProperties.getPassword());
druidDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
druidDataSource.setInitialSize(dbProperties.getInitialSize());
druidDataSource.setMaxActive(dbProperties.getMaxActive());
druidDataSource.setMinIdle(dbProperties.getInitialSize());
druidDataSource.setMaxWait(10000);
druidDataSource.setValidationQuery("select 1");
druidDataSource.setValidationQueryTimeout(1);
druidDataSource.setTimeBetweenEvictionRunsMillis(300000);
druidDataSource.setKeepAliveBetweenTimeMillis(600000);
druidDataSource.setMinEvictableIdleTimeMillis(600000);
druidDataSource.setTestOnBorrow(true);
druidDataSource.setTestOnReturn(false);
druidDataSource.setTestWhileIdle(true);
druidDataSource.setKeepAlive(true);
druidDataSource.setConnectionInitSqls(Collections.singletonList("SET NAMES utf8mb4"));
// log.info("vte_server db init success!");
//druid 监控配置拦截器加入
List<Filter> filters = new ArrayList<>();
filters.add(statFilter());
filters.add(logFilter());
// filters.add(wallFilter());
druidDataSource.setProxyFilters(filters);
return druidDataSource;
}
....
/**
* @description 配置慢sql拦截器
* @return
*/
@Bean(name = "statFilter")
public StatFilter statFilter(){
StatFilter statFilter = new StatFilter();
//慢sql时间设置,即执行时间大于200毫秒的都是慢sql
statFilter.setSlowSqlMillis(500);
statFilter.setLogSlowSql(true);
statFilter.setMergeSql(true);
return statFilter;
}
/**
* @description 配置日志拦截器
* @return
*/
@Bean(name = "logFilter")
public Slf4jLogFilter logFilter(){
Slf4jLogFilter slf4jLogFilter = new Slf4jLogFilter();
slf4jLogFilter.setDataSourceLogEnabled(true);
slf4jLogFilter.setStatementExecutableSqlLogEnable(true);
return slf4jLogFilter;
}
/**
* @description 配置防止注入拦截器
* @return
*/
@Bean(name = "wallFilter")
public WallFilter wallFilter(){
WallFilter wallFilter = new WallFilter();
return wallFilter;
}
开启 Spring 监控
- 增加配置类即可
import com.alibaba.druid.support.spring.stat.DruidStatInterceptor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.JdkRegexpMethodPointcut;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Scope;
/**
* druid Spring 监控配置
* @Author: xieteng-meowQAQ
* @Date: 2023/6/25 11:47
* @Description:
*/
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class DruidAspectConfig {
@Bean
public DruidStatInterceptor druidStatInterceptor() {
DruidStatInterceptor dsInterceptor = new DruidStatInterceptor();
return dsInterceptor;
}
@Bean
@Scope("prototype")
public JdkRegexpMethodPointcut druidStatPointcut() {
JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
//AOP 切入点
pointcut.setPatterns("com.XXX.dal.mapper.*", "com.XXXX.server.*");
return pointcut;
}
@Bean
public DefaultPointcutAdvisor druidStatAdvisor(DruidStatInterceptor druidStatInterceptor, JdkRegexpMethodPointcut druidStatPointcut) {
DefaultPointcutAdvisor defaultPointAdvisor = new DefaultPointcutAdvisor();
defaultPointAdvisor.setPointcut(druidStatPointcut);
defaultPointAdvisor.setAdvice(druidStatInterceptor);
return defaultPointAdvisor;
}
}
2.2.1 问题解决
- Error creating bean with name ‘org.springframework.aop.config.internalAutoProxyCreator’: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Pointcut
问题原因:缺失依赖
解决方式:增加依赖
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>