溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Spring boot應(yīng)用啟動(dòng)后首次訪問很慢怎么解決

發(fā)布時(shí)間:2021-06-23 13:55:10 來源:億速云 閱讀:614 作者:chen 欄目:開發(fā)技術(shù)

這篇文章主要介紹“Spring boot應(yīng)用啟動(dòng)后首次訪問很慢怎么解決”,在日常操作中,相信很多人在Spring boot應(yīng)用啟動(dòng)后首次訪問很慢怎么解決問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Spring boot應(yīng)用啟動(dòng)后首次訪問很慢怎么解決”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

Spring boot應(yīng)用在ECS服務(wù)器上啟動(dòng)后首次訪問很慢的問題

環(huán)境:

  • CentOS7

  • JDK1.8

  • MYSQL8

  • 應(yīng)用是Spring boot框架的(內(nèi)嵌式tomcat)jar文件

問題描述:

通過命令:nohup java -jar XXXX.jar & 啟動(dòng)項(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)資源訪問太慢

Spring boot應(yīng)用啟動(dòng)后首次訪問很慢怎么解決

產(chǎn)生的問題:

spring boot 啟動(dòng)的服務(wù)靜態(tài)資源非常慢,慢到無法忍受。

排查過程 一

1. 在filter 中記錄請(qǐng)求時(shí)間 ,得到某些靜態(tài)資源居然600ms,但是主要問題不在這里,是客戶端的連接被阻塞了。如上圖

2. 然后然后禁用filter(直接spring boot static) 返回

3. 結(jié)果還是很慢

排查過程 二

1. 開啟客戶端資源 GZIP

2. 手動(dòng)設(shè)置cache-contro

結(jié)果還是很慢,我就很疑惑了,難道是選用的資源有問題,看著也很正常。

于是我就把資源都放到 python flask??! 結(jié)果比java的快了好幾倍。。 瞬間我人就蒙了。

然后仔細(xì)看application.xml 配置,其實(shí)當(dāng)時(shí)也沒設(shè)置什么東西 ,于是一項(xiàng)一項(xiàng)的注釋,效率上還是沒變化,我就試了試新建一個(gè)項(xiàng)目,然后把 html 都拿過去。

問題解決了!! 速度 非常快

好家伙,我直接好家伙,我查了幾天的問題,居然可能是在依賴上。

最后結(jié)論 :應(yīng)該是某一個(gè)依賴項(xiàng)有問題導(dǎo)致的,或者版本本身不對(duì)勁

有空再去看看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>

到此,關(guān)于“Spring boot應(yīng)用啟動(dòng)后首次訪問很慢怎么解決”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI