转发: Feign报错feign.RetryableException: too many bytes written executing

 

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 包;

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

后面搜到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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值