需求

实现dbcp监控。

思想

基于cat心跳实现。

步骤

1.扩展dbcp数据源
2.基于cat心跳实现监控

代码

扩展dbcp数据源

/**
 * 扩展了dbcp数据源对象,即添加cat监控功能
 * @author gzh
 * @createTime 2020/9/18 11:44 AM
 */
public class CatSecretBasicDataSource extends SecretBasicDataSource implements CatDataSourceMonitor, InitializingBean {
  private static final org.slf4j.Logger log = LoggerFactory.getLogger(CatSecretBasicDataSource.class);

  public CatSecretBasicDataSource() {
  }

  public Logger getParentLogger() throws SQLFeatureNotSupportedException {
    return null;
  }

  /**
   * 初始化操作
   * 1.注册cat自定义监控扩展
   * 2.获取数据源对象
   * @throws Exception
   */
  public void afterPropertiesSet() throws Exception {
    log.info("dbcp-注册cat自定义监控扩展");
    //注册cat自定义监控扩展
    StatusExtensionRegister.getInstance().register(new DataSourceStatusExecutor());

    log.info("dbcp-获取数据源对象");
    //获取数据源对象
    CatDataSourceWapper.addDSMonitor(this);
  }

  public String getName() {
    return "SecretBasicDataSource";
  }
}

基于cat心跳实现监控

/**
 * cat自定义监控,原理是基于cat的StatusExtension进行扩展
 *
 * @author gzh
 * @createTime 2020/9/18 11:43 AM
 */
public class DataSourceStatusExecutor implements StatusExtension {
  private static final org.slf4j.Logger log = LoggerFactory.getLogger(DataSourceStatusExecutor.class);

  public DataSourceStatusExecutor() {
  }

  /**
   * 当前类DataSourceStatusExecutor被pluxus容器实例化之后,执行初始化
   * 注:暂时没有使用pluxus容器实例化DataSourceStatusExecutor,所以这里不会被执行,目前注册cat自定义监控扩展是在CatSecretBasicDataSource.afterPropertiesSet
   * @throws InitializationException
   */
// public void initialize() throws InitializationException {
// StatusExtensionRegister.getInstance().register(this);
// }

  public String getId() {
    return "DataSource";
  }

  public String getDescription() {
    return "DataSource Connection Pool";
  }

  /**
   * 写自定义监控数据到cat,原理是cat客户端间隔每分钟心跳执行该方法
   * @return
   */
  public Map<String, String> getProperties() {
    log.info("dbcp-心跳开始");
    Map<String, String> maps = new HashMap();
    Set<xxx.trade.monitor.dbcp.CatDataSourceMonitor> set = CatDataSourceWapper.getDSMonitors();
    String prefix = "DB";
    int idx = 0;
    Iterator var5 = set.iterator();

    while(var5.hasNext()) {
      xxx.trade.monitor.dbcp.CatDataSourceMonitor cdsm = (CatDataSourceMonitor)var5.next();
      ++idx;
      maps.put(prefix + idx + "-NumActive", String.valueOf(cdsm.getNumActive()));
      maps.put(prefix + idx + "-NumIdle", String.valueOf(cdsm.getNumIdle()));
      maps.put(prefix + idx + "-MaxActive", String.valueOf(cdsm.getMaxActive()));
      log.info("dbcp-maps:{}",JSONObject.toJSONString(maps));
    }

    return maps;
  }
}
/**
 * @author gzh
 * @createTime 2020/9/18 11:43 AM
 */
import java.util.HashSet;
import java.util.Set;

/**
 * 封装了数据源对象,即包含了数据源对象字段,主要是用来保存数据源对象
 */
public class CatDataSourceWapper {
  private static Set<xxx.trade.monitor.dbcp.CatDataSourceMonitor> dsMonitor = new HashSet();

  public CatDataSourceWapper() {
  }

  public static void addDSMonitor(xxx.trade.monitor.dbcp.CatDataSourceMonitor cdsm) {
    Class var1 = xxx.trade.monitor.dbcp.CatDataSourceWapper.class;
    synchronized(xxx.trade.monitor.dbcp.CatDataSourceWapper.class) {
      dsMonitor.add(cdsm);
    }
  }

  public static void removeDSMonitor(xxx.trade.monitor.dbcp.CatDataSourceMonitor cdsm) {
    Class var1 = xxx.trade.monitor.dbcp.CatDataSourceWapper.class;
    synchronized(xxx.trade.monitor.dbcp.CatDataSourceWapper.class) {
      dsMonitor.remove(cdsm);
    }
  }

  public static Set<CatDataSourceMonitor> getDSMonitors() {
    return dsMonitor;
  }
}
/**
 * 读数据库连接池监控数据的接口
 */
public interface CatDataSourceMonitor {
  int getNumIdle();

  int getMaxActive();

  int getNumActive();

  String getName();

}

监控结果

cat心跳菜单


Java个体户
17 声望4 粉丝

Java个体户


引用和评论

0 条评论