线上问题处理-feign调用报错(Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) )

线上问题处理-feign调用报错

业务场景:服务1通过Feign调用服务2,测试阶段一切正常,线上有数据丢失(为避免敏感本地简单复现了下)。报错如下:

2021-12-04 13:47:47.774 DEBUG 29480 --- [io-10011-exec-1] .w.s.m.m.a.ServletInvocableHandlerMethod : Could not resolve parameter [0]
 in public void com.example.service1.controller.TaskControler.syncTaskFile(com.example.service1.domain.rep.SyncTaskFileRep): JSON parse
  error: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens; nested exception is 
  com.fasterxml.jackson.core.JsonParseException: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed
   between tokens at [Source: (PushbackInputStream); line: 1, column: 2]

报错不是很简单明了,首先开启Feign日志,具体分析:
开启Feign日志方法:

//在注解中设置configuration类
@FeignClient(value = "service1",contextId = "service1-client",
        configuration = {FeignLogConfiguration.class},
        fallbackFactory = Service1ClientFallbackFactory.class)
public interface Service1Client {

    @PostMapping(value = "/service1/task/sync/task_file")
    Void syncTaskFile(@RequestBody SyncTaskFileReq rep);
}

//日志级别提升为FULL
//feign的日志级别共四种:NONE,BASIC,HEADERS,FULL,建议平时默认为Basic
@Component
public class FeignLogConfiguration {
    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
}

查询日志为:

: [Service1Client#syncTaskFile] ---> POST http://service1/service1/task/sync/task_file HTTP/1.1
: [Service1Client#syncTaskFile] Accept-Encoding: gzip
: [Service1Client#syncTaskFile] Accept-Encoding: deflate
: [Service1Client#syncTaskFile] Content-Encoding: gzip
: [Service1Client#syncTaskFile] Content-Encoding: deflate
: [Service1Client#syncTaskFile] Content-Length: 2762
: [Service1Client#syncTaskFile] Content-Type: application/json

可以看到应该是因为传输体过大,开启了gzip压缩来传输请求,提升效率,查看nacos的配置:

feign:
  sentinel:
    enabled: true
  okhttp:
    enabled: true
  httpclient:
    enabled: false
  client:
    config:
      default:
        connectTimeout: 10000
        readTimeout: 10000
  compression:
    request:
      enabled: true
    response:
      enabled: true

使用的是okhttp,而且开启了compression,okhttp和httpclient这里不重要,主要是因为开启了compression,默认采用gzip压缩,默认请求体大小超过2048就开启,上面看到请求体已经超过了2048,应该是之前测试的时候测试没有造太大的数据,所以没有出现这个问题,线上有大一点的数据就有这个问题。
ps.这里临时解决方法是将feign.compression.request.enabled改为false,也就是不压缩传输。
已经预想到是因为gzip的压缩问题导致了报错,报错为JSON是因为传输过去的请求体以JSON的格式接受,在转换class对象的过程中发生了报错,可以尝试把服务2的@RequestBody改为String类型,然后断点发现,就是乱码。

    //测试feign问题
    @RequestMapping(value = "/sync/task_file", method = RequestMethod.POST)
    public void syncTaskFile(@RequestBody SyncTaskFileRep rep) {
        log.error(rep.toString());
    }
    //改为:
    @RequestMapping(value = "/sync/task_file", method = RequestMethod.POST)
    public void syncTaskFile(@RequestBody String rep) {
        log.error(rep);
    }

打印日志很显然:

2021-12-04 14:22:57.915 ERROR 13688 --- [io-10011-exec-1] c.e.service1.controller.TaskControler    :        ݕ�jA�_E����� ��"�����I����z�a��[X����Ω�����U�>)�pu�hR1)�o\=�������bT@��[4��K7*���	�f���FǡݻS��V�v���IQ�9�g�._�8{O�է�dܸ�������W�[���LaŌ|4�3V��`u�����j�s����nc6w��޴G�����[�]n�F��ԃf�r���t��v���>��A�i9hR�g`\)�d ��a�a�<���=H)��L�3�+�b:1H�����dr06� ���{*H�d�6�HβO�0���k5dR)���=Rv�Cp��9&p��>����I�O�z�����6Y$���b�AJq�/R�N���

原因就是服务2在接收的时候无法解析gzip编码的请求体,如何解决?
1.网上有人说可以设置feign.compression.response.useGzipDecoder为true即可生效,试了一下完全没用。
2.新增过滤器,专门处理Content-Encoding为gzip的请求:

@Slf4j
public class MyGzipRequestWrapper extends HttpServletRequestWrapper {

    private HttpServletRequest request;

    public MyGzipRequestWrapper(HttpServletRequest request) {
        super(request);
        this.request = request;
    }
    
    @Override
    public ServletInputStream getInputStream() throws IOException {

        ServletInputStream inputStream = request.getInputStream();
        try {
            GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream);
            ServletInputStream newStream = new ServletInputStream() {
                @Override
                public boolean isFinished() {
                    return false;
                }

                @Override
                public boolean isReady() {
                    return false;
                }

                @Override
                public void setReadListener(ReadListener readListener) {
                }

                @Override
                public int read() throws IOException {
                    return gzipInputStream.read();
                }
            };
            return newStream;
        } catch (Exception e) {
            log.error("ungzip fail, ", e);
        }
        return inputStream;
    }
}
@Slf4j
public class MyGzipFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String contentEncoding = request.getHeader("Content-Encoding");
        if(StringUtils.isNotBlank(contentEncoding) && contentEncoding.contains("gzip")){
            request = new MyGzipRequestWrapper(request);
        }
        filterChain.doFilter(request, servletResponse);
    }
}
@Configuration
public class FilterConfiguration {

    @Bean
    public FilterRegistrationBean<MyGzipFilter> gzipFilter(){
        FilterRegistrationBean<MyGzipFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new MyGzipFilter());
        registration.addUrlPatterns("/task/*");
        registration.setName("gzipFilter");
        registration.setOrder(5);  //值越小,Filter越靠前。
        return registration;
    }
}

解决!该场景线上环境注册中心为nacos,听人说eureka为注册中心不会有这个情况,尚未验证。


更新: 后面又遇到同样的问题,不过是响应体过大,也就是feign调用时,response采用了gzip压缩,这时候可以用上面没提的一个配置:

feign:
  compression:
    response:
      useGzipDecoder: true	//补充到上面的配置里
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty("feign.compression.response.enabled")
@ConditionalOnMissingBean(type = "okhttp3.OkHttpClient")
@AutoConfigureAfter(FeignAutoConfiguration.class)
public class DefaultGzipDecoderConfiguration {

	private ObjectFactory<HttpMessageConverters> messageConverters;

	public DefaultGzipDecoderConfiguration(
			ObjectFactory<HttpMessageConverters> messageConverters) {
		this.messageConverters = messageConverters;
	}

	@Bean
	@ConditionalOnMissingBean
	@ConditionalOnProperty("feign.compression.response.useGzipDecoder")
	public Decoder defaultGzipDecoder() {
		return new OptionalDecoder(new ResponseEntityDecoder(
				new DefaultGzipDecoder(new SpringDecoder(messageConverters))));
	}

}

看源码其实就是一个默认的gzip解码器,不过注意,如果本身就自定义了Decoder,那就不需要上面那句配置了,比如我这里有一个Decoder要把参数的下划线转驼峰,要处理text/html的MediaType,又要做gzip解压:

public class GzipTextHtmlDecoder {

    @Bean
    public Decoder gzipDecoder() {

        ObjectMapper objectMapper = customObjectMapper();
        //text/html转换器
        HttpMessageConverter textHtmlConverter = new TextMappingJackson2HttpMessageConverter(objectMapper);
        ObjectFactory<HttpMessageConverters> objectFactory = () -> new HttpMessageConverters(textHtmlConverter);

        //先gzip解码,再处理转换器
        return new ResponseEntityDecoder(new DefaultGzipDecoder(new SpringDecoder(objectFactory)));
    }

    public ObjectMapper customObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        //下划线转驼峰
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);

        return objectMapper;
    }

}
  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值