知识&技术&梦想 知识&技术&梦想

druid - 怎么保存Druid的监控记录

Druid中有DruidDataSource/Spring/Web等监控记录,其中DruidDataSource提供了保存监控记录的API。

保存DruidDataSource的监控记录

DruidDataSource有一个属性timeBetweenLogStatsMillis,配置timeBetweenLogStatsMillis>0之后,DruidDataSource会定期把监控数据输出到日志中。
  <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    ... ...
    <property name="timeBetweenLogStatsMillis" value="300000" />
    ... ...
  </bean>
或者通过jvm启动参数来指定,例如:
  -Ddruid.timeBetweenLogStatsMillis=300000

定制StatLogger

DruidDataSource是通过 com.alibaba.druid.pool.DruidDataSourceStatLoggerImpl.DruidDataSourceStatLoggerImpl 来实现输入监控数据到日志的,你可以自定义一个StatLogger,例如: Java代码
  import com.alibaba.druid.pool.DruidDataSourceStatLoggerAdapter;
  import com.alibaba.druid.pool.DruidDataSourceStatLogger ;
  public class MyStatLogger extends DruidDataSourceStatLoggerAdapter implements DruidDataSourceStatLogger {
    ... ...
  }
配置
  <bean id="myStatLogger" class="com.mycompany.MyStatLogger">
    ... ...
  </bean>
  <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    ... ...
    <property name="statLogger" ref="myStatLogger" />
    ... ...
  </bean>
大纲