Skip to content

注解方式配置监控

CoolLittle edited this page Jun 1, 2018 · 6 revisions
  1. spring监控配置

@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();
    pointcut.setPatterns("com.xxx.dao.*","com.xxx.service.*");
    return pointcut;
}
@Bean
public DefaultPointcutAdvisor druidStatAdvisor(DruidStatInterceptor druidStatInterceptor, JdkRegexpMethodPointcut druidStatPointcut) {
    DefaultPointcutAdvisor defaultPointAdvisor = new DefaultPointcutAdvisor();
    defaultPointAdvisor.setPointcut(druidStatPointcut);
    defaultPointAdvisor.setAdvice(druidStatInterceptor);
    return defaultPointAdvisor;
}

}

  1. sql监控配置

@Configuration public class DruidConfig {

@Bean
public Slf4jLogFilter logFilter () {

    Slf4jLogFilter logFilter = new Slf4jLogFilter();
    logFilter.setStatementExecutableSqlLogEnable(true);
    logFilter.setStatementLogEnabled(false);
    return logFilter;
}

@Bean
public StatFilter statFilter () {

    StatFilter statFilter = new StatFilter();
    statFilter.setSlowSqlMillis(3000);
    statFilter.setLogSlowSql(true);
    statFilter.setMergeSql(true);
    return statFilter;
}

/**
 * sql防火墙过滤器配置
 * @param wallConfig
 * @return
 */
@Bean
public WallFilter wallFilter (WallConfig wallConfig) {

    WallFilter wallFilter = new WallFilter();
    wallFilter.setConfig(wallConfig);
    wallFilter.setLogViolation(true);//对被认为是攻击的SQL进行LOG.error输出
    wallFilter.setThrowException(false);//对被认为是攻击的SQL抛出SQLException
    return wallFilter;
}

/**
 * sql防火墙配置
 * @return
 */
@Bean
public WallConfig wallConfig () {

    WallConfig wallConfig = new WallConfig();
    wallConfig.setAlterTableAllow(false);
    wallConfig.setCreateTableAllow(false);
    wallConfig.setDeleteAllow(false);
    wallConfig.setMergeAllow(false);
    wallConfig.setDescribeAllow(false);
    wallConfig.setShowAllow(false);
    return wallConfig;
}

}

  1. 注意引入aspectj支持,否则会报错

org.aspectj aspectjrt xxx org.aspectj aspectjweaver xxxx
Clone this wiki locally