SpringCloud Feign調(diào)用報(bào)錯(cuò)feign.RetryableException: too many bytes written executing

SpringCloud Feign調(diào)用報(bào)錯(cuò)feign.RetryableException: too many bytes written executing

版本:

SpringCloud : Greenwich.SR5

SpringBoot : 2.1.9.RELEASE

SpringCloudAlibaba : 2.1.0.RELEASE

看到這個(gè)錯(cuò)誤第一時(shí)間我也是打開(kāi)百度/Goole 但是搜出來(lái)的,無(wú)一例外 基本都是添加feign增強(qiáng)包 feign-httpclient 或者feign-okhttp 包;

image

無(wú)奈之下只好一步步debug 發(fā)現(xiàn)是把request.body 寫(xiě)入到流時(shí)發(fā)生的錯(cuò)誤.java.io.IOException: insufficient data written

image

后面搜到body是跟Content-Length 有關(guān)系的... 附上博主鏈接 https://my.oschina.net/u/4410077/blog/3323588 看了之后 原來(lái)發(fā)生這個(gè)問(wèn)題的原因跟我一樣,因?yàn)榉?wù)之間調(diào)用需要攜帶一些用戶信息之類的 所以實(shí)現(xiàn)了Feign的RequestInterceptor攔截器復(fù)制請(qǐng)求頭,復(fù)制的時(shí)候是所有頭都復(fù)制的,可能導(dǎo)致Content-length長(zhǎng)度跟body不一致. 所以只需要判斷如果是Content-length就跳過(guò)

原配置 :

/**
 * @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);
                // 跳過(guò) 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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容