Druid监控踩坑指南

概述

最近项目组在准备接入各种指标监控系统,笔者负责的部分刚好涉及到了 Druid,故记录一下在过程中遇到的各种情况和坑。

1. 直接使用 Druid

直接使用 Druid 的监控功能,需要直接将它提供的 Servlet 配置到 Web 容器中。具体可以直接参照官方文档。

此外,在这个过程中大部分问题官方文档中也有解答https://github.com/alibaba/druid/wiki/常见问题

2. 使用 starter

2.1. 启用监控

如果使用 spring-boot-starter ,则可以基于 springboot 集成。
参照:SpringBoot——开启Druid监控统计功能

2.2. SQL监控无数据问题

开启配置后,虽然可用访问监控页面了,但是发现 SQL 监控依然没有数据。
参照:SpringBoot中使用 Druid 数据库连接池, 后台SQL监控无效
不过笔者在尝试过上述两种方案后,在 SQL 监控依然无法获取相应的监控数据,在查找 issues 后发现了解决方案:SQL监控无数据--DataSource注入问题导致没有数据
如果用 javaConf 或者 xml 的方式手动配置 Bean,需要指定开启的 Filters。

@Bean("druidDataSource")
public DataSource dataSource(Environment env) throws SQLException {
  DruidDataSource dataSource = new DruidDataSource();
  dataSource.setFilters("stat");
  ......
  return dataSource;
}

如果不手动指定 Bean,使用配置指定 Druid 数据,会自动开启 statFilter

spring.datasource.url=your_url 
spring.datasource.username=username 
spring.datasource.password=password
pring.datasource.driver-class-name=com.mysql.jdbc.Driver 
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

实际上,笔者的项目就是第一种情况,我们的数据源是在配置类中单独配置的,因此需要通过 dataSource.setFilters("stat") 手动开启状态采集器,其余配置保持不变,重启项目后正常。

3. 手动采集

如果不依赖 Druid 的监控页面,也可以自己获取线程池进行采集,Druid 已经提供好的响应的 API,我们只需要将相应的指标收集任务注册好即可。
这里以 Metrics 为例:

/**
 * DruidMetricsCollector
 *
 * @author huangchengxing
 */
@ConditionalOnClass({DruidDataSource.class, MeterRegistry.class})
@Component
@Slf4j
@RequiredArgsConstructor
public class DruidMetricsCollector implements ApplicationContextAware, InitializingBean {

    private static final String LABEL_NAME = "druid-data-source";
    private final MeterRegistry registry;
    @Setter
    private ApplicationContext applicationContext;

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("Metrics components: druid register finish");
        Map<String, DataSource> dataSourceMap = applicationContext.getBeansOfType(DataSource.class);
        dataSourceMap.forEach((name, ds) -> {
            DruidDataSource druidDataSource = null;
            try {
                druidDataSource = ds.unwrap(DruidDataSource.class);
            } catch (SQLException e) {
                log.warn("Failed to unwrap DruidDataSource from DataSource: {}", name, e);
            }
            if (druidDataSource != null) {
                log.info("Registering metrics for DruidDataSource: {}", name);
                registerMetrics(druidDataSource);
            }
        });
    }

    private void registerMetrics(DruidDataSource druidDataSource) {
        registerGauge(druidDataSource, "druid_initial_size", "Initial size", (datasource) -> (double) druidDataSource.getInitialSize());
        registerGauge(druidDataSource, "druid_min_idle", "Min idle", datasource -> (double) druidDataSource.getMinIdle());
        registerGauge(druidDataSource, "druid_max_active", "Max active", datasource -> (double) druidDataSource.getMaxActive());

        registerGauge(druidDataSource, "druid_active_count", "Active count", datasource -> (double) druidDataSource.getActiveCount());
        registerGauge(druidDataSource, "druid_active_peak", "Active peak", datasource -> (double) druidDataSource.getActivePeak());
        registerGauge(druidDataSource, "druid_pooling_peak", "Pooling peak", datasource -> (double) druidDataSource.getPoolingPeak());
        registerGauge(druidDataSource, "druid_pooling_count", "Pooling count", datasource -> (double) druidDataSource.getPoolingCount());
        registerGauge(druidDataSource, "druid_wait_thread_count", "Wait thread count", datasource -> (double) druidDataSource.getWaitThreadCount());

        registerGauge(druidDataSource, "druid_not_empty_wait_count", "Not empty wait count", datasource -> (double) druidDataSource.getNotEmptyWaitCount());
        registerGauge(druidDataSource, "druid_not_empty_wait_millis", "Not empty wait millis", datasource -> (double) druidDataSource.getNotEmptyWaitMillis());
        registerGauge(druidDataSource, "druid_not_empty_thread_count", "Not empty thread count", datasource -> (double) druidDataSource.getNotEmptyWaitThreadCount());

        registerGauge(druidDataSource, "druid_logic_connect_count", "Logic connect count", datasource -> (double) druidDataSource.getConnectCount());
        registerGauge(druidDataSource, "druid_logic_close_count", "Logic close count", datasource -> (double) druidDataSource.getCloseCount());
        registerGauge(druidDataSource, "druid_logic_connect_error_count", "Logic connect error count", datasource -> (double) druidDataSource.getConnectErrorCount());
        registerGauge(druidDataSource, "druid_physical_connect_count", "Physical connect count", datasource -> (double) druidDataSource.getCreateCount());
        registerGauge(druidDataSource, "druid_physical_close_count", "Physical close count", datasource -> (double) druidDataSource.getDestroyCount());
        registerGauge(druidDataSource, "druid_physical_connect_error_count", "Physical connect error count", datasource -> (double) druidDataSource.getCreateErrorCount());

        registerGauge(druidDataSource, "druid_error_count", "Error count", datasource -> (double) druidDataSource.getErrorCount());
        registerGauge(druidDataSource, "druid_execute_count", "Execute count", datasource -> (double) druidDataSource.getExecuteCount());
        registerGauge(druidDataSource, "druid_start_transaction_count", "Start transaction count", datasource -> (double) druidDataSource.getStartTransactionCount());
        registerGauge(druidDataSource, "druid_commit_count", "Commit count", datasource -> (double) druidDataSource.getCommitCount());
        registerGauge(druidDataSource, "druid_rollback_count", "Rollback count", datasource -> (double) druidDataSource.getRollbackCount());

        registerGauge(druidDataSource, "druid_prepared_statement_open_count", "Prepared statement open count", datasource -> (double) druidDataSource.getPreparedStatementCount());
        registerGauge(druidDataSource, "druid_prepared_statement_closed_count", "Prepared statement closed count", datasource -> (double) druidDataSource.getClosedPreparedStatementCount());
        registerGauge(druidDataSource, "druid_ps_cache_access_count", "PS cache access count", datasource -> (double) druidDataSource.getCachedPreparedStatementAccessCount());
        registerGauge(druidDataSource, "druid_ps_cache_hit_count", "PS cache hit count", datasource -> (double) druidDataSource.getCachedPreparedStatementHitCount());
        registerGauge(druidDataSource, "druid_ps_cache_miss_count", "PS cache miss count", datasource -> (double) druidDataSource.getCachedPreparedStatementMissCount());
        registerGauge(druidDataSource, "druid_execute_query_count", "Execute query count", datasource -> (double) druidDataSource.getExecuteQueryCount());
        registerGauge(druidDataSource, "druid_execute_update_count", "Execute update count", datasource -> (double) druidDataSource.getExecuteUpdateCount());
        registerGauge(druidDataSource, "druid_execute_batch_count", "Execute batch count", datasource -> (double) druidDataSource.getExecuteBatchCount());

        registerGauge(druidDataSource, "druid_max_wait", "Max wait", datasource -> (double) druidDataSource.getMaxWait());
        registerGauge(druidDataSource, "druid_max_wait_thread_count", "Max wait thread count", datasource -> (double) druidDataSource.getMaxWaitThreadCount());
        registerGauge(druidDataSource, "druid_login_timeout", "Login timeout", datasource -> (double) druidDataSource.getLoginTimeout());
        registerGauge(druidDataSource, "druid_query_timeout", "Query timeout", datasource -> (double) druidDataSource.getQueryTimeout());
        registerGauge(druidDataSource, "druid_transaction_query_timeout", "Transaction query timeout", datasource -> (double) druidDataSource.getTransactionQueryTimeout());
    }

    private void registerGauge(DruidDataSource weakRef, String metric, String help, ToDoubleFunction<DruidDataSource> measure) {
        Gauge.builder(metric, weakRef, measure)
            .description(help)
            .tag(LABEL_NAME, weakRef.getName())
            .register(this.registry);
    }
}

项目启动后,确保该组件被 Spring 管理即可。

热门相关:九星之主   来自异世界的诺诺   大金主,你别假正经了   傲天弃少   闪婚总裁很惧内