1.更新swagger
升级版本swagger报错
原因: springfox假设springMVC 的路径匹配策略是ant-path-matcher,而springboot 2.6版本以上默认的匹配策略是path-patermn-matcher
报错代码
//解决方案,添加配置
package ****.common.swagger.config;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider;
import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;
import java.lang.reflect.Field;
import java.util.List;
import java.util.stream.Collectors;
/**
* swagger 在 springboot 2.6.x 不兼容问题的处理
*
* @author ruoyi
*/
public class SwaggerBeanPostProcessor implements 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);
}
}
}
//同时在application/bootstrap.ym1文件中添加mvc的路径匹配策略
spring
mvc :
pathmatch:
matching-strategy: ant_path_matcher
//spring 2.7.默认不解决循环依赖问题,需要自己添加
spring:
main:
a11ow-circular-references: true
2.redis配置更新
package ****;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data,redis.connection.RedisStandaloneConfiquration;
import org.springframework.data,redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis,serializer.RedisSerializationContext;
import org.springframework.data,redis,serializer StringRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson .databind.objectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
/**
* 当前Agent应用的简称
*/
@Value("${spring.redis.host:'}")
private String redisHost;
@Value("${spring.redis.port:''}")
private String redisPort;
@Value("${spring.redis.database:''}")
private String redisDatabase;
@Bean
public RedisConnectionFactory redisConnectionFactory(){
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration()
configuration.setHostName(redisHost);
configuration.setPort(Integer.parseInt(redisPort));
confiquration.setDatabase(Integer.parseInt(redisDatabase));
return new LettuceConnectionFactory(configuration);
}
@Bean
@SuppressWarnings(value = {"unchecked","rawtypes"})
public RedisTemplate<Object, Object> redisTemplate() {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory());
FastJson2sonRedisSerializer serializer = new FastJson2JsonRedisSerializer(object.class);
ObjectMapper mapper = new ObjectMapper():
mapper.setVisibility(PropertyAccessor.ALL,JsnAutoDetect.Visibility.ANY)
mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance
ObjectMapper.DefaultTyping.NON_FINAL):
serializer.setObjectMapper(mapper);
template.setValueSerializer(serializer);
//使用stringRedisSerializer来序列化和反序列化redis 的key值
template.setKeySerializer(new StringRedisSerializer());
template.afterPropertiesSet);
return template;
}
@Bean
public RedisCacheManager redisCacheManager() {
FastJson2JsonRedisSerializer<0bject> serializer = new FastJson2JsonRedisSerializer<>(object.class);
RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
configuration = configuration.serializealueslith(RedisSerializationContext.SerializationPair.fromSerializer(serializer)).
//.entryTtl(Duration.ofDays(30))
return RedisCacheManager
.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory()))
.cacheDefaults(configuration).build();
}
}
3.中间件springboot升级
<!-- springboot-升级到2.7.18 升级c1oud版本 -->
<spring-boot.version>2.7.18</spring-boot.version>
<spring-cloud.version>2021.0.8</spring-cloud.version>
<spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version>
<spring-boot-admin.version>2.7.11</spring-boot-admin.version>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hdrhistogram</groupId>
<artifactId>HdrHistogram</artifactId>
<version>2.1.12</version>
</dependency>
<dependency>
<groupId>com.google.quava</groupId>
<artifactId>quava</artifactId>
<version>30.0-jre</version>
</dependency>
4.找不到或无法加载主类 Application
原因:版本升级后读取不到bootstrap.yml配置文件的配置
解决办法:引入依赖
<!-- 报错信息 No spring.config.import property has been defined -->
<!-- 版本升级到2.7.18之后,bootstrap文件读取被单独分成一派,不再提供默认支持,需要自己独立添加依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.0.4</version>
</dependency>
5.回调接口FallbackFactory
//原来引入包
import feign.hystrix.FallbackFactory;
//现在引入包
import org.springframework.cloud.openfeign.FallbackFactory;
6.报错信息ServerCodecConfigurer
****************************
APPLICATION FAILED TO START
****************************
Description:
Parameter 0 of method modifyRequestodyGatewayfilterfactory in org.springframework.cloud.gateway.config.GateayAutocanfiguration required a bean of type
'org.springframework.http.codec.Servercodecconfigurer' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.springframework.http.codec.ServerCodecconfigurer' in your configuration.
解决办法:
去除网关中的spring-boot-starter-web依赖
7.项目启动后,服务间调用找不到已经启动的应用
<!-- 在springcloud 2020.0.0版本开始,ribbon被选弃,负载均衡方案改为1oadbanlancer -->
<!-- 需要加入以下依赖 否则会 无法处理 uri:1b:// 产生503错误-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
<version>4.1.0</version>
</dependency>