美文网首页HiKariCP
Hikaricp配置prometheus http输出

Hikaricp配置prometheus http输出

作者: 七星石 | 来源:发表于2020-04-03 16:39 被阅读0次

    学习Hikaricp,发现不少metrics相关源代码,网上没有找到相关使用的代码

    以下程序实现Hikaricp连接mysql,prometheus http输出,

    可以将数据被prometheus服务器接收,

    配置grafana展现

    注释部分为console输出方式

    HikariConfig config = new HikariConfig();

    config.setMinimumIdle(10);

    config.setMaximumPoolSize(200);

    config.setConnectionTestQuery("SELECT 1");

    config.setDriverClassName("com.mysql.jdbc.Driver");

    config.setJdbcUrl("jdbc:mysql://localhost:3306/world");

    config.setUsername("root");

    config.setPassword("chen5034");

    config.setRegisterMbeans(true);

    /*metrics

    MetricRegistry mr = new MetricRegistry();

    config.setMetricRegistry(mr);

    ConsoleReporter cr = ConsoleReporter.forRegistry(mr).build();

    cr.start(5, TimeUnit.SECONDS);

    */

    HTTPServer srv = null;

    CollectorRegistry cr = new CollectorRegistry();

    PrometheusMetricsTrackerFactory pmtf = new PrometheusMetricsTrackerFactory(cr);

    config.setMetricsTrackerFactory(pmtf);

    try {

    srv = new HTTPServer(new InetSocketAddress(9026), cr);

    } catch (IOException e1) {

    // TODO Auto-generated catch block

    e1.printStackTrace();

    }

    HikariDataSource dataSource = new HikariDataSource(config);

    JdbcTemplate jt = new JdbcTemplate(dataSource);

    for (int i = 1; i < 100; i++) {

    String out = jt.queryForObject("select Name from city where id = "+i, String.class);

    System.out.println("Name : " + out + " of id=" + i);

    try {

    Thread.sleep(10*1000);

    } catch (InterruptedException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    }

    srv.stop();

    相关文章

      网友评论

        本文标题:Hikaricp配置prometheus http输出

        本文链接:https://www.haomeiwen.com/subject/obckphtx.html