1、问题产生描述:在高并发且feign接口处理的数据量很大的情况下,会发生线程阻塞异常导致并发QPS在70多,如下图所示:
图片.png
根据源码进行分析:
图片.png 图片.png 图片.png
在进行类加载的时候,使用了synchronized关键字进行同步加载,在这个地方出现了线程阻塞,导致系统并发吞吐量不高。
解决方案:
采用FastJsonHttpMessageConverter替换MappingJackson2HttpMessageConverter,提示系统并发吞吐量达到1000+。
@EnableSwagger2
@SpringBootApplication
@EnableFeignClients(basePackages = {"com.daling.cart.client"}, defaultConfiguration = FeignFastjsonConfig.class)
public class ShoppingCartApplication {
public static void main(String[] args) {
SpringApplication.run(ShoppingCartApplication.class, args);
}
}
@Configuration
public class FeignFastjsonConfig {
@Bean
public Encoder EncoderfeignEncoder() {
return new SpringEncoder(feignHttpMessageConverter());
}
@Bean
public Decoder DecoderfeignDecoder() {
return new SpringDecoder(feignHttpMessageConverter());
}
private ObjectFactory<HttpMessageConverters> feignHttpMessageConverter() {
final HttpMessageConverters httpMessageConverters = new HttpMessageConverters(getFastJsonConverter());
return () -> httpMessageConverters;
}
private FastJsonHttpMessageConverter getFastJsonConverter() {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
List<MediaType> supportedMediaTypes = new ArrayList<>();
MediaType mediaTypeJson = MediaType.valueOf(MediaType.APPLICATION_JSON_UTF8_VALUE);
supportedMediaTypes.add(mediaTypeJson);
converter.setSupportedMediaTypes(supportedMediaTypes);
FastJsonConfig config = new FastJsonConfig();
config.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
converter.setFastJsonConfig(config);
return converter;
}
}
网友评论