文章目录
- 项目场景
- 依赖关系
- 升级记录
- 1.Spring Cloud Dependencies 升级
- 2.Knife4j报错问题 Failed to start bean 'documentationPluginsBootstrapper';
- 3.循环依赖问题 The dependencies of some of the beans in the application context form a cycle
- 4.oauth2授权解析失败 Failed to evaluate expression '#oauth2.hasScope('server')'
- 5.Unsatisfied dependency expressed through method 'routeDefinitionRouteLocator' parameter 1;
项目场景
前面的文章已经让你的项目焕然一新,升级到了SpringCloud2020版本,摘掉了SpringCloud Hoxton版本低端的帽子,也摆脱了CVE漏洞的攻击。这次我打算将版本继续升级到SpringCloud2021版本,让项目更加先进和可靠。
CVE 的英文全称是“Common Vulnerabilities & Exposures”通用漏洞披露。
产生背景
随着全球范围的黑客入侵不断猖獗,信息安全问题越来越严重。在对抗黑客入侵的安全技术中,实时入侵检测和漏洞扫描评估(IDnA——Intrusion Detection and Assessment)的技术和产品已经开始占据越来越重要的位置。
依赖关系
Spring Cloud Version | Spring Boot Version |
---|---|
2022.0.0-M2 | Spring Boot >=3.0.0-M2 and < 3.1.0-M1 |
2022.0.0-M1 | Spring Boot >=3.0.0-M1 and < 3.0.0-M2 |
2021.0.0 | Spring Boot >=2.6.0 and < 3.0.0-M1 |
2020.0.5 | Spring Boot >=2.4.0.M1 and <2.6.0-M1 |
访问SpringCloud官网,可以看到最新版本发布信息,Spring Boot映射版本信息,如何引用等信息。
或者访问https://start.spring.io/actuator/info获取详细的版本对应信息。
"spring-cloud": {
"2021.0.8": "Spring Boot >=2.6.0 and <3.0.0",
"2022.0.4": "Spring Boot >=3.0.0 and <3.2.0-M1"
},
从上面的信息可以看出,如果你想使用SpringCloud2022版本,那么你的SpringBoot版本应该是3.0。而SpringBoot3.0之后就不再支持JDK1.8了,这意味着你需要升级你的Java版本。不过不用担心,升级Java版本并不是一件容易的事情,需要你做好充分的准备和测试。但是如果你真的想尝试试用新的版本,那就开始吧!
升级记录
1.Spring Cloud Dependencies 升级
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>2.7.14</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.1.7</version>
</dependency>
</dependencies>
2.Knife4j报错问题 Failed to start bean ‘documentationPluginsBootstrapper’;
Failed to start bean 'documentationPluginsBootstrapper';nested exception is java.lang.NullPointerException
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181)
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54)
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356)
出现这个问题的原因是:SpringBoot 2.6.0开始,请求路径与Spring MVC处理映射匹配的默认策略已从AntPathMatcher更改为PathPatternParser。
解决方案:
1.application.yml文件中加入如下配置
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
注:微服务中需要修改nacos中application.yml文件
2.资源服务注册(ResourceServerConfig)中加入如下代码
/**
* 解决springboot2.6 和springfox不兼容问题
* @return
*/
@Bean
public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
return new BeanPostProcessor() {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) {
customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
}
return bean;
}
private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
List<T> copy = mappings.stream().filter(mapping -> mapping.getPatternParser() == null).collect(Collectors.toList());
mappings.clear();
mappings.addAll(copy);
}
@SuppressWarnings("unchecked")
private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
try {
Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
field.setAccessible(true);
return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
};
}
3.循环依赖问题 The dependencies of some of the beans in the application context form a cycle
Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.
***************************
APPLICATION FAILED TO START
***************************
Description:
The dependencies of some of the beans in the application context form a cycle:
┌──->──┐
| com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration
└──<-──┘
Action:
Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.
在2.6.0之前,Spring Boot会自动处理循环依赖的问题,而2.6.0版本以上开始检查循环依赖,存在该问题则会报错。
解决方案:
application.yml中加入如下配置
spring.main.allow-circular-references=true
4.oauth2授权解析失败 Failed to evaluate expression ‘#oauth2.hasScope(‘server’)’
解决方案:#oauth2.hasScope(‘scope’) 去掉,或更换为hasAuthority(“SCOPE_scope”) oauth包不再存在
5.Unsatisfied dependency expressed through method ‘routeDefinitionRouteLocator’ parameter 1;
Error creating bean with name 'routeDefinitionRouteLocator' defined in class path resource [org/springframework/cloud/gateway/config/GatewayAutoConfiguration.class]: Unsatisfied dependency expressed through method 'routeDefinitionRouteLocator' parameter 1;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jsonToGRPCFilterFactory' defined in class path resource [org/springframework/cloud/gateway/config/GatewayAutoConfiguration.class]: Unsatisfied dependency expressed through method 'jsonToGRPCFilterFactory' parameter 0;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'grpcSslConfigurer' defined in class path resource [org/springframework/cloud/gateway/config/GatewayAutoConfiguration.class]: Post-processing of merged bean definition failed;
nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.cloud.gateway.config.GrpcSslConfigurer] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]
出现这个问题原因可能是SpringCloud与SpringBoot版本不兼容导致的,也有可能是组件依赖问题,组件缺失等问题。我这里是缺少了grpc-netty的jar。
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>1.50.2</version>
</dependency>
以上是我在升级Spring Cloud相关组件,遇到的问题。希望这篇文章能对大家有所帮助。