最近新人在开发项目过程中改造了很多代码,启动过程中发现非常慢,于是我进去帮忙排查,简单记录下;

项目启动我开启了debug日志,发现主要耗时卡在jooq上面;

2023-06-16 19:43:31,346 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Autowiring by type from bean name 'dslContext' via factory method to bean named 'jooqConfiguration' 
2023-06-16 19:43:31,957 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'dslContext' to allow for resolving potential circular references 
2023-06-16 19:43:32,182 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor' 
2023-06-16 19:43:32,183 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor' 
2023-06-16 19:51:45,172 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'dslContext' 
2023-06-16 19:51:45,173 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'jooqConfiguration' 
2023-06-16 19:51:45,173 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'spring.jooq-org.springframework.boot.autoconfigure.jooq.JooqProperties' 
2023-06-16 19:51:45,173 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration' 
2023-06-16 19:51:45,173 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'dataSourceConnectionProvider' 
2023-06-16 19:51:45,173 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'transactionProvider' 

发现完成dslcontext用了足足8分钟

2023-06-16 19:43:32,183 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor' 
2023-06-16 19:51:45,172 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'dslContext' 

深入排查一下dslContext为啥这么慢,升入源码通过条件断点看了下,发现主要耗时在
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsAfterInitialization

image.png

image.png

目前来看AnnotationAwareAspectJAutoProxyCreator的处理器,该处理器会扫描dslContext中的方法,并逐一判断当前的方式是否需要动态代理。dslContext中包含约几百上千的方法启动自然会很慢;

项目中之前就存在jooq,为啥突然扫描了;

diff比对改造的代码发现新增了@Aspect切面代码,而且扫码全工程路径,导致很慢

@Pointcut("execution( * com.alibaba.xxx..*(..))")
   public void getMethods() {
  
}

切面匹配是一个耗时的工作,最好减少范围,匹配的表达式越精确越好;


例子不难,思路很明确,最近在眼睛chatgpt插件结合RPA看下这类场景能不能自动化,提高效率,还在摸索ing,后续有机会分享下;


edagarli
4.3k 声望91 粉丝

github: