Hikari连接池使⽤SpringBoot配置JMX监控实现
Hikari是Spring Boot默认的数据库连接池。区别于C3P0直接通过连接池对象获取各项状态指标,Hikari需要通过JMX来获取。Demo如下,采⽤Spring Boot集成,定时采集连接状态。
public static void main(String[] args) throws SQLException, MalformedObjectNameException, InterruptedException {
SpringApplication.run(HikariTest.class, args);
HikariDataSource hikaridatasource = new HikariDataSource();
hikaridatasource.setJdbcUrl("jdbc:mysql://localhost:3306?serverTimezone=GMT");
hikaridatasource.setUsername("root");
hikaridatasource.setPassword("");
hikaridatasource.setDriverClassName("sql.cj.jdbc.Driver");
hikaridatasource.setRegisterMbeans(true);
手机mysql安装配置教程hikaridatasource.setPoolName("HikariConnectionPool");
MBeanServer mBeanServer = PlatformMBeanServer();
渐变头发ObjectName poolName = new ObjectName("com.zaxxer.hikari:type=Pool (" + PoolName() + ")");
poolProxy = wMXBeanProxy(mBeanServer, poolName, HikariPoolMXBean.class);
Connection conn = Connection();
Statement sm = ateStatement();
ResultSet rs = null;
for (int i = 0; i < 999999999; i++) {
rs = sm.executeQuery("select name from test.t1");
}
rs.close();
sm.close();
conn.close();
hikaridatasource.close();
}
@Scheduled(fixedRate = 1000)
public void HikariMonitor() {
if(poolProxy == null) {
log.info("Hikari not initialized,");
}else {
log.info("HikariPoolState = "
+ "Active=[" + String.ActiveConnections() + "] "
+ "Idle=[" + String.IdleConnections() + "] "
+ "Wait=["+ThreadsAwaitingConnection()+"] "
+ "Total=["+TotalConnections()+"]")));
}
python定义一个取值范围}
另外,在github有提到这样的issue:
ObjectName poolName = new ObjectName("com.zaxxer.hikari:type=Pool (" + PoolName() + ")");
可能会抛错
22:06:23.231 [main] DEBUG com.zaxxer.hikari.HikariConfig - Driver sql.cj.jdbc.Driver found in
Thread context class loader sun.misc.Launcher$AppClassLoader@73d16e93
Exception in thread "main" flect.UndeclaredThrowableException
at com.sun.proxy.$IdleConnections(Unknown Source)
hikariTest.HikariTest.main(HikariTest.java:32)
Caused by: javax.management.InstanceNotFoundException: com.zaxxer.hikari:type=Pool (foo)
at com.sun.jmx.MBean(Unknown Source)
at com.sun.jmx.Attribute(Unknown Source)
rain音译at com.sun.jmx.Attribute(Unknown Source)
at com.sun.jmx.mbeanserver.MXBeanProxy$GetHandler.invoke(Unknown Source)
at com.sun.jmx.mbeanserver.MXBeanProxy.invoke(Unknown Source)
at javax.management.MBeanServerInvocationHandler.invoke(Unknown Source)
... 2 more
这是因为Hikari设置参数同样⽀持setHikariConfig和配置⽂件两种配置⽅式,请选择其中⼀种进⾏配置,⽽不是⼆者⼀起使⽤。并且请配置属性如下,否则 JMX ⽆法⽣效。
hikaridatasource.setRegisterMbeans(true);
代码效果如下所⽰
2019-03-09 02:05:04.738 hikariTest.HikariTest.69 -HikariPoolState = Active=[1] Idle=[9] Wait=[0]
Total=[10]
熟练掌握htmlcss2019-03-09 02:05:05.740 hikariTest.HikariTest.69 -HikariPoolState = Active=[1] Idle=[9] Wait=[0]
Total=[10]
2019-03-09 02:05:06.732 hikariTest.HikariTest.69 -HikariPoolState = Active=[1] Idle=[9] Wait=[0]
Total=[10]
2019-03-09 02:05:07.738 hikariTest.HikariTest.69 -HikariPoolState = Active=[1] Idle=[9] Wait=[0]ui设计师的薪资待遇
Total=[10]
到此这篇关于Hikari连接池使⽤SpringBoot配置JMX监控的⽂章就介绍到这了,更多相关SpringBoot配置JMX监控内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论