登录
首页 >  数据库 >  MySQL

Hikari连接池使用SpringBoot配置JMX监控

来源:SegmentFault

时间:2023-01-24 19:14:24 444浏览 收藏

小伙伴们对数据库编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《Hikari连接池使用SpringBoot配置JMX监控》,就很适合你,本篇文章讲解的知识点主要包括MySQL、Java、springboot。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!

Hikari是SpringBoot 2.0中默认的连接池。
区别于c3p0直接通过连接池对象获取各项状态指标,Hikari需要通过JMX来获取。
示例代码如下,采用SpringBoot集成,定时采集连接池连接状态。

@Component
@Controller
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableScheduling
public class HikariTest {

    private static HikariPoolMXBean poolProxy;
    private static final Logger log = LoggerFactory.getLogger(HikariTest.class);

    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("db10$ZTE");
        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 

另,在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:03.725 INFO com.zte.hikariTest.HikariTest.67 -Hikari connection pool is not already...please wait...
2019-03-09 02:05:03.726 INFO o.a.c.http11.Http11NioProtocol.173 -Starting ProtocolHandler ["http-nio-8080"]
2019-03-09 02:05:03.760 INFO o.s.b.w.e.t.TomcatWebServer.204 -Tomcat started on port(s): 8080 (http) with context path ''
2019-03-09 02:05:03.763 INFO com.zte.hikariTest.HikariTest.59 -Started HikariTest in 3.487 seconds (JVM running for 3.98)
2019-03-09 02:05:03.775 INFO c.z.hikari.HikariDataSource.110 -HikariConnectionPool - Starting...
2019-03-09 02:05:04.439 INFO c.z.hikari.HikariDataSource.123 -HikariConnectionPool - Start completed.
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监控》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于数据库的相关知识,请关注golang学习网公众号!

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>