背景
项目比较大,tomcat启动过程很长,启动速度在60+秒,甚至到100+秒
优化步骤
1、查看启动日志,发现tomcat有行日志:
"INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time."
其中TLD 是tag library descriptor的意思,我们项目由于没有用到jstl之类东西,jsp也只有很少的几个页面,大多数都是rest接口,因此这里把所有jar都禁止扫描TLD。
https://stackoverflow.com/questions/40204124/how-to-fix-jars-that-were-scanned-but-no-tlds-were-found-in-them-in-tomcat-9
根据这个方法,在catalina.properties里加上
tomcat.util.scan.StandardJarScanFilter.jarsToSkip=\
*.jar,\
如果用到了jstl,需要在tomcat.util.scan.StandardJarScanFilter.jarsToScan=\加上jstl-*.jar
注意:这个要求tomcat8+。
2、swagger初始化时间太长
org.apache.cxf.endpoint.ServerImpl.initDestination(85) - Setting the server's publish address to be
这步之后要等待很久,原因是用到了BeanConfig扫描很多类用于生成Swagger对象,除了BeanConfig外还提供了了ApplicationBeanConfig,这个类会直接使用配置的jaxrs:serviceBeans,这样就不用再次扫描。看代码需要设置Swagger2Feature的scan参数为false。
3、mybatis解析mapper.xml耗时长
把spring的日志级别改成debug,看到在SqlSessionFactoryBean的parse mapper.xml这步耗时比较长,查明原因是系统用到了多个sqlSessionFactory,配置的mapperLocation都是通配符,导致每一个sqlSessionFactory都加载、解析了所有mapper.xml,改成按需加载,速度提升。
网友评论