Gateway网关和Feign调用开启gzip压缩

本文介绍了一种提高数据响应速度的方法,通过在网关层实现数据压缩,并利用Feign进行服务间调用来进一步优化。文章详细展示了如何在Spring Cloud Gateway中添加自定义过滤器以实现响应体压缩,同时介绍了如何在YAML配置文件中设置Feign的gzip压缩。

总监:咳咳咳…小王啊,最近挺清闲啊!

我:没有没有,还有好多bug没改呢。

总监:有个需求需要你弄一下。

我:好的好的。(摸鱼被抓不敢反抗啊!!!)

总监:现在的数据响应我觉得还可以提升一下,你把所有的响应在响应前都压缩一遍。还有服务之间feign调用的形式。

我:好的好的,现在就干。

总监:晚上给我个demo!


一:GateWay网关实现数据压缩

思路:
  1. 要网关吐出压缩后的数据,则要实现一个网关拦截器,在最后一层的拦截器当中修改响应的数据。
  2. 查看浏览器请求头中的Accept-Encoding: gzip, deflate一项,这里使用gzip压缩。

好啦,既然没有那么复杂,就开始干吧。


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.reactivestreams.Publisher;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBufferFactory
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值