Feign in SpringCloud makes service call java.io.IOException: too many bytes written problem solving

Problem Description

When calling microservices through Feign in Spring Cloud, an error is reported: java.io.IOException: too many bytes written
Error graph

source of problem

In the Feign call interceptor, when the header is passed, the content of the written header data is too large

Solution

Mainly modify the Feign interceptor:

  1. You can only pass the request token, and use request.getHeader(HttpHeaders.AUTHORIZATION) to judge that the interface authentication token is not empty.
  2. Remove the pass of content-length

The second method I use here is as follows:

@Component
public class TokenFeignClientInterceptor implements RequestInterceptor {
    
    

  /**
   * 通过feign调用微服务之前都先检查下头文件,将请求头文件中的令牌数据再放入到header中,再调用其他微服务
   */
  @Override
  public void apply(RequestTemplate requestTemplate) {
    
    
    try {
    
    
      ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder
          .getRequestAttributes();
      if (null != servletRequestAttributes) {
    
    
        HttpServletRequest request = servletRequestAttributes.getRequest();
        // 获取所有头文件信息的key
        Enumeration<String> headerNames = request.getHeaderNames();
        if (null != headerNames) {
    
    
          while (headerNames.hasMoreElements()) {
    
    
            // 获取头文件的key和value
            String headerName = headerNames.nextElement();
            String headerValue = request.getHeader(headerName);
            // 跳过content-length,不然可能会报too many bites written问题
            if ("content-length".equalsIgnoreCase(headerName)) {
    
    
              continue;
            }
            // 将令牌数据添加到头文件中,当用feign调用的时候,会传递给下一个微服务
            requestTemplate.header(headerName, headerValue);
          }
        }
      }
    } catch (Exception e) {
    
    
      e.printStackTrace();
    }
  }

}

おすすめ

転載: blog.csdn.net/qq_36737803/article/details/123259702