spring boot druid动态多数据源监控集成
Posted 太白的技术博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot druid动态多数据源监控集成相关的知识,希望对你有一定的参考价值。
当经常有发生连接池满获取不到连接时,可以开启druid监控,在连接都正常关闭的应用中,不应该出现下列警告,如下:
10:53:39.927 [Druid-ConnectionPool-Destroy--877824563] ERROR com.alibaba.druid.pool.DruidDataSource - abandon connection, owner thread: http-nio-8066-exec-8, connected at : 1579574585225, open stackTrace at java.lang.Thread.getStackTrace(Thread.java:1161) at com.alibaba.druid.pool.DruidDataSource.getConnectionDirect(DruidDataSource.java:1450) at com.alibaba.druid.filter.FilterChainImpl.dataSource_connect(FilterChainImpl.java:5059) at com.alibaba.druid.filter.FilterAdapter.dataSource_getConnection(FilterAdapter.java:2756) at com.alibaba.druid.filter.FilterChainImpl.dataSource_connect(FilterChainImpl.java:5055) at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1359) at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1351) at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:78) at org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource.getConnection(AbstractRoutingDataSource.java:164) at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111) at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77) at org.mybatis.spring.transaction.SpringManagedTransaction.openConnection(SpringManagedTransaction.java:86) at org.mybatis.spring.transaction.SpringManagedTransaction.getConnection(SpringManagedTransaction.java:72) at org.apache.ibatis.session.defaults.DefaultSqlSession.getConnection(DefaultSqlSession.java:298) at com.yidoo.xxx.datasource.DBContextHolder.setTargetDataSource(DBContextHolder.java:74) at com.yidoo.xxx.datasource.DBContextHolder.setTargetDataSource(DBContextHolder.java:26) at com.yidoo.xxx.datasource.DataSourceAspect.before(DataSourceAspect.java:47) at sun.reflect.GeneratedMethodAccessor309.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:627) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:609) at org.springframework.aop.aspectj.AspectJMethodBeforeAdvice.before(AspectJMethodBeforeAdvice.java:43) at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:55) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:47) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:671) at com.yidoo.xxx.base.service.ErrorLogServiceImpl$$EnhancerBySpringCGLIB$$68c9d6f3.addSysErrorLog(<generated>) at com.yidoo.xxx.base.TaException.saveErrorLog(TaException.java:91) at com.yidoo.xxx.base.TaException.<init>(TaException.java:52) at com.yidoo.xxx.base.BaseConfig.getConfig(BaseConfig.java:113) at com.yidoo.xxx.base.BaseConfig.getConfig(BaseConfig.java:118) at com.yidoo.xxx.filter.xss.XssAndSqlHttpServletRequestWrapper.getParameter(XssAndSqlHttpServletRequestWrapper.java:59) at com.yidoo.xxx.filter.xss.XssAndSqlHttpServletRequestWrapper.<init>(XssAndSqlHttpServletRequestWrapper.java:37) at com.yidoo.xxx.filter.XssFilter.doFilter(XssFilter.java:17) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:84) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:111) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:528) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:747) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:609) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:810) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1506) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:819) ownerThread current state is WAITING, current stackTrace at sun.misc.Unsafe.park(Native Method) at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039) at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442) at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:103) at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:31) at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:819)
要查看活跃的连接堆栈,需要开启下列选项:
dyn.spring.datasources[0].removeAbandoned=true dyn.spring.datasources[0].removeAbandonedTimeout=600 dyn.spring.datasources[0].logAbandoned=true
按照druid的文档配置对应的fitler,即可同时监控多数据源,包括在sharding jdbc中的数据源。可参考下列:
https://blog.csdn.net/garyond/article/details/80189939
https://blog.csdn.net/qq_38455201/article/details/80769354
https://www.cnblogs.com/qdhxhz/p/10192041.html
这样在发生连接获取不到时就能够找到哪些逻辑没有正常关闭连接。
如今,hikaricp很火,也是spring boot 2.0默认的内置数据源。不过它没有自带的监控体系,通过JMX暴露,通用的做法是采用MicroMeter+Prometheus,参见https://www.cnblogs.com/fireround/p/11756087.html。慢日志可以使用https://my.oschina.net/hutaishi/blog/3020251。
以上是关于spring boot druid动态多数据源监控集成的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot 集成 Druid 批量插入数据和效率监控配置
Spring Boot 集成 Druid 批量插入数据和效率监控配置
Spring Boot 集成 Druid 批量插入数据和效率监控配置