Spring Boot 2.1.2 & Spring Cloud Greenwich 升级记录

28次阅读

共计 2146 个字符,预计需要花费 6 分钟才能阅读完成。

节前没有新业务代码,正好 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 的会调用实际方法

其他问题

版本升级后会有 deprecated 的类或方法,所以要注意看 console 中 build 的 warning 信息

由于 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’
}
}

如果启用出现 error,报 bean 重复,首先确认是不是故意覆盖,如重写 spring-boot 自带的 bean,如是,可以在 bootstrap.yml 加入
spring.main.allow-bean-definition-overriding=true

FeignClient 注解增加了 contextId 属性
@FeignClient(value = “foo”, contextId = “fooFeign”)

此 contextId 即表示 bean id,所有注入使用时需要
@Autowried
FooFeign fooFeign

如果不写 contextId,当多个 class 都是 @FeignClient(“foo”),即会认为是同一个 bean 而排除上一条所说的 warning

正文完
 0