SpringCloud Feign调用报错feign.RetryableException: too many bytes written executing

SpringCloud Feign调用报错feign.RetryableException: too many bytes written executing

版本:

SpringCloud : Greenwich.SR5

SpringBoot : 2.1.9.RELEASE

SpringCloudAlibaba : 2.1.0.RELEASE

看到这个错误第一时间我也是打开百度/Goole 但是搜出来的,无一例外 基本都是添加feign增强包 feign-httpclient 或者feign-okhttp 包;

image

无奈之下只好一步步debug 发现是把request.body 写入到流时发生的错误.java.io.IOException: insufficient data written

image

后面搜到body是跟Content-Length 有关系的... 附上博主链接 https://my.oschina.net/u/4410077/blog/3323588 看了之后 原来发生这个问题的原因跟我一样,因为服务之间调用需要携带一些用户信息之类的 所以实现了Feign的RequestInterceptor拦截器复制请求头,复制的时候是所有头都复制的,可能导致Content-length长度跟body不一致. 所以只需要判断如果是Content-length就跳过

原配置 :

/**
 * @author Joe
 * createTime 2020/06/10 18:13
 */
@Log4j2
@Configuration
public class FeignConfiguration implements RequestInterceptor {


    @Override
    public void apply(RequestTemplate template) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        Enumeration<String> headerNames = request.getHeaderNames();
        if (headerNames != null) {
            while (headerNames.hasMoreElements()) {
                String name = headerNames.nextElement();
                String values = request.getHeader(name);
                template.header(name, values);
            }
        } else {
            log.info("feign interceptor error header:{}", template);
        }
    }
}

修改之后:

/**
 * @author Joe
 * createTime 2020/06/10 18:13
 */
@Log4j2
@Configuration
public class FeignConfiguration implements RequestInterceptor {


    @Override
    public void apply(RequestTemplate template) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        Enumeration<String> headerNames = request.getHeaderNames();
        if (headerNames != null) {
            while (headerNames.hasMoreElements()) {
                String name = headerNames.nextElement();
                String values = request.getHeader(name);
                // 跳过 content-length
                if (name.equals("content-length")){
                    continue;
                }
                template.header(name, values);
            }
        } else {
            log.info("feign interceptor error header:{}", template);
        }
    }
}

content-length详解参考文章 :https://juejin.im/post/5d772cb4e51d453b5f1a0502

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容