一.打开Druid的监控统计功能 页面效果
二、ssm整合druid监控功能
1.打开监控功能 Druid内置提供一个StatFilter,用于统计监控信息。
(参考文档: https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_StatFilter)
1.1. 别名配置
StatFilter的别名是stat,这个别名映射配置信息保存在druid-xxx.jar!/META- INF/druid-filter.properties。 在spring中使用别名配置方式如下:
//如果Properties文件的属性名命名符合configFromPropety的参数Properties的命名规 则,则自动赋值
@Bean
public DruidDataSource getDataSource(){ Properties props = new Properties();
try {
props.load(SpringMybatisConfig.class.
getClassLoader().getResourceAsStream("db.properties"));
DruidDataSource dataSource = new DruidDataSource();
dataSource.configFromPropety(props);
dataSource.setFilters("stat");
//配置默认监控filter
return dataSource;
}
catch (IOException e) { e.printStackTrace(); }
catch (SQLException e) {
e.printStackTrace();
}
return null;
}
1.2. 组合配置 StatFilter可以和其他的Filter配置使用,比如:
//如果Properties文件的属性名命名符合configFromPropety的参数Properties的命名规 则,则自动赋值
@Bean
public DruidDataSource getDataSource(){
Properties props = new Properties();
try { props.load(SpringMybatisConfig.class.getClassLoader().
getResourceAsStream("d b.properties"));
DruidDataSource dataSource = new DruidDataSource();
dataSource.configFromPropety(props);
dataSource.setFilters("stat,wall,slf4j");
dataSource.setConnectionProperties("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=1000");//开启慢sql监控,大于1000毫秒的为慢sql,默认是3秒
return dataSource; }
catch (IOException e) { e.printStackTrace(); }
catch (SQLException e) { e.printStackTrace(); }
return null; }
在上面的配置中,StatFilter和WallFilter、SlfFilter组合使用。
WallFilter:防止sql注入的过滤器
(参考文档:https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE- wallfilter) Slf4jFileter:日志记录JDBC执行的SQL
(参考文档:https://github.com/alibaba/druid/wiki/配置_LogFilter)
2.使用Druid的内置监控页面 要访问druid的内置监控页面,需要配置一个servlet
(参考文档: https://github.com/alibaba/druid/wiki/配置_StatViewServlet配置)
@WebServlet(urlPatterns = "/druid/*",initParams = {
@WebInitParam(name="allow",value = ""),//ip白名单,没有配置或空, 则允许所有访问 @WebInitParam(name="deny",value = ""),//ip黑名单
@WebInitParam(name = "loginUsername",value = "admin"),//用户名
@WebInitParam(name="loginPassword",value = "123456"),//密码
@WebInitParam(name="resetEnable",value="false")//禁用html页面的reset all功能 })
public class DruidStatueServlet extends StatViewServlet { }
或者在spring配置类中:
/** * 注册一个StatViewServlet ** @return */
@Bean public ServletRegistrationBean druidServlet() {
ServletRegistrationBean reg = new ServletRegistrationBean();
reg.setServlet(new StatViewServlet());
reg.addUrlMappings(new String[]{"/druid/*"}); //IP白名单 (没有配置或者为空,则允许所有访问)
reg.addInitParameter("allow", ""); //是否能够重置数据
reg.addInitParameter("resetEnable", "false"); //设置账号密码
reg.addInitParameter("loginUsername", "admin");
reg.addInitParameter("loginPassword", "123456");
return reg; }
开启后即可以启动服务器,并且通过地址栏访问监控页面,
访问规则: ip:端口号/应用名/druid/login.html
输入上一步设置的账户密码即可登录
3.设置web监控
WebStatFilter用于采集web-jdbc关联监控的数据
(参考文档: https://github.com/alibaba/druid/wiki/配置_配置WebStatFilter)
@WebFilter(filterName = "druidStatueFilter",urlPatterns = "/*", initParams = {
@WebInitParam(name="exclusions",value = "*.js,*.jpg,*.png,*.css,/druid/*"),
@WebInitParam(name="profileEnable",value = "true") })
public class DruidStatueFilter extends WebStatFilter { }
或者在spring配置类中:
/** * 注册一个:filterRegistrationBean ** @return */
@Bean public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new WebStatFilter()); //添加过滤规则
filterRegistrationBean.addUrlPatterns(new String[] {"/*"});//添加需要忽略的格式信息
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
filterRegistrationBean.addInitParameter("profileEnable", "true");//配置profileEnable能够 控单个url调用的sql列表
filterRegistrationBean.addInitParameter("principalCookieName", "USER_COOKIE");//如果你的user信息保存在cookie中,你可以配置 //principalCookieName,使得druid知道当前的user是谁 USER_COOKIE为cookie名
filterRegistrationBean.addInitParameter("principalSessionName", "USER_SESSION");//使得druid能够知道当前的session的用户是谁
filterRegistrationBean.addInitParameter("DruidWebStatFilter", "/*");
return filterRegistrationBean; }
4.设置spring监控
@Bean(name="druidStatInterceptor")//设置druid 的 aop切面类
public DruidStatInterceptor getDruidStatInterceptor(){
DruidStatInterceptor druidStatInterceptor = new DruidStatInterceptor();
return druidStatInterceptor; }
@Bean//配置spring监控
public BeanNameAutoProxyCreator getAutoProxyCreator(){
BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator();
beanNameAutoProxyCreator.setProxyTargetClass(true);
beanNameAutoProxyCreator.setBeanNames(new String[] {"*Mapper","*Service"});
beanNameAutoProxyCreator.setInterceptorNames("druidStatInterceptor"); return
beanNameAutoProxyCreator; }