zoukankan      html  css  js  c++  java
  • Druid监控Admin使用记录

    druid监控配置

    StatViewServlet

    使用过druid-spring-boot-starter的同学应该知道只需要通过这个配置就可以开启监控页面:spring.datasource.druid.stat-view-servlet.enabled

    原理是通过Spring的ServletRegistrationBean注册了一个新的Servlet Bean:StatViewServlet。具体代码:

    @ConditionalOnWebApplication
    @ConditionalOnProperty(name = "spring.datasource.druid.stat-view-servlet.enabled", havingValue = "true")
    public class DruidStatViewServletConfiguration {
        private static final String DEFAULT_ALLOW_IP = "127.0.0.1";
    
        @Bean
        public ServletRegistrationBean statViewServletRegistrationBean(DruidStatProperties properties) {
            DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
            ServletRegistrationBean registrationBean = new ServletRegistrationBean();
            registrationBean.setServlet(new StatViewServlet());
            registrationBean.addUrlMappings(config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*");
            if (config.getAllow() != null) {
                registrationBean.addInitParameter("allow", config.getAllow());
            } else {
                registrationBean.addInitParameter("allow", DEFAULT_ALLOW_IP);
            }
            if (config.getDeny() != null) {
                registrationBean.addInitParameter("deny", config.getDeny());
            }
            if (config.getLoginUsername() != null) {
                registrationBean.addInitParameter("loginUsername", config.getLoginUsername());
            }
            if (config.getLoginPassword() != null) {
                registrationBean.addInitParameter("loginPassword", config.getLoginPassword());
            }
            if (config.getResetEnable() != null) {
                registrationBean.addInitParameter("resetEnable", config.getResetEnable());
            }
            return registrationBean;
        }
    }
    

    当然,这种直接可以访问的页面自然默认是不会开启的,正常情况,我们也不会配置开启监控,一般在排查问题的时候才会有开启需求,所以自然会想到应该需要在应用不重启的情况下能够开启。

    我们可以理解原理的基础上结合ServletContextInitializerStatViewServlet动态注册到容器中,从而达到动态开启的效果,以下是示例代码:

    @Component
    public class DynamicDruidAdminRefresh implements ServletContextInitializer {
    
        private ServletContext servletContext;
    
        public void openDruidAdmin() throws ServletException {
            StatViewServlet statViewServlet = servletContext.createServlet(StatViewServlet.class);
            ServletRegistration.Dynamic dynamic = servletContext.addServlet("statViewServlet", statViewServlet);
            dynamic.addMapping("/druid/*");
        }
    
        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
            this.servletContext = servletContext;
        }
    
    }
    

    WebStatFilter

    WebStatFilter 继承 javax.servlet.Filter,是属于Servlet Web 技术栈下的,并不是Druid里的Filter。目前大多数java web server都是基于Servlet标准封装的。

    这是 druid github上对WebStatFilter的解释:

    WebStatFilter用于采集web-jdbc关联监控的数据。

    意思是WebStatFilter 用来记录从请求到DB的链路上的数据。开启配置:

    spring.datasource.druid.web-stat-filter.enabled=true
    spring.datasource.druid.web-stat-filter.url-pattern=/*
    

    很显然,这种监控是会对业务请求增加性能损耗的,网上会在开启stat-view-servlet的时候,同时给出开启web-stat-filter的配置,容易在没有了解情况的时候使用到web-stat-filter。

    综上,web-stat-filter的最好也是可以动态开启和关闭的,就可以在实际应用中良好的使用了。

    监控配置例子

    ##### 连接池配置 #######
    # 过滤器设置(第一个stat很重要,没有的话会监控不到SQL)
    spring.datasource.druid.filters=stat,wall,log4j2
    
    ##### WebStatFilter配置 #######
    #启用WebStatFilter
    spring.datasource.druid.web-stat-filter.enabled=true
    #添加过滤规则
    spring.datasource.druid.web-stat-filter.url-pattern=/*
    #排除一些不必要的url
    spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
    #开启session统计功能
    spring.datasource.druid.web-stat-filter.session-stat-enable=true
    #缺省sessionStatMaxCount是1000个
    spring.datasource.druid.web-stat-filter.session-stat-max-count=1000
    #spring.datasource.druid.web-stat-filter.principal-session-name=
    #spring.datasource.druid.web-stat-filter.principal-cookie-name=
    #spring.datasource.druid.web-stat-filter.profile-enable=
    
    ##### StatViewServlet配置 #######
    #启用内置的监控页面
    spring.datasource.druid.stat-view-servlet.enabled=true
    #内置监控页面的地址
    spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
    #关闭 Reset All 功能
    spring.datasource.druid.stat-view-servlet.reset-enable=false
    #设置登录用户名
    spring.datasource.druid.stat-view-servlet.login-username=admin
    #设置登录密码
    spring.datasource.druid.stat-view-servlet.login-password=123
    #白名单(如果allow没有配置或者为空,则允许所有访问)
    spring.datasource.druid.stat-view-servlet.allow=127.0.0.1
    #黑名单(deny优先于allow,如果在deny列表中,就算在allow列表中,也会被拒绝)
    spring.datasource.druid.stat-view-servlet.deny=
    
  • 相关阅读:
    浅析全球电信运营商排名
    《时空骇客》中的远距传物理论和虫洞理论
    优秀的商业计划书一定会“动”
    手机搜索的商业模式
    手机网游排行榜
    手机按键对应表
    "Avatar模式"透析
    百度数据暗示无线互联网将以个人为中心
    一种精神致加西亚的信
    手机定位技术将成社交网络催化剂
  • 原文地址:https://www.cnblogs.com/killbug/p/14928624.html
Copyright © 2011-2022 走看看