Feign远程调用数据量大时压缩导致转json失败

Spring Cloud Feign 支持对请求和响应进行GZIP压缩,以提高通信效率。

fegin默认的Client对响应流不支持对gzip后的字节流进行解析,所以在序列化成对象时会存在解析问题。我们可以使用过滤器实现。开始实现。

1.依赖引入,pom文件添加feign-httpclient,将feign的http组件改为OkHttp

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>2.2.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-okhttp</artifactId>
    <version>10.7.4</version>
</dependency>

2.在application.properties中进行配置

ribbon.httpclient.enabled=false
ribbon.okhttp.enabled=true
feign.httpclient.enabled=false
feign.okhttp.enabled=true//(使用okhttp)
feign.compression.request.enabled=true//开启请求压缩
feign.compression.request.min-request-size=1024//设置请求大小,1024kb以上开始压缩
feign.compression.response.enabled=true//响应压缩
feign.compression.response.useGzipDecoder=true//响应解码

 在nacos里面配置:

ribbon: 
  httpclient:
    enabled: false
  okhttp:
    enabled: true
feign: 
  httpclient:
    enabled: false
  okhttp:
    #使用okhttp
    enabled: true
  compression:
    request:
      #开启请求压缩
      enabled: true
      #设置请求大小,1024kb以上开始压缩
      min-request-size: 1024
    response:
      #响应压缩
      enabled: true
      #响应解码
      useGzipDecoder: true

3.配置启动类

@SpringCloudApplication
//启用Fegin注解
@EnableFeignClients
//启用过滤器,下面使用当前过滤器所在包名
@ServletComponentScan(basePackages = {"com.filter"})
public class Application {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Application.class);
        application.run(args);
    }
}

4.配置过滤器

@WebFilter(filterName="GzipFilter",urlPatterns="/*")
@Slf4j
public class GzipFilter implements Filter {

    @Override
    public void doFilter(
            ServletRequest servletRequest,
            ServletResponse servletResponse,
            FilterChain filterChain
    ) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String contentEncoding = request.getHeader("Content-Encoding");
        // 如果对内容进行了压缩,则解压 
        if (null != contentEncoding && contentEncoding.contains("gzip")){
            request=new GzipRequestWrapper(request);
        }
        filterChain.doFilter(request,servletResponse);
    }


    public class GzipRequestWrapper  extends HttpServletRequestWrapper {

        private HttpServletRequest request;

        public GzipRequestWrapper(HttpServletRequest request) {
            super(request);
            this.request = request;
        }

        @Override
        public ServletInputStream getInputStream() throws IOException {

            ServletInputStream stream = request.getInputStream();
            try {
                final GZIPInputStream gzipInputStream = new GZIPInputStream(stream);
                ServletInputStream newStream = new ServletInputStream() {
                    @Override
                    public int read() throws IOException {
                        return gzipInputStream.read();
                    }
                    @Override
                    public boolean isFinished() {
                        return false;
                    }
                    @Override
                    public boolean isReady() {
                        return false;
                    }
                    @Override
                    public void setReadListener(ReadListener arg0) {
                        // TODO Auto-generated method stub
                    }
                };
                return newStream;
            } catch (Exception e) {
                log.debug("ungzip content fail.", e);
            }
            return stream;
        }
    }
}

 

  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晒干的老咸鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值