码迷,mamicode.com
首页 > 其他好文 > 详细

开启Druid监控

时间:2021-01-27 13:33:25      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:str   turn   过滤   url   boot   tor   regexp   忽略   list   

1.添加配置类

当前监控了sql

@Configuration
public class DruidConfig {

    /**
     * 注册一个StatViewServlet
     *
     * @return servlet registration bean
     */
    @Bean
    public ServletRegistrationBean druidStatViewServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(
                new StatViewServlet(), "/druid/*");

        servletRegistrationBean.addInitParameter("loginUsername", "admin");
        servletRegistrationBean.addInitParameter("loginPassword", "123456");
        servletRegistrationBean.addInitParameter("resetEnable", "false");
        return servletRegistrationBean;
    }

    /**
     * 注册一个:filterRegistrationBean
     *
     * @return filter registration bean
     */
    @Bean
    public FilterRegistrationBean druidStatFilter() {

        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(
                new WebStatFilter());

        // 添加过滤规则.
        filterRegistrationBean.addUrlPatterns("/*");

        // 添加不需要忽略的格式信息.
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }

}

2.开启spring监控(即对服务层监控) 添加druid-bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 配置_Druid和Spring关联监控配置 -->
    <bean id="druid-stat-interceptor"
          class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor"/>

    <!-- 方法名正则匹配拦截配置 无效-->
    <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
        <property name="patterns">
            <list>
                <value>com.erp.sing.service.*</value>
                <value>com.erp.sing.dao.*</value>
            </list>
        </property>
    </bean>

    <aop:config proxy-target-class="true">
        <!-- 方法名正则匹配拦截配置 有效-->
        <aop:pointcut id="txPointcut" expression="execution(* com.erp.sign.service..*.*(..))" />
        <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="txPointcut" />
    </aop:config>

</beans>

3.springboot启动类上添加

@EnableAspectJAutoProxy
@ImportResource(locations = { "classpath:druid-bean.xml" })

 

开启Druid监控

标签:str   turn   过滤   url   boot   tor   regexp   忽略   list   

原文地址:https://www.cnblogs.com/hanzhengjie/p/14330911.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!