1. sql 监控 无数据
#druid连接池
spring.datasource.type: com.alibaba.druid.pool.DruidDataSource
#最大活跃数
spring.datasource.maxActive: 20
#初始化数量
spring.datasource.initialSize: 1
#最大连接等待超时时间
spring.datasource.maxWait: 60000
#打开PSCache,并且指定每个连接PSCache的大小
spring.datasource.poolPreparedStatements: true
spring.datasource.maxPoolPreparedStatementPerConnectionSize: 20
#通过connectionProperties属性来打开mergeSql功能;慢SQL记录
#connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
spring.datasource.minIdle: 1
spring.datasource.timeBetweenEvictionRunsMillis: 60000
spring.datasource.minEvictableIdleTimeMillis: 300000
spring.datasource.validationQuery: select 1 from dual
spring.datasource.testWhileIdle: true
spring.datasource.testOnBorrow: false
spring.datasource.testOnReturn: false
#配置监控统计拦截的filters,去掉后监控界面sql将无法统计,'wall'用于防火墙
filters: stat, wall, log4j
关键最后一句:filters: stat, wall, log4j
DruidConfiguration
/**
* 说明:druid数据库连接池的配置类(必须与启动类包目录同级)
* 官网:www.1b23.com
*/
@Configuration
public class DruidConfiguration {
@Bean
public ServletRegistrationBean<StatViewServlet> statViewServlet() {
// 创建servlet注册实体
ServletRegistrationBean<StatViewServlet> servletRegistrationBean = new ServletRegistrationBean<StatViewServlet>(new StatViewServlet(), "/druid/*");
// 设置ip白名单
//servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
// 设置ip黑名单,如果allow与deny共同存在时,deny优先于allow
//servletRegistrationBean.addInitParameter("deny", "192.168.0.1");
// 设置控制台管理用户
//servletRegistrationBean.addInitParameter("loginUsername", "druid");
//servletRegistrationBean.addInitParameter("loginPassword", "admin");
// 是否可以重置数据
servletRegistrationBean.addInitParameter("resetEnable", "false");
return servletRegistrationBean;
}
@Bean
public FilterRegistrationBean<WebStatFilter> statFilter() {
// 创建过滤器
FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<WebStatFilter>(new WebStatFilter());
// 设置过滤器过滤路径
filterRegistrationBean.addUrlPatterns("/*");
// 忽略过滤的形式
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}
}
如果是多数据源的,需要配置
@Bean(name = "masterDataSource")
@Primary //www.1b23.com
public DataSource masterDataSource() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
dataSource.addFilters("stat");
dataSource.addFilters("wall");
return dataSource;
}
关键这两句;
dataSource.addFilters("stat");
dataSource.addFilters("wall");
2. spring 监控 无数据
加入配置类
package org.fh.config;
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.Scope;
import com.alibaba.druid.support.spring.stat.DruidStatInterceptor;
/**
* 说明:druid sring监控
* 官网:www.1b23.com
*/
@Configuration
public class SpringDaoMethodAspect {
@Bean
public DruidStatInterceptor druidStatInterceptor() {
DruidStatInterceptor dsInterceptor = new DruidStatInterceptor();
return dsInterceptor;
}
@Bean
@Scope("prototype")
public JdkRegexpMethodPointcut druidStatPointcut() {
JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
pointcut.setPattern("org.fh.mapper.*");//根据实际包名
return pointcut;
}
@Bean
public DefaultPointcutAdvisor druidStatAdvisor(DruidStatInterceptor druidStatInterceptor, JdkRegexpMethodPointcut druidStatPointcut) {
DefaultPointcutAdvisor defaultPointAdvisor = new DefaultPointcutAdvisor();
defaultPointAdvisor.setPointcut(druidStatPointcut);
defaultPointAdvisor.setAdvice(druidStatInterceptor);
return defaultPointAdvisor;
}
}
来源:oschina
链接:https://my.oschina.net/fhadmin/blog/4290517