Feign報(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í)間我也是打開百度/Goole 但是搜出來的,無一例外 基本都是添加feign增強(qiáng)包 feign-httpclient 或者feign-okhttp 包;
無奈之下只好一步步debug 發(fā)現(xiàn)是把request.body 寫入到流時(shí)發(fā)生的錯(cuò)誤.java.io.IOException: insufficient data written
后面搜到body是跟Content-Length 有關(guān)系的… 附上博主鏈接 https://my.oschina.net/u/4410077/blog/3323588 看了之后 原來發(fā)生這個(gè)問題的原因跟我一樣,因?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就跳過
原配置 :
/**
* @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:///post/5d772cb4e51d453b5f1a0502
|