总监:咳咳咳…小王啊,最近挺清闲啊!
我:没有没有,还有好多bug没改呢。
总监:有个需求需要你弄一下。
我:好的好的。(摸鱼被抓不敢反抗啊!!!)
总监:现在的数据响应我觉得还可以提升一下,你把所有的响应在响应前都压缩一遍。还有服务之间feign调用的形式。
我:好的好的,现在就干。
总监:晚上给我个demo!
一:GateWay网关实现数据压缩
思路:
- 要网关吐出压缩后的数据,则要实现一个网关拦截器,在最后一层的拦截器当中修改响应的数据。
- 查看浏览器请求头中的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

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





