天天看點

ssm整合druid監控功能入門純注解

Druid内置提供一個StatFilter,用于統計監控資訊。

參考文檔:(https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_StatFilter)

1.建立DruidStatueServlet

//  比對所有/druid/* 的請求   進行處理
@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 {
}
           

開啟後即可以啟動伺服器,并且通過位址欄通路監控頁面,通路規則:

ip:端口号/應用名/druid/login.html

2.建立WebStatFilter過濾器

@WebFilter(urlPatterns = "/*",
       initParams ={
               @WebInitParam(name="exclusions",value="*.js,*.jpg,*.png,*.css,/druid/*"),//忽略字尾
               @WebInitParam(name="profileEnable",value = "true")//開啟sql詳情
       }
)
public class DruidWebFilter extends WebStatFilter {
}
           

3.配置DruidDataSource資料源

//資料源
@Bean
public DruidDataSource getDataSource() {
    Properties properties = new Properties();
    try {
        properties.load(SpringMybatisConfig.class.getClassLoader().getResourceAsStream("db.properties"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    DruidDataSource druidDataSource = new DruidDataSource();
    druidDataSource.configFromPropety(properties);
    //配置使用druid内置filter   通過别名,别名...的方式設定
    //stat:開啟監控功能過濾器
    try {
        //開啟性能監控、sql防火牆、日志
        druidDataSource.setFilters("stat,wall,log4j2");
        //開啟慢sql監控,大于1000毫秒的為慢sql,預設是3秒
        druidDataSource.setConnectionProperties("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=1000");
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return druidDataSource;
}
           

4.配置session插件

//sqlSessionFactor建立
@Bean
public SqlSessionFactoryBean getSessionFactoryBean(DruidDataSource dataSource) {
    SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
    sessionFactoryBean.setDataSource(dataSource);
   // org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
    //tkmapper 需要使用  tkmapper提供的configuration
    //預設使用駝峰命名轉換
    tk.mybatis.mapper.session.Configuration configuration = new tk.mybatis.mapper.session.Configuration();
    configuration.setLogImpl(Log4j2Impl.class);//設定日志類
    configuration.setMapUnderscoreToCamelCase(true);//設定駝峰命名轉換
    configuration.setCallSettersOnNulls(true);//解決查詢傳回map,null的資料不封裝key到map中的問題

    configuration.setObjectWrapperFactory(new MapWrapperFactory());

    sessionFactoryBean.setConfiguration(configuration);

    //設定分頁插件
    PageInterceptor pageInterceptor = new PageInterceptor();
    pageInterceptor.setProperties(new Properties());//使用預設配置
    sessionFactoryBean.setPlugins(new Interceptor[]{pageInterceptor});//設定使用插件
    return sessionFactoryBean;
}
           

5.注解配置spring監控

設定druid 的 aop切面類

@Bean(name = "druidStatInterceptor")//設定druid 的 aop切面類
public DruidStatInterceptor getDruidStatInterceptor() {
    DruidStatInterceptor druidStatInterceptor = new DruidStatInterceptor();
    return druidStatInterceptor;
}
           

配置spring監控

public BeanNameAutoProxyCreator getAutoProxyCreator() {
    BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator();
    beanNameAutoProxyCreator.setProxyTargetClass(true);
    beanNameAutoProxyCreator.setBeanNames(new String[]{"*Mapper", "*Service*"});
    beanNameAutoProxyCreator.setInterceptorNames("druidStatInterceptor");
    return beanNameAutoProxyCreator;
}
           

繼續閱讀