Spring cloud版本由Edgware.SR5升级到Finchley.RELEASE,Spring boot版本1.5.7升级到2.0.4,开启升级过程。
配置跟服务注册使用阿里的nacos,版本也由0.1.1升级到0.2.1。
- org.springframework.cloud.netflix.feign.FeignClient
引用路径为org.springframework.cloud.openfeign.FeignClient
- org.springframework.cloud.netflix.feign.EnableFeignClients
引用路径为org.springframework.cloud.openfeign.EnableFeignClients
org.hibernate.validator.constraints.NotBlank
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
上面的方法已过时,改为引用下面的包,同时路径改为
import javax.validation.constraints.NotNull;
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.0.Alpha4</version>
</dependency>
如果校验,请求时需要加上@Validated
同时NotBlank的javax.validation.constraints.NotBlank 已过时,换成下面路径
javax.validation.constraints.NotBlank
更多validator的可以参考下面几个文章,增加拦截,统一捕获异常处理
https://blog.csdn.net/u013107634/article/details/80639412
https://www.jianshu.com/p/6f3d809c6705?utm_source=oschina-app
https://blog.csdn.net/ThirdFloor/article/details/81029051
-
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
starter-feign如果引用则指定版本,在2.0中没有starter的,我这边改为了
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
-
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> 改为<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
包路径加入了netflix公司
-
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> <scope>provided</scope> <optional>true</optional> </dependency> 改为<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> <scope>provided</scope> <optional>true</optional> </dependency>
- RelaxedPropertyResolver 2.0舍弃,网上查到的都尝试过了,都不行。官网上也给出了说明https://www.oschina.net/translate/spring-boot-2-0-migration-guide?cmp
改用自动注册Environment ,environment.getProperty进行读取配置赋值
@Autowired
private Environment environment;
@PostConstruct
public void setEnvironment() {
this.url = environment.getProperty("spring.datasource.url");
this.userName= environment.getProperty("spring.datasource.username");
this.password = environment.getProperty("spring.datasource.password");
this.driveClassName = environment.getProperty("spring.datasource.driver-class-name");
this.filters = environment.getProperty("spring.datasource.filters");
this.maxActive = environment.getProperty("spring.datasource.maxActive");
this.initialSize = environment.getProperty("spring.datasource.initialSize");
this.maxWait = environment.getProperty("spring.datasource.maxWait");
this.minIdle = environment.getProperty("spring.datasource.minIdle");
this.timeBetweenEvictionRunsMillis = environment.getProperty("spring.datasource.timeBetweenEvictionRunsMillis");
this.minEvictableIdleTimeMillis = environment.getProperty("spring.datasource.minEvictableIdleTimeMillis");
this.validationQuery = environment.getProperty("spring.datasource.validationQuery");
this.testWhileIdle = environment.getProperty("spring.datasource.testWhileIdle");
this.testOnBorrow = environment.getProperty("spring.datasource.testOnBorrow");
this.testOnReturn = environment.getProperty("spring.datasource.testOnReturn");
this.poolPreparedStatements = environment.getProperty("spring.datasource.poolPreparedStatements");
this.maxOpenPreparedStatements = environment.getProperty("spring.datasource.maxOpenPreparedStatements");
this.typeAliasesPackage = environment.getProperty("mybatis.typeAliasesPackage");
this.xmlLocation = environment.getProperty("mybatis.xmlLocation");
}
- nested exception is java.lang.BootstrapMethodError: java.lang.NoSuchMethodError: org.springframework.kafka.listener.config.ContainerProperties.setClientId(Ljava/lang/String;)
spring-boot-autoconfigure 2.0.4中KafkaAnnotationDrivenConfiguration引用ContainerProperties,
查看该方法中ContainerProperties中setClientId是从2.1.1版本开始,解决办法,升级Spring-Kafka的版本,kafka-clients可以保持不变。
- redis RedisCacheManager 的初始化,Spring 缓存管理,spring2.0 跟Spring1.X版本的构造方法不一样
<cache:annotation-driven proxy-target-class="true"/>
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg ref="redisTemplate"/>
</bean>
改为自定义配置代码,不用xml了
-
WebMvcConfigurerAdapter 类过时问题,2.0中官方推荐通过implements WebMvcConfigurer,还有一种办法是继承WebMvcConfigurationSupport(这个相对麻烦一些,需要override很多方法) 修改是通过implements WebMvcConfigurer,修改哪一个,仅需实现具体方法就可以。 如果修改了还不生效,那是因为没有加上@EnableWebMvc
同样的SwaggerConfiguration也implements WebMvcConfigurer,@EnableWebMvc,重写addResourceHandlers
- java.sql.SQLFeatureNotSupportedException: 这个 org.postgresql.jdbc.PgConnection.createClob() 方法尚未被实作。
大意就是Hibernate要验证pgsql的一个方法,但是jdbc没有实现,但是这个错误呢并不影响,所以下面的方法把这块验证给屏蔽掉。参考https://blog.csdn.net/justlpf/article/details/86570611
- elasticsearch org.elasticsearch.transport.TcpTransport.sendMessage(Ljava/lang/Object;Lorg/elasticsearch/common/bytes/BytesReference;Ljava/lang/Runnable;)
这个主要是jar包冲突的原因omitted for duplicate,下图中可以看到org.elasticsearch.client-transport中引入的netty4的版本5.6.10超过了本身transport的5.5.0
修改办法:显示引入netty,同时exclusion transport中的netty
-------------------至此,所有的module全部成功启动成功,全部注册到nacos中了--------------------------