Spring boot應(yīng)用啟動后首次訪問很慢的解決方案
Spring boot應(yīng)用在ECS服務(wù)器上啟動后首次訪問很慢的問題
環(huán)境:
- CentOS7
- JDK1.8
- MYSQL8
- 應(yīng)用是Spring boot框架的(內(nèi)嵌式tomcat)jar文件
問題描述:
通過命令:nohup java -jar XXXX.jar & 啟動項(xiàng)目后瀏覽器訪問響應(yīng)十分的緩慢,網(wǎng)頁圖片和css等靜態(tài)資源加載的十分緩慢(網(wǎng)站登錄更是需要好幾分鐘才能完全加載完畢)。
然后在Google瀏覽器搜索了一下,搜索需用英文,類似問題看來不是個(gè)例呀,甚至JDK bug列表匯中就有相似的bug,如JDK-6521844 : SecureRandom hangs on Linux Systems,但這些bug都標(biāo)記為fixed。但明顯沒有完全fix掉啊。然后繼續(xù)找,原來
Avoiding JVM Delays Caused by Random Number Generation
正好記錄了這個(gè)隨機(jī)數(shù)生成慢的原因和解決方案。Java隨機(jī)數(shù)生成依賴熵源(Entropy Source),默認(rèn)的阻塞型的 /dev/random熵源可能導(dǎo)致阻塞,而換一個(gè)非阻塞的 /dev/urandom的熵源就可以了。
進(jìn)入你的JAVA_HOME的jre目錄下找到并vim編輯這個(gè)文件:
$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靜態(tài)資源訪問太慢

產(chǎn)生的問題:
spring boot 啟動的服務(wù)靜態(tài)資源非常慢,慢到無法忍受。
排查過程 一
1. 在filter 中記錄請求時(shí)間 ,得到某些靜態(tài)資源居然600ms,但是主要問題不在這里,是客戶端的連接被阻塞了。如上圖
2. 然后然后禁用filter(直接spring boot static) 返回
3. 結(jié)果還是很慢
排查過程 二
1. 開啟客戶端資源 GZIP
2. 手動設(shè)置cache-contro
結(jié)果還是很慢,我就很疑惑了,難道是選用的資源有問題,看著也很正常。
于是我就把資源都放到 python flask??! 結(jié)果比java的快了好幾倍。。 瞬間我人就蒙了。
然后仔細(xì)看application.xml 配置,其實(shí)當(dāng)時(shí)也沒設(shè)置什么東西 ,于是一項(xiàng)一項(xiàng)的注釋,效率上還是沒變化,我就試了試新建一個(gè)項(xiàng)目,然后把 html 都拿過去。
問題解決了?。?速度 非???/p>
好家伙,我直接好家伙,我查了幾天的問題,居然可能是在依賴上。
最后結(jié)論 :應(yīng)該是某一個(gè)依賴項(xiàng)有問題導(dǎo)致的,或者版本本身不對勁
有空再去看看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>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis基于TypeHandler實(shí)現(xiàn)敏感數(shù)據(jù)加密
業(yè)務(wù)場景中經(jīng)常會遇到諸如用戶手機(jī)號,身份證號,銀行卡號,郵箱,地址,密碼等等信息,屬于敏感信息,本文就來介紹一下Mybatis基于TypeHandler實(shí)現(xiàn)敏感數(shù)據(jù)加密,感興趣的可以了解一下2023-10-10
MyBatis 的一級緩存導(dǎo)致的數(shù)據(jù)一致性問題分析及解決方法
在MySQL提交讀隔離級別下,MyBatis一級緩存導(dǎo)致同一SqlSession內(nèi)重復(fù)查詢結(jié)果不更新,即使數(shù)據(jù)庫已變更,下面給大家介紹MyBatis 的一級緩存導(dǎo)致的數(shù)據(jù)一致性問題分析及解決方法,感興趣的朋友一起看看吧2025-06-06
關(guān)于JavaEE匿名內(nèi)部類和Lambda表達(dá)式的注意事項(xiàng)
這篇文章主要介紹了關(guān)于JavaEE匿名內(nèi)部類和Lambda表達(dá)式的注意事項(xiàng),匿名內(nèi)部類顧名思義是沒有修飾符甚至沒有名稱的內(nèi)部類,使用匿名內(nèi)部類需要注意哪些地方,我們一起來看看吧2023-03-03
mybatis plus表的創(chuàng)建時(shí)間和修改時(shí)間的操作方法
這篇文章主要介紹了mybatis plus表的創(chuàng)建時(shí)間和修改時(shí)間的實(shí)現(xiàn)方法,本文給大家分享兩種方法,每種方法通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09
springboot集成nacos實(shí)現(xiàn)自動刷新的示例代碼
研究nacos時(shí)發(fā)現(xiàn),springboot版本可使用@NacosValue實(shí)現(xiàn)配置的自動刷新,本文主要介紹了springboot集成nacos實(shí)現(xiàn)自動刷新的示例代碼,感興趣的可以了解一下2023-11-11

