springboot项目启动慢的问题排查方式

作者:JohnJary 时间:2023-06-19 18:58:40 

springboot项目启动慢的问题排查

springboot项目,随着时间的推移,启动耗时逐步增加,从几分钟慢慢的达到30多分钟,有点恐怖!

项目中用到技术:hibernate、redis、kafka、线程池等,启动慢的环境使用的是mysql数据库!

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

后停滞,等相当长时间后继续输出:

o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'taskExecutor'

springboot项目启动慢的问题排查方式

怀疑是创建kafka、线程池等导致耗时,通过去掉相关对象创建,耗时仍无改观!

2. 启动项目,打印日志级别改为debug,查看更详细信息

springboot项目启动慢的问题排查方式

springboot项目启动慢的问题排查方式

发现:大量的alter table 增加外键!奇怪,不是第一次启动项目,mysql库中的表早已创建好,为什么每次都要重复alter?

查看mysql相关表发现无外键,表引擎为MyISAM!此引擎不支持外键!

在使用hibernate自动创建表时,mysql中建表使用的MyISAM引擎,查看配置:

springboot项目启动慢的问题排查方式

  • dialect:应使用org.hibernate.dialect.MySQL5InnoDBDialect

这就解释了:为什么orcle环境下没有此问题,而mysql就有,其随着时间的推移,表中数据逐渐增加,在启动时alter table耗时严重!

如何优化SpringBoot的项目的启动速度

日常开发SpringBoot项目启动类都用@SpringBootApplication

实际上它是下面三个注解的组合

  • @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加载的内容太多有关,一种方法是不用这两个注解,通过@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项目启动很慢,可能意味着你要重新拆分微服务。

来源:https://blog.csdn.net/Jerry_John/article/details/106354154

标签:springboot,启动慢,排查
0
投稿

猜你喜欢

  • Android自定义ListView实现下拉刷新上拉加载更多

    2021-08-20 21:17:04
  • Java构造方法和方法重载详解

    2021-06-06 13:23:00
  • SpringBoot + SpringSecurity 短信验证码登录功能实现

    2022-10-16 10:26:25
  • C#实现单位换算器

    2022-04-20 07:25:55
  • Android的OkHttp包处理用户认证的代码实例分享

    2022-04-25 00:00:52
  • Android开发之imageView图片按比例缩放的实现方法

    2023-01-09 21:52:51
  • Java下载远程服务器文件到本地(基于http协议和ssh2协议)

    2022-08-29 12:23:18
  • JDK14性能管理工具之jstack使用介绍

    2022-10-11 18:48:01
  • 深入浅析java中finally的用法

    2023-06-14 00:03:35
  • idea mybatis配置log4j打印sql语句的示例

    2023-11-25 10:32:39
  • Android实现签名涂鸦手写板

    2022-07-27 04:10:14
  • C#手工双缓冲技术用法实例分析

    2023-05-09 07:13:44
  • java实现动态 代理方法浅析

    2023-11-28 23:33:59
  • Java分布式学习之Kafka消息队列

    2022-04-10 22:22:47
  • spring security动态配置url权限的2种实现方法

    2021-06-25 15:31:12
  • mybatis批量添加,批量更新之前如何判断是否已经存在

    2022-01-03 20:33:38
  • java设计模式学习之装饰模式

    2021-10-02 04:22:54
  • Java基础教程之数组的定义与使用

    2022-04-24 10:24:12
  • Java守护线程实例详解_动力节点Java学院整理

    2023-03-29 08:14:30
  • Android使用WebView播放flash的方法

    2021-08-10 04:46:51
  • asp之家 软件编程 m.aspxhome.com