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=
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章