記錄spring-boot 1.5.9升級至2.0.1的那些坑

前兩天組裏的大佬心血來潮,讓我這周把項目裏的spring-boot、spring-cloud版本升級到最新版本,目前項目用到的是spring-boot版本爲1.5.9.RELEASE,spring-cloud的版本爲Edgware.SR2,如今按照要求統一升級到2.0.1.RELEASE、Finchley.RELEASE。如下分享一些我遇到的坑:
pom.xml配置以下,內容太長,只貼出部分核心的內容
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>
    <dependencyManagement>
        <dependencies>
             <!--spring boot-->
             <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.0.1.RELEASE</version>
             </dependency>
              <!--spring cloud-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--redis-->
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.9.0</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.9.4</version>
            </dependency>
            <dependency>
                <groupId>com.zaxxer</groupId>
                <artifactId>HikariCP</artifactId>
                <version>2.7.8</version>
            </dependency>
            
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>

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

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
            
          </dependencies>
    </dependencyManagement>

一.spring boot2.0.x引用redis的版本須要升級至2.9.0,之前的版本將不支持jedisConnFactory對象的注入,具體報錯以下redis

clipboard.png

二.jackson-databind 這個依賴能夠將JSON格式的數據轉化爲類對象,這裏有個很蛋疼的坑,以前項目裏用的版本是2.6.3,升級事後本地項目也運行沒啥問題,上了測試環境就報錯,看了啓動日誌上是缺乏Jackson包裏的某個類致使報錯,上網查了下,這種狀況通常是spring的版本和jackson的版本不兼容致使,新版本已經不支持2.6.3的依賴,因而直接換成最新的版本2.9.0,問題解決。spring

三.HikariCP 數據庫鏈接池 一樣也是版本不兼容,須要升級到2.7.8,親測有效。數據庫

clipboard.png

四.spring boot 2.0以前的版本用的spring-cloud-starter-feign,升級事後你會發現這個依賴下有關的類都找不到了,看了下2.0以後的新特性,是改爲了spring-cloud-starter-openfeign,註解類名的路徑也變了,由org.springframework.cloud.netflix.feign.FeignClient變爲org.springframework.cloud.openfeign.FeignClient。mybatis

@Configuration
public class DataSourceConfig {

    @Primary
    @Bean(name = "pmDataSource")
    @ConfigurationProperties(prefix = "credit-push-message.jdbc")
    public DataSource pmDataSource() {
        return DataSourceBuilder.create().type(HikariDataSource.class).build();
    }

}

五.配置數據源這裏也發現一個問題,@ConfigurationProperties,新版本將再也不支持自定義的前綴包含大寫或下劃線格式類型,官方給出的規範是小寫加鏈接號。mvc

clipboard.png

spring:
  http:
    encoding:
      charset: utf-8
      force: false
      enabled: false
  mvc:
    favicon:
      enabled: false
  jmx:
    enabled: false
  application:
    name: push-message-gateway

server:
  port: 21102
  servlet:
   context-path: /push-message-gateway
  ssl:
    enabled: false
  error:
    whitelabel:
      enabled: false

zuul:
  ignored-services: "*"            # 設置忽略的服務
  routes:
    configServer:
      path: /config/**
      serviceId: push-message-config
    gatherServer:
      path: /gather/**
      serviceId: push-message-gather
    messageServer:
      path: /center/**
      serviceId: message-center

info:
  app:
    name: "@parent.artifactId@" #從pom.xml中獲取
    description: "@project.description@"
    version: "@project.version@"
    spring-boot-version: "@spring.boot.version@"

eureka:
   client:
     serviceUrl:
       defaultZone:  ${eureka-zone-url}
   instance:
     preferIpAddress: true
     instance-id: ${spring.cloud.client.ip-address}:${server.port}

六.新版本不支持context-path: /push-message-gateway的寫法,必須寫成servlet:context-path: /push-message-gateway,instance-id這裏的客戶端ip地址也不兼容,須要從spring.cloud.client.ipAddress變成spring.cloud.client.ip-address。app

差很少這些,後續遇到其餘問題還會繼續補充。
相關文章
相關標籤/搜索