您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“Hikari連接池使用SpringBoot配置JMX監(jiān)控的方法是什么”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Hikari連接池使用SpringBoot配置JMX監(jiān)控的方法是什么”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。
Hikari是Spring Boot默認(rèn)的數(shù)據(jù)庫連接池。區(qū)別于C3P0直接通過連接池對象獲取各項(xiàng)狀態(tài)指標(biāo),Hikari需要通過JMX來獲取。Demo如下,采用Spring Boot集成,定時(shí)采集連接狀態(tài)。
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() + ")");
可能會拋錯(cuò)
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
這是因?yàn)镠ikari設(shè)置參數(shù)同樣支持setHikariConfig和配置文件兩種配置方式,請選擇其中一種進(jìn)行配置,而不是二者一起使用。并且請配置屬性如下,否則 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監(jiān)控的方法是什么”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。