springboot項(xiàng)目啟動(dòng)慢的問題排查方式
springboot項(xiàng)目啟動(dòng)慢的問題排查
springboot項(xiàng)目,隨著時(shí)間的推移,啟動(dòng)耗時(shí)逐步增加,從幾分鐘慢慢的達(dá)到30多分鐘,有點(diǎn)恐怖!
項(xiàng)目中用到技術(shù):hibernate、redis、kafka、線程池等,啟動(dòng)慢的環(huán)境使用的是mysql數(shù)據(jù)庫(kù)!
1.最開始查看的啟動(dòng)日志,是在輸出:
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)長(zhǎng)時(shí)間后繼續(xù)輸出:
o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'taskExecutor'
懷疑是創(chuàng)建kafka、線程池等導(dǎo)致耗時(shí),通過去掉相關(guān)對(duì)象創(chuàng)建,耗時(shí)仍無(wú)改觀!
2. 啟動(dòng)項(xiàng)目,打印日志級(jí)別改為debug,查看更詳細(xì)信息
發(fā)現(xiàn):大量的alter table 增加外鍵!奇怪,不是第一次啟動(dòng)項(xiàng)目,mysql庫(kù)中的表早已創(chuàng)建好,為什么每次都要重復(fù)alter?
查看mysql相關(guān)表發(fā)現(xiàn)無(wú)外鍵,表引擎為MyISAM!此引擎不支持外鍵!
在使用hibernate自動(dòng)創(chuàng)建表時(shí),mysql中建表使用的MyISAM引擎,查看配置:
dialect
:應(yīng)使用org.hibernate.dialect.MySQL5InnoDBDialect
這就解釋了:為什么orcle環(huán)境下沒有此問題,而mysql就有,其隨著時(shí)間的推移,表中數(shù)據(jù)逐漸增加,在啟動(dòng)時(shí)alter table耗時(shí)嚴(yán)重!
如何優(yōu)化SpringBoot的項(xiàng)目的啟動(dòng)速度
日常開發(fā)SpringBoot項(xiàng)目啟動(dòng)類都用@SpringBootApplication
實(shí)際上它是下面三個(gè)注解的組合
@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
啟動(dòng)慢往往跟@ComponentScan和@EnableAutoConfiguration加載的內(nèi)容太多有關(guān),一種方法是不用這兩個(gè)注解,通過@import注解精確指定要加載掃描的類,但要加載的類多時(shí)又很麻煩,
可以用@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)目啟動(dòng)很慢,可能意味著你要重新拆分微服務(wù)。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot接收參數(shù)所有方式總結(jié)
這篇文章主要介紹了SpringBoot接收參數(shù)所有方式總結(jié),文中通過代碼示例和圖文結(jié)合的方式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-07-07Spring連接Mysql數(shù)據(jù)庫(kù)全過程
這篇文章主要介紹了Spring連接Mysql數(shù)據(jù)庫(kù)全過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11mybatis resultType自帶數(shù)據(jù)類型別名解讀
MyBatis為了簡(jiǎn)化開發(fā),通過org.apache.ibatis.type.TypeAliasRegistry為常見類定義了別名,這些別名包括基本數(shù)據(jù)類型及其數(shù)組、集合類型等,如string對(duì)應(yīng)java.lang.String,int對(duì)應(yīng)java.lang.Integer等,此外,還有特殊前綴的別名如_int對(duì)應(yīng)int類型2024-10-10java 設(shè)計(jì)模式(DAO)的實(shí)例詳解
這篇文章主要介紹了java 設(shè)計(jì)模式(DAO)的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09如何將Spring Session存儲(chǔ)到Redis中實(shí)現(xiàn)持久化
這篇文章主要介紹了如何將Spring Session存儲(chǔ)到Redis中實(shí)現(xiàn)持久化,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07Java微信公眾平臺(tái)開發(fā)(6) 微信開發(fā)中的token獲取
這篇文章主要為大家詳細(xì)介紹了Java微信公眾平臺(tái)開發(fā)第六步,微信開發(fā)中的token獲取,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04SpringMVC中的ConversionServiceExposingInterceptor工具類解析
這篇文章主要介紹了SpringMVC中的ConversionServiceExposingInterceptor工具類解析,ConversionServiceExposingInterceptor是Spring MVC的一個(gè)HandlerInterceptor,用于向請(qǐng)求添加一個(gè)屬性,需要的朋友可以參考下2023-12-12