目录
1、Gradle 4.x→7.4.1
2、springBoot升级包修改
3、代码修改
4、配置修改
5、兼容zuul网关
6、PathVariable校验不通过默认返回500 =>修改为返回404错误
1) 写法变更
complie 修改为 api
testcomplie 修改为 testImplementation
2)引入integrationTest 单测包
升级org.unbroken-dome.test-sets至4.0.0
3)修改Querydsl配置
增加配置:
compileQuerydsl { options.annotationProcessorPath = configurations.querydsl } configurations { querydsl.extendsFrom compileClasspath } querydsl { library = "com.querydsl:querydsl-apt" jpa = true querydslSourcesDir = "${buildDir}/generated/java" } |
4)修改apply plugin:java
apply plugin: 'java-library' |
1)springcloud 同步升级至2021.0.3版本2)指定flyway 版本为7.15.0 8版本开始不再支持mysql5.7版本
3)client中引入 包
api "org.springframework.cloud:spring-cloud-starter-loadbalancer" |
4)移除spring-cloud-starter-netflix-ribbon引用 与spring-cloud-starter-loadbalancer冲突
5)hystrix已弃用 替换成spring官方推荐的 resilience4j 地址:https://github.com/resilience4j/resilience4j
5)指定commons-beanutils版本
6)指定commons-io版本
7)引入com.google.guava:guava包(如果项目中有用到的话)
8)com.cosium.spring.data:spring-data-jpa-entity-graph版本升级至 2.6.1
9)querydsl-jpa版本升级至5.0.0
10)org.glassfish:javax.el版本升级至3.0.1-b12
11)commons-validator:commons-validator升级至1.7 老版本有安全漏洞
12)org.apache.commons:commons-collections4升级至4.4 老版本有安全漏洞
13)org.springframework.security.oauth:spring-security-oauth2 升级至2.5.2.RELEASE 老版本有安全漏洞
14) 使用PostConstruct注解需要增加引用
api "jakarta.annotation:jakarta.annotation-api:1.3.5" |
15)增加推荐缓存 caffeine引用
com.github.ben-manes.caffeine:caffeine |
1) FallbackFactory类 引用路径调整
2) @Email 注解类 引用路径调整
3)APPLICATION_JSON_UTF8_VALUE 被弃用 替换为APPLICATION_JSON_VALUE 新版会自动解析,不需要额外指定UTF-8字符集
4)@Nonnull注解修改为@NonNull
5)StringUtils 类需要修改为apache lang3包 spring中StringUtils中有多个方法过时
6)jodaTime在新版中已不再支持 换成JSR-310 增加jackson-jsr310包 在需要转换的Date类型字段增加@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate类型会自动转换
api "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" |
7)RequestMappingInfo原构造方法已过时 替换成build构造
8)RabbitTemplate setReturnCallback已过时,替换成setReturnsCallback
9)JPA 对于有内部entity的jpa bean ,lazy注解不再生效会自动生成inner join语句,需要按业务修改
10)JPA 对于所有数据库操作都会默认开启事务,在配置项加上
enableDefaultTransactions=false |
关闭jpa默认事务
11)jpa的更新与删除必须要开启事务,否则会报
No EntityManager with actual transaction available for current thread 错误
12)MongoDbFactory已过时,无法正常注入 替换成MongoDatabaseFactory
13)配置taskExcutor线程池
@Configuration public class AsyncConfig { @Bean public TaskExecutor taskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); // 设置核心线程数 executor.setCorePoolSize(20); // 设置最大线程数 executor.setMaxPoolSize(50); // 允许核心线程超时 executor.setAllowCoreThreadTimeOut(Boolean.TRUE); // 设置队列容量 executor.setQueueCapacity(1000); // 设置线程活跃时间(秒) executor.setKeepAliveSeconds(60); // 设置默认线程名称 executor.setThreadNamePrefix("cloud-async-"); // 设置拒绝策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 等待所有任务结束后再关闭线程池 executor.setWaitForTasksToCompleteOnShutdown(true); return executor; } } |
1)spring.profiles配置已过时,修改为spring.config.active.on-profile
2)ribbon已弃用,要使用新的服务配置
spring: cloud: discovery: client: simple: instances: bizr-tradec-service: - uri: ${bizr.tradec.server.name:http://xxxx} globiz-service: - uri: ${globiz.server.name:http://xxxx} tendata-accounts-service: - uri: ${server.name:http://xxxx} wenda-service: - uri: ${wenda.server.name:http://xxxx} globalsou-company-service: - uri: https://xxx.com mailsou-service: - uri: http://xxx.com mex-service: - uri: ${mex.server.name:http://xxxx} mail-service: - uri: ${dmx-edm.server.name:http://xxxx} |
3)后端服务间调用要去掉网关的前缀
4)当使用@PathVariable注解时,对于jpa可能查不到结果情况要加上required = false
5)有路径匹配配置 增加配置参数
mvc: pathmatch: matching-strategy: ant_path_matcher |
6)mysql驱动 com.mysql.jdbc.Driver 修改为 com.mysql.cj.jdbc.Driver
1)指定spring-cloud-starter-netflix-zuul版本 为 2.2.10.RELEASE
1)新加ZuulConfiguration文件
package cn.tendata.wenda.webapp.config; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.boot.web.servlet.error.ErrorController; import org.springframework.cglib.proxy.Callback; import org.springframework.cglib.proxy.CallbackFilter; import org.springframework.cglib.proxy.Enhancer; import org.springframework.cglib.proxy.MethodInterceptor; import org.springframework.cglib.proxy.MethodProxy; import org.springframework.cglib.proxy.NoOp; import org.springframework.cloud.netflix.zuul.filters.RouteLocator; import org.springframework.cloud.netflix.zuul.web.ZuulController; import org.springframework.cloud.netflix.zuul.web.ZuulHandlerMapping; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class ZuulConfiguration { /** * The path returned by ErrorController.getErrorPath() with Spring Boot < 2.5 (and no longer * available on Spring Boot >= 2.5). */ private static final String ERROR_PATH = "/error"; private static final String METHOD = "lookupHandler"; /** * Constructs a new bean post-processor for Zuul. * * @param routeLocator the route locator. * @param zuulController the Zuul controller. * @param errorController the error controller. * @return the new bean post-processor. */ @Bean public ZuulPostProcessor zuulPostProcessor(@Autowired RouteLocator routeLocator, @Autowired ZuulController zuulController, @Autowired(required = false) ErrorController errorController) { return new ZuulPostProcessor(routeLocator, zuulController, errorController); } private enum LookupHandlerCallbackFilter implements CallbackFilter { INSTANCE; @Override public int accept(Method method) { if (METHOD.equals(method.getName())) { return 0; } return 1; } } private enum LookupHandlerMethodInterceptor implements MethodInterceptor { INSTANCE; @Override public Object intercept(Object target, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { if (ERROR_PATH.equals(args[0])) { // by entering this branch we avoid the ZuulHandlerMapping.lookupHandler method to trigger the // NoSuchMethodError return null; } return methodProxy.invokeSuper(target, args); } } private static final class ZuulPostProcessor implements BeanPostProcessor { private final RouteLocator routeLocator; private final ZuulController zuulController; private final boolean hasErrorController; ZuulPostProcessor(RouteLocator routeLocator, ZuulController zuulController, ErrorController errorController) { this.routeLocator = routeLocator; this.zuulController = zuulController; this.hasErrorController = (errorController != null); } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (hasErrorController && (bean instanceof ZuulHandlerMapping)) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(ZuulHandlerMapping.class); enhancer.setCallbackFilter( LookupHandlerCallbackFilter.INSTANCE); // only for lookupHandler enhancer.setCallbacks( new Callback[]{LookupHandlerMethodInterceptor.INSTANCE, NoOp.INSTANCE}); Constructor> ctor = ZuulHandlerMapping.class.getConstructors()[0]; return enhancer.create(ctor.getParameterTypes(), new Object[]{routeLocator, zuulController}); } return bean; } } } |
注意:使用zuul网关的项目无法使用spring-cloud-starter-loadbalancer,只能使用ribbon
在boot2.6使用ribbon时需要重写RibbonLoadBalancerClient类,并实现choose方法。
ServiceInstance choose(String serviceId, Request |
@ControllerAdvice
public class GlobalControllerExceptionHandler extends ResponseEntityExceptionHandler {
/**
* PathVariable 校验不通过 返回404错误
*/
protected ResponseEntity