1. 程式人生 > 實用技巧 >idea2020 上實現 Springboot 熱部署

idea2020 上實現 Springboot 熱部署

idea2020 上實現 Springboot 熱部署

個人實驗成功

一、導包

Developer Tools 官方文件介紹[2.3.2.RELEAS]

Spring Boot includes an additional set of tools that can make the application development experience a little more pleasant. The spring-boot-devtools module can be included in any project to provide additional development-time features. To include devtools support, add the module dependency to your build, as shown in the following listings for Maven and Gradle:

機翻: Spring Boot包含一組額外的工具,可以使應用程式開發體驗更愉快一些。spring-boot-devtools 模組可以包含在任何專案中,以提供額外的開發時特性。為了包含devtools的支援,新增模組依賴到您的構建,如下面的Maven和Gradle清單所示:

Maven

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

Gradle

dependencies {
    developmentOnly("org.springframework.boot:spring-boot-devtools")
}

本人使用的maven. 有些教程還要再build裡的plugin加fork,但我沒加也可以。

二、idea 設定

第一步、勾選 Build project automatically

具體步驟:開啟頂部工具欄 File -> Settings -> 搜Compiler 然後勾選 Build project automatically

第二步、設定 Registry

具體步驟:同時按住 Ctrl + Shift + Alt + / 然後進入Registry ,勾選自動編譯並調整延時引數。
PS: 如果 Ctrl + Shift + Alt + / 沒有彈出, 可以使用 Ctrl + Shift + A 全域性搜尋 Registry

  • compiler.automake.allow.when.app.running -> 自動編譯
  • compile.document.save.trigger.delay -> 自動更新檔案

作者:yizhiwazi:網上極少有人提到compile.document.save.trigger.delay 它主要是針對靜態檔案如JS CSS的更新,將延遲時間減少後,直接按F5重新整理頁面就能看到效果!

第三步、設定Configurations

具體步驟:頂部選單 Run- >Edit Configurations->SpringBoot外掛->目標專案->勾選熱更新。

三、為什麼要熱部署?

​                  Restart vs Reload
The restart technology provided by Spring Boot works by using two classloaders. Classes that do not change (for example, those from third-party jars) are loaded into a base classloader. Classes that you are actively developing are loaded into a restart classloader. When the application is restarted, the restart classloader is thrown away and a new one is created. This approach means that application restarts are typically much faster than “cold starts”, since the base classloader is already available and populated.

If you find that restarts are not quick enough for your applications or you encounter classloading issues, you could consider reloading technologies such as JRebel from ZeroTurnaround. These work by rewriting classes as they are loaded to make them more amenable to reloading.

上面就是官方文件的原文,結合視訊,對於熱部署的優點個人理解是:

Spring Boot提供的重啟技術通過兩個類載入器來工作分別是 base classloaderrestart classloader 。 不變的內容(即引入的哪些包)是被 base classloaders 載入。而你的寫是被 restart classloader 載入。 熱部署就是隻有 restart classloader 重新載入,而冷載入(cold starts)是兩個都重新載入,所以通常更快一些。

文件中提到(機翻): 如果您發現重新啟動對您的應用程式來說不夠快,或者遇到類載入問題,您可以考慮重新載入 ZeroTurnaround 中的 JRebel 等技術。它們的工作原理是在類被載入時重寫類,使它們更易於重新載入。

JRebel 就是一個外掛,這裡貼個教程 JRebel外掛使用詳解

四、排除某些資源

Certain resources do not necessarily need to trigger a restart when they are changed. For example, Thymeleaf templates can be edited in-place. By default, changing resources in /META-INF/maven, /META-INF/resources, /resources, /static, /public, or /templates does not trigger a restart but does trigger a live reload. If you want to customize these exclusions, you can use the spring.devtools.restart.exclude property. For example, to exclude only /static and /public you would set the following property:

spring.devtools.restart.exclude=static/**,public/**

就是預設/META-INF/maven, /META-INF/resources, /resources, /static, /public, or /templates不會觸發重新啟動(restart),但是會觸發 live reload , 預設排除的都是靜態資源,模板,前端頁面相關之類的。最後就是自定義排除一個示例。

參考 來源:

SpringBoot 在IDEA中實現熱部署(實用版)
官方文件 2.3.2.RELEASE
B站視訊
b站最強新版Springboot教程 全程有廢話我直播吃紙