Spring Boot 2.1.2 & Spring Cloud Greenwich 升級記錄

節前沒有新業務代碼,正好Greenwich剛發佈,於是開始爲期四天的框架代碼升級。

之前的版本是 spring boot 1.5.10 , spring cloud Edgware.SR3

依賴升級

  • 增加依賴管理插件 apply plugin: 'io.spring.dependency-management'
  • spring-cloud-starter-eureka → spring-cloud-starter-netflix-eureka-client
  • spring-cloud-starter-feign → spring-cloud-starter-openfeign
  • gradle版本要求4.4

boot : spring-boot-starter-data-jpa

  • delete → deleteById
  • findone → findById

    這個改動確實大,返回值變成了Optional,合理是合理的,只改的真多。。

boot : spring-boot-starter-data-redis

Jedis → Lettuce

還好並沒有使用它的autoconfiguration,配置上有一個小坑,Jedis的redis.timeout是表示connection timeout, 而Lettuce是表示command timeout,之前配置成0的,如果set到Lettuce的commandtimeout裏面那就要拋異常了。

配置:

可以在build.gradle中加入,啓動時會檢查配置是否兼容

compile "org.springframework.boot:spring-boot-properties-migrator" 

注意:完成遷移後需要刪除

圖片描述

警告如上圖會告知最新的配置格式

boot: spring-boot-starter-actuator

endpoint的暴露方式變化,management.endpoints.web.exposure.include = "*" 表示暴露所有endpoints,如果配置了security那麼也需要在security的配置中開放訪問/actuator路徑

boot: spring-boot-starter-security

自動注入的AuthenticationManager可能會找不到

If you want to expose Spring Security’s AuthenticationManager as a bean, override the authenticationManagerBean method on your WebSecurityConfigurerAdapter and annotate it with @Bean.

cloud : eureka

各個項目在註冊中心裏面的客戶端實例IP顯示不正確,需要修改每個項目的

bootstarp.yml

  • ${spring.cloud.client.ipAddress} → ${spring.cloud.client.ip-address}

boot: spring-boot-starter-test:

  • org.mockito.Matchers → org.mockito.ArgumentMatchers 注意build時的warning
  • Mock方法時請使用Mocikto.doReturn(...).when(...),不使用when(...).thenReturn(...),否則@spybean的會調用實際方法

其他問題

  1. 版本升級後會有deprecated的類或方法,所以要注意看console中build的warning信息
  2. 由於spring cloud依賴管理插件強制cuator升級到4.0.1,導致我們使用的elestic-job不能正常工作,只能強行控制版本。

    dependencyManagement {
        imports {
            mavenBom "org.springframework.cloud:spring-cloud-dependencies:${SPRING_CLOUD_VERSION}"
        }
        dependencies {
            dependency 'org.apache.curator:curator-framework:2.10.0'
            dependency 'org.apache.curator:curator-recipes:2.10.0'
            dependency 'org.apache.curator:curator-client:2.10.0'
        }
    }
    
  3. 如果啓用出現error,報bean重複,首先確認是不是故意覆蓋,如重寫spring-boot自帶的bean,如是,可以在bootstrap.yml加入

    spring.main.allow-bean-definition-overriding=true
    
  4. FeignClient註解增加了contextId屬性

    @FeignClient(value = "foo", contextId = "fooFeign")
    

    此contextId即表示bean id,所有注入使用時需要

    @Autowried
    FooFeign fooFeign
    

    如果不寫contextId,當多個class都是@FeignClient("foo"),即會認爲是同一個bean而排除上一條所說的warning

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