由于整合的Druid版本不断升级,导致很多的配置都发生了不同的变化,使用老配置会导致一些监控失效。本文存在的意义就是记录一个可用的版本,后面升级均可参考本文进行修改。
Druid整合
Druid Maven依赖
<!-- Druid数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>
<!-- 介于Druid需要Spring监控,需要Aop动态代理的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
Druid配置类
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
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 javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* @author : zanglikun
* @date : 2021/9/25 17:11
* @Version: 1.0
* @Desc : Druid 配置文件
*/
@Configuration
public class DruidConfig {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druidDataSource() {
return new DruidDataSource();
}
// 配置Druid的监控
// 1、配置一个管理后台的Servlet
@Bean
//@ConditionalOnProperty(prefix = "spring.datasource.druid.filter.stat", name = "enable")
public ServletRegistrationBean statViewServlet() {
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
Map<String, String> initParams = new HashMap<>();
initParams.put("loginUsername", "admin");
initParams.put("loginPassword", "123456");
initParams.put("allow", ""); //默认就是允许所有访问
initParams.put("deny", ""); //拒绝某IP访问
bean.setInitParameters(initParams);
return bean;
}
// 2、配置一个web监控的过滤器filter
@Bean
public FilterRegistrationBean webStatFilter() {
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
Map<String, String> initParams = new HashMap<>();
//initParams.put("exclusions","*.js,*.css,/druid/*");
initParams.put("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
bean.setInitParameters(initParams);
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}
}
Druid配置文件
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/你的数据库名?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&useSSL=false&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
# 使用Druid驱动连接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# Druid详细配置
# min-idle 最少保留线程数
# max-active 最大线程数
# initial-size 线程池初始线程数
# async-init=true Druid 异步初始化。(第一次在Druid面板点击数据源是看不到信息的,只有执行一次SQL后,才能看到)
spring.datasource.druid.async-init=true
spring.datasource.druid.initial-size=5
spring.datasource.druid.max-active=20
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-wait=60000
# 据说Ocricle SELECT 1 FROM DUAL Mysql用 SELECT 1
spring.datasource.validationQuery=SELECT 1
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.max-open-prepared-statements= #和上面的等价
# 开启Sql监控 (注意Druid升级版本后 下面的filter在监控页面看不到,需要去除druid才能看得到)
#spring.datasource.druid.filters=stat,wall,slf4j
spring.datasource.filters=stat,wall,slf4j
spring.datasource.druid.filter.stat.enabled=true
spring.datasource.druid.filter.stat.db-type=mysql
# Druid 慢日志 可以用sql: SELECT SLEEP(10)即可获得10秒的慢sql
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=10
# 开启Spring监控 (注意多个路径以逗号(,) 隔开,如果直接项目.*就可以看到大量的Bean的加载时间,但是我们更多关注我们方法执行时间!)
spring.datasource.druid.aop-patterns=com.xunliao.redenvelop.controller.*,com.xunliao.redenvelop.service.*,com.xunliao.redenvelop.mapper.*
配置完成后,就可以启动访问使用了!!!
下文附上:配置类加入的Spring监控,本配置类内容 等价于配置文件 spring.datasource.druid.aop-patterns=com.zhangsan.abc.*,com.lisi.efg.*
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;
/**
* @Author: zanglikun
* @Date: 2022/5/9 10:13
* @Description: 将Druid的Spring监控的抽离出来,本配置类依赖于AOP,请自行引入Aop依赖 总的来说这个配置类 等价于:配置文件spring.datasource.druid.aop-patterns=com.xunliao.redenvelop.*
* 参考于:https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_Druid%E5%92%8CSpring%E5%85%B3%E8%81%94%E7%9B%91%E6%8E%A7%E9%85%8D%E7%BD%AE
*/
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class DruidSpringConfig {
// 配置一个Spring监控的拦截器interceptor
@Bean
public DruidStatInterceptor druidStatInterceptor() {
DruidStatInterceptor bean = new DruidStatInterceptor();
return bean;
}
// 指定切点
@Bean
@Scope("prototype")
public JdkRegexpMethodPointcut druidStatPointcut() {
JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
// 修改切点的包名,一般是直接拦截所有的方法
pointcut.setPatterns("com.xunliao.redenvelop.*");
return pointcut;
}
@Bean
public DefaultPointcutAdvisor druidStatAdvisor(DruidStatInterceptor druidStatInterceptor, JdkRegexpMethodPointcut druidStatPointcut) {
DefaultPointcutAdvisor defaultPointAdvisor = new DefaultPointcutAdvisor();
defaultPointAdvisor.setPointcut(druidStatPointcut);
defaultPointAdvisor.setAdvice(druidStatInterceptor);
return defaultPointAdvisor;
}
}
特殊说明: 以上文章,均是我实际操作,写出来的笔记资料,不会盗用别人文章!烦请各位,请勿直接盗用!转载记得标注来源!