学习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();
网友评论