druid 如何将监控数据保存到数据库里?

rjee0c15  于 2022-11-02  发布在  Druid
关注(0)|答案(4)|浏览(349)

请教如何将监控数据保存到数据库里?
我看都提供了创建对应存储的SQL DDL了,应该是可以把监控数据对应的保存至数据库表中的吧。
我该如何做?
麻烦大神们给给回复!谢谢!

dly7yett

dly7yett1#

这部分功能没做完

rsl1atfo

rsl1atfo2#

好的,已了解情况,非常谢谢wenshao的回复。期待下一版本!

kzmpq1sx

kzmpq1sx3#

public void run() throws FileNotFoundException, JSONException, EmailException, IOException {

    SpringStatManager springStatManager = SpringStatManager.getInstance();

    List<Map<String, Object>> lists = springStatManager.getMethodStatData();

    String sqlMethod = "INSERT INTO springMethodStatData(controlServerIp,addtime,Class,Method,RunningCount,ConcurrentMax,ExecuteCount,"
            + "ExecuteErrorCount,ExecuteTimeMillis,JdbcCommitCount,JdbcRollbackCount,JdbcPoolConnectionOpenCount,"
            + "JdbcPoolConnectionCloseCount,JdbcResultSetOpenCount,JdbcResultSetCloseCount,JdbcExecuteCount,"
            + "JdbcExecuteErrorCount,JdbcExecuteTimeMillis,JdbcFetchRowCount,JdbcUpdateCount,LastError,LastErrorTime,"
            + "Histogram,Histogram_0_1,Histogram_1_10,Histogram_10_100,Histogram_100_1000,Histogram_1000_10000,Histogram_10000_100000,"
            + "Histogram_100000_1000000,Histogram_1000000_more) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
    String sql = "INSERT INTO springStat (controlServerIp,addtime,Class,Method,ExecuteCount,ExecuteTimeMillis,"
            + "RunningCount,ConcurrentMax,JdbcExecuteCount,JdbcExecuteTimeMillis,JdbcCommitCount,JdbcRollbackCount,"
            + "JdbcFetchRowCount,JdbcUpdateCount,Histogram,Histogram_0_1,Histogram_1_10,Histogram_10_100,Histogram_100_1000,Histogram_1000_10000,"
            + "Histogram_10000_100000,Histogram_100000_1000000,Histogram_1000000_more) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";

    for (Map<String, Object> list : lists){

        long[] histogram = (long[]) list.get("Histogram");

        int j= jdbcTemplate.update(sqlMethod, controlserver.getServerIp(),new Date(),list.get("Class"),list.get("Method"),list.get("RunningCount"),
                list.get("ConcurrentMax"),list.get("ExecuteCount"),list.get("ExecuteErrorCount"),list.get("ExecuteTimeMillis"),list.get("JdbcCommitCount"),
                list.get("JdbcRollbackCount"),list.get("JdbcPoolConnectionOpenCount"),list.get("JdbcPoolConnectionCloseCount"),list.get("JdbcResultSetOpenCount"),
                list.get("JdbcResultSetCloseCount"),list.get("JdbcExecuteCount"),list.get("JdbcExecuteErrorCount"),list.get("JdbcExecuteTimeMillis"),
                list.get("JdbcFetchRowCount"),list.get("JdbcUpdateCount"),list.get("LastError"),list.get("LastErrorTime"),
                Arrays.toString((long[]) list.get("Histogram")),histogram[0],histogram[1],histogram[2],histogram[3],histogram[4],histogram[5],histogram[6],histogram[7]);
        int i= jdbcTemplate.update(sql, controlserver.getServerIp(),new Date(),list.get("Class"),list.get("Method"),list.get("ExecuteCount"),list.get("ExecuteTimeMillis"),
                list.get("RunningCount"),list.get("ConcurrentMax"),list.get("JdbcExecuteCount"),list.get("JdbcExecuteTimeMillis"),list.get("JdbcCommitCount"),
                list.get("JdbcRollbackCount"),list.get("JdbcFetchRowCount"),list.get("JdbcUpdateCount"),Arrays.toString((long[]) list.get("Histogram")),
                histogram[0],histogram[1],histogram[2],histogram[3],histogram[4],histogram[5],histogram[6],histogram[7]);
        log.debug("update:" + i);
        log.debug("update springMethodStatData:" + j);
    }   

    springStatManager.resetStat();

}

贴个代码,这是我定时任务的方法,定时保存到mysql,希望对你有帮助。

pinkon5k

pinkon5k4#

https://github.com/alibaba/druid/wiki/定时输出统计信息到日志中
https://github.com/alibaba/druid/wiki/怎么保存Druid的监控记录
我参考这两篇实现了定时将数据库连接相关的信息输出到数据库
但是现在,想将druid监控的web访问记录也写入数据库,请问该如何实现比较合理呢?

相关问题