Spring boot應(yīng)用啟動(dòng)后首次訪問(wèn)很慢的解決方案
通過(guò)命令:nohup java -jar XXXX.jar & 啟動(dòng)項(xiàng)目后瀏覽器訪問(wèn)響應(yīng)十分的緩慢,網(wǎng)頁(yè)圖片和css等靜態(tài)資源加載的十分緩慢(網(wǎng)站登錄更是需要好幾分鐘才能完全加載完畢)。
然后在Google瀏覽器搜索了一下(已翻墻),搜索需用英文,類似問(wèn)題看來(lái)不是個(gè)例呀,甚至JDK bug列表匯中就有相似的bug,如JDK-6521844 : SecureRandom hangs on Linux Systems,但這些bug都標(biāo)記為fixed。但明顯沒(méi)有完全fix掉啊。然后繼續(xù)找,原來(lái)
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)資源訪問(wèn)太慢spring boot 啟動(dòng)的服務(wù)靜態(tài)資源非常慢,慢到無(wú)法忍受。
排查過(guò)程 一
1. 在filter 中記錄請(qǐng)求時(shí)間 ,得到某些靜態(tài)資源居然600ms,但是主要問(wèn)題不在這里,是客戶端的連接被阻塞了。如上圖
2. 然后然后禁用filter(直接spring boot static) 返回
3. 結(jié)果還是很慢
排查過(guò)程 二
1. 開(kāi)啟客戶端資源 GZIP
2. 手動(dòng)設(shè)置cache-contro
結(jié)果還是很慢,我就很疑惑了,難道是選用的資源有問(wèn)題,看著也很正常。
于是我就把資源都放到 python flask!! 結(jié)果比java的快了好幾倍。。 瞬間我人就蒙了。
然后仔細(xì)看application.xml 配置,其實(shí)當(dāng)時(shí)也沒(méi)設(shè)置什么東西 ,于是一項(xiàng)一項(xiàng)的注釋,效率上還是沒(méi)變化,我就試了試新建一個(gè)項(xiàng)目,然后把 html 都拿過(guò)去。
問(wèn)題解決了!! 速度 非常快
好家伙,我直接好家伙,我查了幾天的問(wèn)題,居然可能是在依賴上。
最后結(jié)論 :應(yīng)該是某一個(gè)依賴項(xiàng)有問(wèn)題導(dǎo)致的,或者版本本身不對(duì)勁有空再去看看2.3.4 的 底層tomcat配置有什么不同
有問(wèn)題的配置<?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>沒(méi)問(wèn)題的配置
<?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è)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 解決ajax的delete、put方法接收不到參數(shù)的問(wèn)題方法2. 存儲(chǔ)于xml中需要的HTML轉(zhuǎn)義代碼3. XML解析錯(cuò)誤:未組織好 的解決辦法4. .Net Core和RabbitMQ限制循環(huán)消費(fèi)的方法5. 使用FormData進(jìn)行Ajax請(qǐng)求上傳文件的實(shí)例代碼6. JavaWeb Servlet中url-pattern的使用7. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)財(cái)務(wù)記賬管理系統(tǒng)8. XML入門的常見(jiàn)問(wèn)題(二)9. 阿里前端開(kāi)發(fā)中的規(guī)范要求10. CSS hack用法案例詳解
