一 前言
本篇是关于spirngboot 开启响应压缩的技术点;主要是为了节省网络传输成本;
公众号:知识追寻者
知识追寻者(Inheriting the spirit of open source, Spreading technology knowledge;)
二GZIP压缩介绍
在 各种restful请求 中,我们通常会使用 http 的 json 格式 提交文本;由于为了减少网络传输成本,通常来说都会开启压缩请求技术;htpp 请求头中携带 Content-Encoding: gzip 说明就是有进行请求压缩;
三 SpringBoot开启Gzip压缩
如果是springboot 项目默认是不开窍 gzip压缩技术,需要我们手动去配置开启,根据不同的响应格式,开启的类型也不一样,知识追寻者在下面给出了常用的媒体类型压缩; 如果是application.properties 配置如下:
server.compression.enabled=true
server.compression.mime-types=application/javascript,text/css,application/json,application/xml,text/html,text/xml,text/plain
如果是application.yml 中加入如下配置:
server:
compression:
enabled: true
min-response-size: 1024
mime-types: application/javascript,application/json,application/xml,text/html,text/xml,text/plain,text/css,image/*
压缩 与 不压缩的区别在于 ,响应的速度会有很大的差距,携带传输文本的体积也不一样;开启压缩技术能够减少带宽,建议大家开启gzip压缩,加快请求和响应速度;
四Tomcat开启Gzip压缩
tomcat中使用gzip需要进行配置,在server.xml中,在Connector标签中加入如下属性
compression="on" compressionMinSize="2048" compressableMimeType="text/html,text/css,text/javascript"
五 测试
前端开启gzip压缩,使用谷歌浏览器默认开启zip压缩
后端开启gzip压缩
明显的1.2MB 和 670kb 明显的差距还是很大;响应时间也是差距很大,虽然没截图,也能猜的出来;
本套教程
- springboot入门 (1)
- Springboot自定义banner(2)
- springboot配置文件解析(3)
- springboot集成mybatis(4)
- springboot集成jdbcTemplate(5)
- spingboot单元测试(6)
- springboot集成thymeleaf(7)
- springboot多文件上传(8)
- springboot文件下载(9)
- Springboot自定义异常类(10)
- springboot多环境配置(11)
- springboot自动配置原理解析(12)
- springboot集成restTemplate接口调用(13)
- springboot集成任务调度(14)
- springboot跨域CORS处理(15)
- springboot开启GIZP压缩(16)
- springboot集成logback(17)
- springboot集成Swagger(18)
- springboot集成actuator后台监控(19)
- springboot集成mybatis+oracle+druid(20)
- springboot 集成springsession(21)
- springboot集成jwt(22)
- springboot集成admin后台监控(23)
- springboot集成redis基础篇(24)
- springboot集成redis缓存篇(25)
- springboot使用AOP日志拦截(26)
- springboot集成Validation参数校验(27)
- springboot集成mybatisPlus(28)
- springboot集成shiro(29)
- springboot实现接口等幂次校验(30)
- springboot-集成WebSockets(31)
- restTemplate源码解析(32)
- SpringBoot使用@Async异步调用与线程池(33)
- 待续