Spring Cloud版本由1.5.x升級到2.x

https://blog.csdn.net/evasnowind/article/details/105864183

衆所周知,spring cloud 1.5版本與2.x版本差異很大,官方沒有做向下兼容,導致大家對於升級spring cloud版本都非常慎重。

此處,首先推薦閱讀官方給出的遷移手冊 Spring Boot 2.0 Migration Guide

雖然是英文的,但建議還是好好讀讀。

https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide

預備知識

廢話一下:升級操作前務必要對spring, spring boot, spring cloud版本、各自關聯有清晰的認識。建議看官網,然後也可以參考此文章SpringBoot與SpringCloud的版本對應詳細版

https://blog.csdn.net/qq32933432/article/details/89375630

事前準備

首先,官方建議,若要升級到2.x,首先應該分步驟來:

  • 先升級到1.5.x
  • 再升級到2.0
  • 再升級到2.1等更高版本

再者,建議在遷移到2.0時,先在pom中添加如下依賴:

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-properties-migrator</artifactId>
 <scope>runtime</scope>
</dependency>


這個是官方提供用於驗證原有配置是否還生效的包,項目啓動時將會給出提示,並告知在新版spring cloud中,對應配置關鍵字是什麼。

開始遷移

更新步驟說起來簡單,即更新依賴文件中的版本號,但spring cloud涉及n多組件,升級過程中必然遇到各種坑。

1. 更新spring cloud, spring boot

以本人負責項目爲例。spring cloud更新前:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.SR4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


更新後:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


spring boot更新前:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.13.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>


spring boot更新後:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>


2. 更新關聯的包

更新這兩個包後,若有使用eureka/feign/hystrix,會因爲這些包重命名、原有包在新版spring cloud中不能再使用,此處僅給出對應包名,細微之處建議仔細查看相關的官方文檔,確保使用上不會問題。

E版本重命名列表參見Spring Cloud Edgware Release Notes

https://github.com/spring-projects/spring-cloud/wiki/Spring-Cloud-Edgware-Release-Notes


在這裏插入圖片描述

2.1 eureka

mvnrepository 更新前:下面的這兩個包在mvnrepository.com上已經被標記爲deprecated

        <dependency>
            <!--mvnrepository 上標記爲:Spring Cloud Starter Eureka Server (deprecated, please use spring-cloud-starter-netflix-eureka-server) -->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <!--mvnrepository 上標記爲:Spring Cloud Starter Eureka (deprecated, please use spring-cloud-starter-netflix-eureka-client) -->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>


更新後

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>


2.2 feign

更新前:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>


更新後:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>


注意,feign本身也有一些變化,可以參考此文章 Spring cloud系列十八 Spring Cloud 從Dalston.SR5到Greenwich.SR1 的升級記錄 ,比如:

https://blog.csdn.net/hry2015/article/details/89340069

Spring cloud系列十八 Spring Cloud 從Dalston.SR5到Greenwich.SR1 的升級記錄


  1. feign中部分包名變化
org.springframework.cloud.netflix.feign.**.java -> org.springframework.cloud.openfeign.**.java
  1. @FeignClient增加一個屬性值contextId

具體變化建議查看官網,懶的話可以看下面兩篇文章:

https://blog.csdn.net/hry2015/article/details/89340069

Spring cloud系列十八 Spring Cloud 從Dalston.SR5到Greenwich.SR1 的升級記錄

https://blog.csdn.net/f641385712/article/details/104211659

享學Feign 一、原生Feign初體驗,Netflix Feign or Open Feign


3. 屬性變化

此處就要感謝spring-boot-properties-migrator 這個包了,會自動給出新版本里應該用哪個屬性。比如:應用的上下文路徑:server.context-path: authority --> server.servlet.context-path: authority 上傳文件參數配置:spring.http.multipart.* -> spring.servlet.multipart.* 如原 spring.http.multipart.maxFileSize:5Mb -> spring.servlet.multipart.maxFileSize:5MB 同時這裏5Mb中的Mb的必須是大寫字母,必須是5MB,否則會拋出異常:failed to convert java.lang.String to @org.springframework.boot.convert.DataSizeUnit org.springframework.util.unit.DataSize

4. 其他包的升級

spring cloud, spring boot, spring升級後,相關聯的包及建議同步升級,否則可能會無效,比如說pagehelper,druid,jedis等。既然要動依賴包,建議這次就一次性到位,本身整個項目的所有功能肯定需要回歸驗證一遍,建議就一起升級。可以參考這兩篇文章:

https://blog.csdn.net/hry2015/article/details/89340069

Spring cloud系列十八 Spring Cloud 從Dalston.SR5到Greenwich.SR1 的升級記錄

https://www.cnblogs.com/javastack/p/9446837.html

Spring Cloud 升級最新 Finchley 版本,踩了所有的坑!


本文分享自微信公衆號 - 阿提說說(itsaysay)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章