Spring boot应用启动后首次访问很慢的解决方案
通过命令:nohup java -jar XXXX.jar & 启动项目后浏览器访问响应十分的缓慢,网页图片和css等静态资源加载的十分缓慢(网站登录更是需要好几分钟才能完全加载完毕)。
然后在Google浏览器搜索了一下(已翻墙),搜索需用英文,类似问题看来不是个例呀,甚至JDK bug列表汇中就有相似的bug,如JDK-6521844 : SecureRandom hangs on Linux Systems,但这些bug都标记为fixed。但明显没有完全fix掉啊。然后继续找,原来
Avoiding JVM Delays Caused by Random Number Generation
正好记录了这个随机数生成慢的原因和解决方案。Java随机数生成依赖熵源(Entropy Source),默认的阻塞型的 /dev/random熵源可能导致阻塞,而换一个非阻塞的 /dev/urandom的熵源就可以了。
进入你的JAVA_HOME的jre目录下找到并vim编辑这个文件:
$JAVA_HOME/jre/lib/security/java.security
找到:
securerandom.source=file:/dev/random 这一行
改之前:
securerandom.source=file:/dev/random
改为:
securerandom.source=file:/dev/urandom
然后保存修改就OK了!
Spring boot静态资源访问太慢产生的问题:spring boot 启动的服务静态资源非常慢,慢到无法忍受。
排查过程 一
1. 在filter 中记录请求时间 ,得到某些静态资源居然600ms,但是主要问题不在这里,是客户端的连接被阻塞了。如上图
2. 然后然后禁用filter(直接spring boot static) 返回
3. 结果还是很慢
排查过程 二
1. 开启客户端资源 GZIP
2. 手动设置cache-contro
结果还是很慢,我就很疑惑了,难道是选用的资源有问题,看着也很正常。
于是我就把资源都放到 python flask!! 结果比java的快了好几倍。。 瞬间我人就蒙了。
然后仔细看application.xml 配置,其实当时也没设置什么东西 ,于是一项一项的注释,效率上还是没变化,我就试了试新建一个项目,然后把 html 都拿过去。
问题解决了!! 速度 非常快
好家伙,我直接好家伙,我查了几天的问题,居然可能是在依赖上。
最后结论 :应该是某一个依赖项有问题导致的,或者版本本身不对劲有空再去看看2.3.4 的 底层tomcat配置有什么不同
有问题的配置<?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.4.RELEASE</version><relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.tianlun</groupId> <artifactId>tianlunpc</artifactId> <version>0.0.1-SNAPSHOT</version> <name>tianlunpc</name> <description>Demo project for Spring Boot</description> <properties><java.version>1.8</java.version> </properties> <dependencies><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope></dependency><dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions><exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId></exclusion> </exclusions></dependency><!-- session jdbc --><dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-jdbc</artifactId></dependency><!--热部署--><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional></dependency> </dependencies> <build><plugins> <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId> </plugin></plugins> </build></project>没问题的配置
<?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <groupId>com.tianlun</groupId> <artifactId>tianlinpc</artifactId> <version>0.0.1-SNAPSHOT</version> <name>tianlinpc</name> <description>Demo project for Spring Boot</description> <properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-boot.version>2.3.7.RELEASE</spring-boot.version> </properties> <dependencies><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version></dependency><dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-core</artifactId></dependency><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope></dependency><!--热部署--><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional></dependency><dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions><exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId></exclusion> </exclusions></dependency> </dependencies> <dependencyManagement><dependencies> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope> </dependency></dependencies> </dependencyManagement> <build><plugins> <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding></configuration> </plugin> <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.3.7.RELEASE</version><configuration> <mainClass>com.tianlun.tianlunpc.TianlinpcApplication</mainClass></configuration><executions> <execution><id>repackage</id><goals> <goal>repackage</goal></goals> </execution></executions> </plugin></plugins> </build></project>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持好吧啦网。
相关文章: