Spring boot应用启动后首次访问很慢的解决方案

网友投稿 961 2022-10-17


Spring boot应用启动后首次访问很慢的解决方案

Spring boot应用在ECS服务器上启动后首次访问很慢的问题

环境:

CentOS7

JDK1.8

mysql8

应用是Spring boot框架的(内嵌式tomcat)jar文件

问题描述:

通过命令: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配置有什么不同

有问题的配置

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.3.4.RELEASE

com.tianlun

tianlunpc

0.0.1-SNAPSHOT

tianlunpc

Demo project for Spring Boot

1vFsjAn.8

org.springframework.boot

spring-boot-starter-data-jpa

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-starter-web

mysql

mysql-connector-java

runtime

org.projectlombok

true

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.springframework.session

spring-session-jdbc

org.springframework.boot

spring-boot-devtools

true

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.3.4.RELEASE

com.tianlun

tianlunpc

0.0.1-SNAPSHOT

tianlunpc

Demo project for Spring Boot

1vFsjAn.8

org.springframework.boot

spring-boot-starter-data-jpa

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-starter-web

mysql

mysql-connector-java

runtime

org.projectlombok

true

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.springframework.session

spring-session-jdbc

org.springframework.boot

spring-boot-devtools

true

org.springframework.boot

spring-boot-maven-plugin

没问题的配置

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.tianlun

tianlinpc

0.0.1-SNAPSHOT

tianlinpc

Demo project for Spring Boot

1.8

UTF-8

UTF-8

2.3.7.RELEASE

org.springframework.boot

spring-boot-starter-data-jpa

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-starter-web

commons-io

commons-io

2.6

org.springframework.session

spring-session-core

mysql

mysql-connector-java

runtime

org.springframework.boot

spring-boot-devtools

true

org.projectlombok

lombok

true

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.springframework.boot

spring-boot-dependencies

${spring-boot.version}

pom

import

org.apache.maven.plugins

maven-compiler-plugin

3.8.1

1.8

1.8

UTF-8

org.springframework.boot

spring-boot-maven-plugin

2.3.7.RELEASE

com.tianlun.tianlunpc.TianlinpcApplication

repackage

repackage

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.tianlun

tianlinpc

0.0.1-SNAPSHOT

tianlinpc

Demo project for Spring Boot

1.8

UTF-8

UTF-8

2.3.7.RELEASE

org.springframework.boot

spring-boot-starter-data-jpa

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-starter-web

commons-io

commons-io

2.6

org.springframework.session

spring-session-core

mysql

mysql-connector-java

runtime

org.springframework.boot

spring-boot-devtools

true

org.projectlombok

lombok

true

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.springframework.boot

spring-boot-dependencies

${spring-boot.version}

pom

import

org.apache.maven.plugins

maven-compiler-plugin

3.8.1

1.8

1.8

UTF-8

org.springframework.boot

spring-boot-maven-plugin

2.3.7.RELEASE

com.tianlun.tianlunpc.TianlinpcApplication

repackage

repackage


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:企业用户混合云组网(SD-WAN)的纪实观察
下一篇:如何管理海量的网络基础设施?
相关文章

 发表评论

暂时没有评论,来抢沙发吧~