您好,登录后才能下订单哦!
本篇内容主要讲解“如何实现Hikari连接池使用SpringBoot配置JMX监控”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何实现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("com.mysql.cj.jdbc.Driver"); hikaridatasource.setRegisterMbeans(true); hikaridatasource.setPoolName("HikariConnectionPool"); MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); ObjectName poolName = new ObjectName("com.zaxxer.hikari:type=Pool (" + hikaridatasource.getPoolName() + ")"); poolProxy = JMX.newMXBeanProxy(mBeanServer, poolName, HikariPoolMXBean.class); Connection conn = hikaridatasource.getConnection(); Statement sm = conn.createStatement(); 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,please wait..."); }else { log.info("HikariPoolState = " + "Active=[" + String.valueOf(poolProxy.getActiveConnections() + "] " + "Idle=[" + String.valueOf(poolProxy.getIdleConnections() + "] " + "Wait=["+poolProxy.getThreadsAwaitingConnection()+"] " + "Total=["+poolProxy.getTotalConnections()+"]"))); } }
另外,在github有提到这样的issue:
ObjectName poolName = new ObjectName("com.zaxxer.hikari:type=Pool (" + hikaridatasource.getPoolName() + ")");
可能会抛错
22:06:23.231 [main] DEBUG com.zaxxer.hikari.HikariConfig - Driver class com.mysql.cj.jdbc.Driver found in Thread context class loader sun.misc.Launcher$AppClassLoader@73d16e93
Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
at com.sun.proxy.$Proxy2.getIdleConnections(Unknown Source)
at com.zte.hikariTest.HikariTest.main(HikariTest.java:32)
Caused by: javax.management.InstanceNotFoundException: com.zaxxer.hikari:type=Pool (foo)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(Unknown Source)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getAttribute(Unknown Source)
at com.sun.jmx.mbeanserver.JmxMBeanServer.getAttribute(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 INFO com.zte.hikariTest.HikariTest.69 -HikariPoolState = Active=[1] Idle=[9] Wait=[0] Total=[10]
2019-03-09 02:05:05.740 INFO com.zte.hikariTest.HikariTest.69 -HikariPoolState = Active=[1] Idle=[9] Wait=[0] Total=[10]
2019-03-09 02:05:06.732 INFO com.zte.hikariTest.HikariTest.69 -HikariPoolState = Active=[1] Idle=[9] Wait=[0] Total=[10]
2019-03-09 02:05:07.738 INFO com.zte.hikariTest.HikariTest.69 -HikariPoolState = Active=[1] Idle=[9] Wait=[0] Total=[10]
到此,相信大家对“如何实现Hikari连接池使用SpringBoot配置JMX监控”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。