springboot項(xiàng)目啟動慢的問題排查方式
springboot項(xiàng)目啟動慢的問題排查
springboot項(xiàng)目,隨著時間的推移,啟動耗時逐步增加,從幾分鐘慢慢的達(dá)到30多分鐘,有點(diǎn)恐怖!
項(xiàng)目中用到技術(shù):hibernate、redis、kafka、線程池等,啟動慢的環(huán)境使用的是mysql數(shù)據(jù)庫!
1.最開始查看的啟動日志,是在輸出:
org.hibernate.id.UUIDHexGenerator : HHH000409: Using org.hibernate.id.UUIDHexGenerator which does not generate IETF RFC 4122 compliant UUID values; consider using org.hibernate.id.UUIDGenerator instead
后停滯,等相當(dāng)長時間后繼續(xù)輸出:
o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'taskExecutor'
懷疑是創(chuàng)建kafka、線程池等導(dǎo)致耗時,通過去掉相關(guān)對象創(chuàng)建,耗時仍無改觀!
2. 啟動項(xiàng)目,打印日志級別改為debug,查看更詳細(xì)信息
發(fā)現(xiàn):大量的alter table 增加外鍵!奇怪,不是第一次啟動項(xiàng)目,mysql庫中的表早已創(chuàng)建好,為什么每次都要重復(fù)alter?
查看mysql相關(guān)表發(fā)現(xiàn)無外鍵,表引擎為MyISAM!此引擎不支持外鍵!
在使用hibernate自動創(chuàng)建表時,mysql中建表使用的MyISAM引擎,查看配置:
dialect
:應(yīng)使用org.hibernate.dialect.MySQL5InnoDBDialect
這就解釋了:為什么orcle環(huán)境下沒有此問題,而mysql就有,其隨著時間的推移,表中數(shù)據(jù)逐漸增加,在啟動時alter table耗時嚴(yán)重!
如何優(yōu)化SpringBoot的項(xiàng)目的啟動速度
日常開發(fā)SpringBoot項(xiàng)目啟動類都用@SpringBootApplication
實(shí)際上它是下面三個注解的組合
@EnableAutoConfiguration
: enable Spring Boot's auto-configuration mechanism@ComponentScan
: enable@Component
scan on the package where the application is located (see the best practices)@Configuration
: allow to register extra beans in the context or import additional configuration classes
啟動慢往往跟@ComponentScan和@EnableAutoConfiguration加載的內(nèi)容太多有關(guān),一種方法是不用這兩個注解,通過@import注解精確指定要加載掃描的類,但要加載的類多時又很麻煩,
可以用@SpringBootApplication注解下面的屬性
exclude
: Exclude the list of classes from the auto configuration.excludeNames
: Exclude the list of fully qualified class names from the auto configuration. This parameter added since spring boot 1.3.0.scanBasePackageClasses
: Provide the list of classes that has to be applied for the @ComponentScan.scanBasePackages
Provide the list of packages that has to be applied for the @ComponentScan. This parameter added since spring boot 1.3.0.
另外,如果SpringBoot項(xiàng)目啟動很慢,可能意味著你要重新拆分微服務(wù)。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot接收參數(shù)所有方式總結(jié)
這篇文章主要介紹了SpringBoot接收參數(shù)所有方式總結(jié),文中通過代碼示例和圖文結(jié)合的方式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-07-07mybatis resultType自帶數(shù)據(jù)類型別名解讀
MyBatis為了簡化開發(fā),通過org.apache.ibatis.type.TypeAliasRegistry為常見類定義了別名,這些別名包括基本數(shù)據(jù)類型及其數(shù)組、集合類型等,如string對應(yīng)java.lang.String,int對應(yīng)java.lang.Integer等,此外,還有特殊前綴的別名如_int對應(yīng)int類型2024-10-10如何將Spring Session存儲到Redis中實(shí)現(xiàn)持久化
這篇文章主要介紹了如何將Spring Session存儲到Redis中實(shí)現(xiàn)持久化,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07Java微信公眾平臺開發(fā)(6) 微信開發(fā)中的token獲取
這篇文章主要為大家詳細(xì)介紹了Java微信公眾平臺開發(fā)第六步,微信開發(fā)中的token獲取,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04SpringMVC中的ConversionServiceExposingInterceptor工具類解析
這篇文章主要介紹了SpringMVC中的ConversionServiceExposingInterceptor工具類解析,ConversionServiceExposingInterceptor是Spring MVC的一個HandlerInterceptor,用于向請求添加一個屬性,需要的朋友可以參考下2023-12-12