Springboot熱部署,使用spring-boot-devtools、springloaded兩種方式進行熱部署


1、在項目的開發階段,經常需要對代碼進行反復修改,這樣就會導致SpringBoot運行容器反復啟動。為了解決這種頻繁重啟問題,SpringBoot提供了自動加載配置的依賴庫,以實現代碼的動態加載。

  在自己要經常修改的項目模塊寫上下面的依賴配置,保存,刷新update項目,然后修改自己的項目,可以看到后台就已經重新部署了,DevTools 在部署項目時使用的是重新部署的方式。

1 <!-- DevTools 的坐標,熱部署 -->
2 <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
3 <dependency>
4     <groupId>org.springframework.boot</groupId>
5     <artifactId>spring-boot-devtools</artifactId>
6     <version>2.2.10.RELEASE</version>
7 </dependency>

  項目中配置了以上坐標之后,每當用戶修改項目中程序類的時候都會由SpringBoot自動加載更新后的程序代碼,同時也可以在項目名稱上看到這樣得標記了“[devtools]”。

 

2、除了使用devtools的方式,還可以使用下面的springloaded的方式進行熱部署的,SpringLoader 在部署項目時使用的是熱部署的方式。

1 <!-- https://mvnrepository.com/artifact/org.springframework/springloaded -->
2 <dependency>
3     <groupId>org.springframework</groupId>
4     <artifactId>springloaded</artifactId>
5     <version>1.2.8.RELEASE</version>
6 </dependency>

需要注意得是這里兩個坐標依賴得方式,我說直接放到dependencies標簽里面得哦,如下所示:

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4     <modelVersion>4.0.0</modelVersion>
  5     <parent>
  6         <groupId>org.springframework.boot</groupId>
  7         <artifactId>spring-boot-starter-parent</artifactId>
  8         <version>2.2.6.RELEASE</version>
  9         <relativePath/> <!-- lookup parent from repository -->
 10     </parent>
 11     <groupId>com.bie</groupId>
 12     <artifactId>springboot-hello</artifactId>
 13     <version>0.0.1-SNAPSHOT</version>
 14     <name>springboot-hello</name>
 15     <description>Demo project for Spring Boot</description>
 16 
 17     <properties>
 18         <java.version>1.8</java.version>
 19     </properties>
 20 
 21     <dependencies>
 22         <dependency>
 23             <groupId>org.springframework.boot</groupId>
 24             <artifactId>spring-boot-starter-web</artifactId>
 25         </dependency>
 26         <!-- 添加junit環境的jar包 -->
 27         <dependency>
 28             <groupId>org.springframework.boot</groupId>
 29             <artifactId>spring-boot-starter-test</artifactId>
 30             <scope>test</scope>
 31             <!--<exclusions>
 32                 <exclusion>
 33                     <groupId>org.junit.vintage</groupId>
 34                     <artifactId>junit-vintage-engine</artifactId>
 35                 </exclusion>
 36             </exclusions>-->
 37         </dependency>
 38         <!-- thymeleaf的啟動器 -->
 39         <dependency>
 40             <groupId>org.springframework.boot</groupId>
 41             <artifactId>spring-boot-starter-thymeleaf</artifactId>
 42         </dependency>
 43         <!-- mybatis的啟動器 -->
 44         <dependency>
 45             <groupId>org.mybatis.spring.boot</groupId>
 46             <artifactId>mybatis-spring-boot-starter</artifactId>
 47             <version>2.1.1</version>
 48         </dependency>
 49         <!-- mysql數據庫驅動的依賴包 -->
 50         <dependency>
 51             <groupId>mysql</groupId>
 52             <artifactId>mysql-connector-java</artifactId>
 53         </dependency>
 54         <!-- druid數據庫連接池 -->
 55         <dependency>
 56             <groupId>com.alibaba</groupId>
 57             <artifactId>druid</artifactId>
 58             <version>1.1.10</version>
 59         </dependency>
 60         <dependency>
 61             <groupId>junit</groupId>
 62             <artifactId>junit</artifactId>
 63             <scope>test</scope>
 64         </dependency>
 65         <!-- DevTools 的坐標,熱部署 -->
 66         <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
 67         <!-- <dependency>
 68             <groupId>org.springframework.boot</groupId>
 69             <artifactId>spring-boot-devtools</artifactId>
 70             <version>2.2.10.RELEASE</version>
 71         </dependency> -->
 72         
 73         <!-- https://mvnrepository.com/artifact/org.springframework/springloaded -->
 74         <dependency>
 75             <groupId>org.springframework</groupId>
 76             <artifactId>springloaded</artifactId>
 77             <version>1.2.8.RELEASE</version>
 78         </dependency>
 79     </dependencies>
 80 
 81     <build>
 82         <plugins>
 83             <plugin>
 84                 <groupId>org.springframework.boot</groupId>
 85                 <artifactId>spring-boot-maven-plugin</artifactId>
 86             </plugin>
 87         </plugins>
 88         <!-- 此配置可以解決,如果將*.mapper.xml配置文件放入到src/main/java,無法加載mybatis映射文件的問題 -->
 89         <!--<resources>
 90             <resource>
 91                 <directory>src/main/java</directory>
 92                 <includes>
 93                     <include>**/*.xml</include>
 94                 </includes>
 95             </resource>
 96         </resources>-->
 97     </build>
 98 
 99 
100 </project>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM