SpringBoot gzip压缩
在这个流量为王的互联网时代,流量就是金钱,而对于初创公司来讲,做到了海量的流量随之带来的就是一笔不小的流量费用,了解过CDN或者自己配置过nginx的同学可能都知道,他们都可以实现对应的gzip压缩,但那大部分都是基于一些静态文件的压缩,比如:jpg,png,html,css等,这个不是本文的重点,今天主要介绍的基于springboot的gzip压缩服务接口返回的response数据
一、springboot的gzip压缩演示
1、application.properties配置
#开启gzip压缩
server.compression.enabled=true
#支持对应的类型
server.compression.mime-types=application/json,application/xml,text/html,text/xml,text/plain
#默认只会压缩2048字节以上
server.compression.min-response-size=100
2、新增一个测试的controller
/**
*
*
* @author zhangqh
* @date 2018年6月7日
*/
@RestController
@RequestMapping("test")
public class TestController {
/**
* 测试
* @return
*/
@RequestMapping("/gzipJson")
public String json(){
return "[{\"userName\":\"ttt\", \"type\":0,\"gender\":2, \"description\":\"中国21333434444\"},"+
"{\"userName\":\"ttt\", \"type\":0,\"gender\":2, \"description\":\"中国21333434444\"},"+
"{\"userName\":\"ttt\", \"type\":0,\"gender\":2, \"description\":\"中国21333434444\"},"+
"{\"userName\":\"ttt\", \"type\":0,\"gender\":2, \"description\":\"中国21333434444\"},"+
"{\"userName\":\"ttt\", \"type\":0,\"gender\":2, \"description\":\"中国21333434444\"},"+
"{\"userName\":\"ttt\", \"type\":0,\"gender\":2, \"description\":\"中国21333434444\"},"+
"{\"userName\":\"ttt\", \"type\":0,\"gender\":2, \"description\":\"中国21333434444\"},"+
"{\"userName\":\"ttt\", \"type\":0,\"gender\":2, \"description\":\"中国21333434444\"},"+
"{\"userName\":\"ttt\", \"type\":0,\"gender\":2, \"description\":\"中国21333434444\"},"+
"{\"userName\":\"ttt\", \"type\":0,\"gender\":2, \"description\":\"中国21333434444\"},"+
"{\"userName\":\"ttt\", \"type\":0,\"gender\":2, \"description\":\"中国21333434444\"},"+
"{\"userName\":\"ttt\", \"type\":0,\"gender\":2, \"description\":\"中国21333434444\"},"+
"{\"userName\":\"ttt\", \"type\":0,\"gender\":2, \"description\":\"中国21333434444\"},"+
"{\"userName\":\"ttt\", \"type\":0,\"gender\":2, \"description\":\"中国21333434444\"},"+
"{\"userName\":\"ttt\", \"type\":0,\"gender\":2, \"description\":\"中国21333434444\"},"+
"{\"userName\":\"ttt\", \"type\":0,\"gender\":2, \"description\":\"中国21333434444\"},"+
"{\"userName\":\"ttt\", \"type\":0,\"gender\":2, \"description\":\"中国21333434444\"},"+
"{\"userName\":\"ttt\", \"type\":0,\"gender\":2, \"description\":\"中国21333434444\"},"+
"{\"userName\":\"ttt\", \"type\":0,\"gender\":2, \"description\":\"中国21333434444\"},"+
"{\"userName\":\"ttt\", \"type\":0,\"gender\":2, \"description\":\"中国21333434444\"},"+
"{\"userName\":\"ttt\", \"type\":0,\"gender\":2, \"description\":\"中国21333434444\"},"+
"{\"userName\":\"ttt\", \"type\":0,\"gender\":2, \"description\":\"中国21333434444\"},"+
"{\"userName\":\"ttt\", \"type\":0,\"gender\":2, \"description\":\"中国21333434444\"},"+
"{\"userName\":\"ttt\", \"type\":0,\"gender\":2, \"description\":\"中国21333434444\"},"+
"{\"userName\":\"ttt\", \"type\":0,\"gender\":2, \"description\":\"中国21333434444\"}]";
}
}
3、浏览器访问效果
未开启gzip效果:
开启gzip效果:
怎么样,很明显吧,开启gzip压缩之后大小降为原来的1/6
二、springboot底层gzip配置详解
找到spring-boot-autoconfigure jar包中web目录中的ServerProperties类可以发现有一个compression属性如下:
@NestedConfigurationProperty
private Compression compression = new Compression();
进去Compression中如下:
public class Compression {
/**
* 默认response压缩是关闭的.
*/
private boolean enabled = false;
/**
* 默认支持的 MIME types
*/
private String[] mimeTypes = new String[] { "text/html", "text/xml", "text/plain",
"text/css", "text/javascript", "application/javascript" };
/**
* 过滤不需要压缩的接口.
*/
private String[] excludedUserAgents = null;
/**
* 默认需要压缩的respo大小nse
*/
private int minResponseSize = 2048;
// 省略部分代码
}
注释中已经把详细的注释加上了,具体没个参数的测试感兴趣的同学可以自己去试试。
原文地址:https://mp.weixin.qq.com/s/FAlU8jKO9aciYm3osp0Jjw