SpringBoot配置Druid数据监控代码实例
druid,一个为监控而生的数据库连接池,提供可视化界面来查看sql执行情况。
1、pom文件引入druid数据源
com.alibaba druid 1.1.8
2、新建一个DrruidConfig配置文件
packagecom.example.mybatisplus.config;
importcom.alibaba.druid.pool.DruidDataSource;
importcom.alibaba.druid.support.http.StatViewServlet;
importcom.alibaba.druid.support.http.WebStatFilter;
importorg.springframework.boot.context.properties.ConfigurationProperties;
importorg.springframework.boot.web.servlet.FilterRegistrationBean;
importorg.springframework.boot.web.servlet.ServletRegistrationBean;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importjavax.sql.DataSource;
importjava.util.Arrays;
importjava.util.HashMap;
importjava.util.Map;
/**
*druid数据池配置
*/
@Configuration
publicclassDruidConfig{
@ConfigurationProperties(prefix="spring.datasource")
@Bean
publicDataSourcedruid(){
returnnewDruidDataSource();
}
//配置Druid的监控
//1、配置一个管理后台的Servlet
@Bean
publicServletRegistrationBeanstatViewServlet(){
ServletRegistrationBeanbean=newServletRegistrationBean(newStatViewServlet(),"/druid/*");
MapinitParams=newHashMap<>();
//配置登录的账号密码
initParams.put("loginUsername","admin");
initParams.put("loginPassword","123456");
//initParams.put("deny","localhost");//拒绝谁登录
//是否能够重置数据.
//bean.addInitParameter("resetEnable","false");
bean.setInitParameters(initParams);
returnbean;
}
//2、配置一个web监控的filter
@Bean
publicFilterRegistrationBeanwebStatFilter(){
FilterRegistrationBeanbean=newFilterRegistrationBean();
bean.setFilter(newWebStatFilter());
MapinitParams=newHashMap<>();
initParams.put("exclusions","*.html,*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
bean.setInitParameters(initParams);
bean.setUrlPatterns(Arrays.asList("/*"));
returnbean;
}
}
3、yml文件配置
spring: datasource: schema: -classpath:schema.sql initialization-mode:ALWAYS username:root password:root url:jdbc:mysql://localhost:3306/jdbc?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false driver-class-name:com.mysql.cj.jdbc.Driver #type:com.alibaba.druid.pool.DruidDataSource initialSize:5 minIdle:5 maxActive:20 maxWait:60000 timeBetweenEvictionRunsMillis:60000 minEvictableIdleTimeMillis:300000 validationQuery:SELECT1FROMDUAL testWhileIdle:true testOnBorrow:false testOnReturn:false poolPreparedStatements:true #配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 filters:stat,wall maxPoolPreparedStatementPerConnectionSize:20 useGlobalDataSourceStat:true connectionProperties:druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。