【SpringCloud+】Spring Cloud 2020升级到Spring Cloud 2021

项目场景

前面的文章已经让你的项目焕然一新,升级到了SpringCloud2020版本,摘掉了SpringCloud Hoxton版本低端的帽子,也摆脱了CVE漏洞的攻击。这次我打算将版本继续升级到SpringCloud2021版本,让项目更加先进和可靠。

CVE 的英文全称是“Common Vulnerabilities & Exposures”通用漏洞披露。
产生背景
随着全球范围的黑客入侵不断猖獗,信息安全问题越来越严重。在对抗黑客入侵的安全技术中,实时入侵检测和漏洞扫描评估(IDnA——Intrusion Detection and Assessment)的技术和产品已经开始占据越来越重要的位置。

依赖关系

Spring Cloud VersionSpring Boot Version
2022.0.0-M2Spring Boot >=3.0.0-M2 and < 3.1.0-M1
2022.0.0-M1Spring Boot >=3.0.0-M1 and < 3.0.0-M2
2021.0.0Spring Boot >=2.6.0 and < 3.0.0-M1
2020.0.5Spring 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相关组件,遇到的问题。希望这篇文章能对大家有所帮助。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: springcloud是一个开源的微服务框架,它基于Spring Boot,并提供了一整套解决方案,用于构建分布式系统中的各个微服务。通过使用springcloud,我们可以轻松实现服务注册与发现、负载均衡、断路器、配置中心等功能,简化了微服务开发和管理的复杂度。 springboot是一个基于Spring的轻量级开发框架,它通过开箱即用的原则,提供了一种快速构建应用程序的方式。使用springboot,我们可以简化繁琐的配置,只需少量的代码即可实现一个功能完整的应用程序,并且可以方便地和其他Spring生态的框架进行集成。 OAuth2是一种授权协议,用于保护Web应用程序、移动应用程序和API的资源。通过OAuth2协议,用户可以授权第三方应用程序访问他们的资源,而无需提供他们的密码。它提供了一种安全且可扩展的机制来处理用户身份验证和授权,并且被广泛应用于各种应用程序中。 Spring Security是一个Java框架,用于提供身份验证和访问控制的功能。它可以轻松地集成到Spring应用程序中,提供了一套强大的API和安全策略,用于保护应用程序免受各种攻击,包括身份验证和授权、会话管理、密码加密等。 Redis是一种内存数据存储系统,它以键值对的形式存储数据,并支持多种数据结构,如字符串、列表、集合、有序集合等。Redis具有高速、持久化和可扩展性等特点,可用于缓存、消息队列、分布式锁等各种场景。在使用Spring框架开发时,我们可以使用Redis作为缓存层,提高应用程序的性能和响应速度。 综上所述,Spring Cloud提供了构建和管理微服务的解决方案,Spring Boot简化了应用程序的开发,OAuth2和Spring Security提供了安全和授权的功能,而Redis作为内存数据存储系统,为应用程序提供了可扩展的缓存和数据存储能力。这些技术和框架相互协作,可以帮助开发者更快速、更安全地构建分布式系统。 ### 回答2: Spring Cloud是一个用于构建分布式系统的开发工具包,它提供了多个子项目来解决分布式系统的常见问题,例如服务注册与发现、配置管理、断路器、负载均衡等。Spring Boot是用于简化Spring应用程序开发的工具,它提供了一种自动配置的方式来快速搭建和运行Spring应用。OAuth2是一个开放标准,用于授权访问特定资源,它允许用户使用某个网站的授权信息来访问其他网站上的受保护资源。Spring Security是一个全面的身份验证和授权框架,它提供了一套安全服务,用于保护Web应用程序中的资源。Redis是一个高性能的键值存储系统,它常被用作缓存、队列、消息中间件等。 结合以上几个技术,可以构建一个基于Spring Cloud的分布式系统,使用Spring Boot快速搭建各个服务,使用Spring Security进行身份验证和授权管理。而OAuth2可以用于保护系统中的资源,通过认证服务器进行用户认证和授权,使得只有授权的用户才能访问相应的资源。Spring Security与OAuth2可以集成使用,通过Spring Security提供的权限管理功能来管理不同角色对资源的访问权限。同时,将Redis作为缓存服务器,可用于提高系统的性能和响应速度。 总之,Spring CloudSpring Boot、OAuth2、Spring Security和Redis等技术可以在构建分布式系统时发挥重要作用,帮助我们快速搭建实现各个功能模块,并提供高性能和安全性。 ### 回答3: Spring Cloud是一套基于Spring Boot的微服务框架,它提供了在分布式系统中构建和管理各种微服务的解决方案。它具有服务注册与发现、负载均衡、熔断、服务网关等功能,可以方便地实现微服务架构。 Spring Boot是一个用于快速开发基于Spring框架的应用程序的工具,它简化了Spring应用程序的配置和部署流程。它提供了自动化配置、内嵌服务器、开箱即用的特性,使得我们只需要关注业务逻辑的开发而不用过多关注框架的配置。 OAuth2是一种开放标准的授权协议,它使得用户可以通过授权的方式将与用户相关的信息共享给第三方应用程序。它使用令牌的方式进行授权,具有安全性高、可扩展性好的优点,常用于实现单点登录和授权管理。 Spring Security是一个用于在Java应用程序中提供身份验证和访问控制的框架。它可以与Spring Boot和Spring Cloud集成,提供了认证、授权、密码加密等功能,帮助我们更好地保护应用程序的安全。 Redis是一种高性能的键值存储系统,它支持多种数据结构,如字符串、列表、哈希表等。它具有高并发读写、持久化、分布式等特点,常用于缓存、消息队列、会话管理等场景。 综上所述,Spring Cloud提供了构建微服务的解决方案,Spring Boot简化了Spring应用程序的开发,OAuth2实现了授权管理,Spring Security提供了身份验证和访问控制,而Redis则可以用于缓存和数据存储。这些技术的结合可以帮助我们构建安全、高效的分布式系统。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值