SpringBoot 启用 GZIP 对响应进行压缩

news/2024/5/18 20:10:16/文章来源:https://blog.csdn.net/Candyz7/article/details/126617372

SpringBoot Web 应用默认是不启用响应数据的压缩,对大的文本类型的响应数据进行压缩是十分必要的,如 JSON, XML 等应用数据,甚至是 JS, CSS 等。

早先的 Web 应用基本是要配置一个叫做 GzipFilter 之类的东西,然后判断请求的 Accept-Encoding 是否含有 gzip , 再对需要的 Content-Type 响应类型的数据进行压缩。

在使用了 SpringBoot 之后,在碰到有压缩响应的需求的时候,第一件事情应该要想到是否能通过在 application.properties (或 application.yml) 配置就行。于是查阅 SpringBoot 2.7.x 的帮助文档 Spring Boot Reference Document , 搜索关键字 compression ,翻几页就能找到 17.3.6. Enable HTTP Response Compression , 介绍了三个配置项

  1. server.compression.enable=true            (默认为 false, 不启用压缩)
  2. server.compression.min-response-size=2048  (默认至少 2K 字节以及以上大小的响应数据才被压缩, 要在网络带宽与 CPU 消耗上找到一个平衡)
  3. server.compression.mim-types=text/html,text/xml,text/plain,text/css,text/javascript,application/json,application/xml (默认压缩的响应类型)

再往下找到 .A.11.Server Properties , 还有一个相关的选项

  • server.compression.exclude-user-agents=  (默认为空,逗号分隔的 user-agent, 针对什么 user-agent 不启用压缩,可能给测试用的)

如果是 SpringBoot 1.2.x 的话,启用压缩的方法是不一样的,详情同样是参考官方的文档 64.18 Enable HTTP response compression

这里我们再回到当前的 SpringBoot 2.7.x 版本,其实只要是 SpringBoot 1.3+ 的版本,唯一要做的就是配置

 server.compression.enable=true 

其他三个选项根据基本满足我们的日常要求了,或者按需稍加调节。

下面进行一些实战烟训,默认未配置 server.compression.enable 时,即默认为 false, 不启用响应压缩,写一个 controller 方法

@GetMapping(value = "/hello")
public String hello(@RequestParam int length) {return StringUtils.repeat("0", length);
}

@ GetMapping ( value = "/hello" )

public String hello ( @ RequestParam int length ) {

     return StringUtils . repeat ( "0" , length ) ;

}

curl 测试

 bash-3.2$ curl -I http://localhost:8080/hello?length=2047  HTTP/1.1 200  Content-Type: text/plain;charset=UTF-8  Content-Length: 2047  Date: Tue, 30 Aug 2022 03:01:53 GMT  bash-3.2$ curl -I http://localhost:8080/hello?length=2048  HTTP/1.1 200  Content-Type: text/plain;charset=UTF-8  Content-Length: 2048  Date: Tue, 30 Aug 2022 03:01:56 GMT  bash-3.2$ curl -I -H "Accept-Encoding:gzip" http://localhost:8080/hello?length=2049  HTTP/1.1 200  Content-Type: text/plain;charset=UTF-8  Content-Length: 2049  Date: Tue, 30 Aug 2022 03:02:20 GMT 

怎么都不会对响应进行压缩,现在我们在 application.properties 中加上

server.compression.enabled = true

server . compression . enabled = true

重新测试

 bash-3.2$ curl -I -H "Accept-Encoding:gzip" http://localhost:8080/hello?length=2047  HTTP/1.1 200  Content-Type: text/plain;charset=UTF-8  Content-Length: 2047  Date: Tue, 30 Aug 2022 13:22:14 GMT  bash-3.2$ curl -I http://localhost:8080/hello?length=2048  HTTP/1.1 200  vary: accept-encoding  Content-Type: text/plain;charset=UTF-8  Content-Length: 2048  Date: Tue, 30 Aug 2022 13:22:16 GMT  bash-3.2$ curl -I -H "Accept-Encoding:gzip" http://localhost:8080/hello?length=2048  HTTP/1.1 200  vary: accept-encoding  Content-Encoding: gzip  Content-Type: text/plain;charset=UTF-8  Transfer-Encoding: chunked  Date: Tue, 30 Aug 2022 13:22:18 GMT 

响应长度为 2048 及以上会采用压缩,并且这时不管有没有 Accept-Encoding:gzip 都会加上 vary: accept-encoding 用以区分不同的响应数据,像 Varnish 就要考虑 Accept-Encoding 作为 Key 的一部分缓存是否压缩的数据。

关于 server.compression.mime-types

前面提过它的默认值是 text/html,text/xml,text/plain,text/css,text/javascript,application/json,application/xml, 即只对这些 Content-Type 类型的数据进行压缩,不该压缩的类型注意不要重复压缩,如 image/jpg, application/octet-stream 等.

text/plain 对 Content-Type:text/plain;charset=UTF-8 同样是适用的

不支持通配符配置,如不能用 text/* 来涵盖所有以 text/ 开头的类型,像 test/html, test/xml, text/plain 等。必须一个个罗列出来

server.compression.mime-types 中的配置是区分大小写的,如

server.compression.mime-types=TEXT/PLAIN

server . compression . mime - types = TEXT / PLAIN

对 Content-Type:text/plain 是不启作用的

 bash-3.2$ curl -I -H "Accept-Encoding:gzip" http://localhost:8080/hello?length=2049  HTTP/1.1 200  Content-Type: text/plain;charset=UTF-8  Content-Length: 2049  Date: Tue, 30 Aug 2022 14:20:21 GMT 

如果我们把 API 的 Content-Type 也设置为 TEXT/PLAIN 就能被压缩了

@GetMapping(value = "/hello")
public ResponseEntity<String> hello(HttpServletResponse response, @RequestParam int length) {MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();headers.add("Content-Type", "TEXT/PLAIN");return new ResponseEntity<>(StringUtils.repeat("0", length), headers,  HttpStatus.OK);
}

@ GetMapping ( value = "/hello" )

public ResponseEntity < String > hello ( HttpServletResponse response , @ RequestParam int length ) {

     MultiValueMap < String , String > headers = new LinkedMultiValueMap <> ( ) ;

     headers . add ( "Content-Type" , "TEXT/PLAIN" ) ;

     return new ResponseEntity <> ( StringUtils . repeat ( "0" , length ) , headers ,    HttpStatus . OK ) ;

}

 bash-3.2$ curl -I -H "Accept-Encoding:gzip" http://localhost:8080/hello?length=2049  HTTP/1.1 200  vary: accept-encoding  Content-Encoding: gzip  Content-Type: TEXT/PLAIN  Transfer-Encoding: chunked  Date: Tue, 30 Aug 2022 14:22:20 GMT 

注意在 Spring Web controller 方法中,对于标准的 Content-Type 是无法通过 @GetMapping 注解的 produces 和 HttpServletResponse 来改变的

@GetMapping(value = "/hello", produces = "TEXT/PLAIN")
public String hello(HttpServletResponse response, @RequestParam int length) {response.setHeader("Content-Type", "TEXT/PLAIN");return StringUtils.repeat("0", length);
}

@ GetMapping ( value = "/hello" , produces = "TEXT/PLAIN" )

public String hello ( HttpServletResponse response , @ RequestParam int length ) {

     response . setHeader ( "Content-Type" , "TEXT/PLAIN" ) ;

     return StringUtils . repeat ( "0" , length ) ;

}

以上代码最终的 Content-Type 仍然为 text/plain;charset=UTF-8

其他相关的内容

SpringBoot 1.2.2 - <1.3 之间启用压缩的配置

server.tomcat.compression=on
server.tomcat.compressableMimeTypes=application/json,application/xml,text/html,text/xml,text/plain,application/javascript,text/css

server . tomcat . compression = on

server . tomcat . compressableMimeTypes = application / json , application / xml , text / html , text / xml , text / plain , application / javascript , text / css

SpringBoot 1.2.2 之前,在使用 Tomcat 作为内嵌应用服务器时,通过  TomcatConnectorCustomizer

@Component
public class TomcatCustomizer implements TomcatConnectorCustomizer {@Overridepublic void customize(Connector connector) {connector.setProperty("compression", "on");// Add json and xml mime types, as they're not in the mimetype list by defaultconnector.setProperty("compressableMimeType", "text/html,text/xml,text/plain,application/json,application/xml");}
}

@ Component

public class TomcatCustomizer implements TomcatConnectorCustomizer {

   @ Override

   public void customize ( Connector connector ) {

     connector . setProperty ( "compression" , "on" ) ;

     // Add json and xml mime types, as they're not in the mimetype list by default

     connector . setProperty ( "compressableMimeType" , "text/html,text/xml,text/plain,application/json,application/xml" ) ;

   }

}

Tomcat 本身可配置 server.xml 中的 connector 自动实现对响应数据的压缩,在 Apache Tomcat 10 Configuration Reference - The HTTP Connector 一章中查找 compression 就能找到下面这几个属性

  • compression: off|on|force (默认为 off)
  • compressibleMimeType: text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json,application/xml
  • compressionMinSize: 2048
  • noCompressionUserAgents: 默认为空,可使用正则表达式

HTTP/2 connector 也继承了以上几个属性配置

Apache HTTP Server 的压缩模块

如果部署时在应用服务器(如  Tomcat) 前端配置了 Apache HTTP Server 的话,可以由 Apache 完成对数据的压缩,要使用到的模块是 mod_defalte

比如在 Debian 系的 OS 中 a2enmod deflate, 或在 httpd.conf 中用 LoadModule deflate_module modules/mod_deflate.so 启用。然后在 http.conf 或是应用的 .htaccess 文件中

AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
......

AddOutputFilterByType DEFLATE text / plain

AddOutputFilterByType DEFLATE text / html

. . . . . .

逐项加入要支持压缩的响应类型

具体使用方式请参照 Apache Module mod_deflate 的文档。

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_3403.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Windows与网络基础-1-2-虚拟机安装Windows10和window server2016

目录 一、虚拟机安装软件和ios镜像文件 二、新建虚拟机 2.1 文件—>新建虚拟机 2.2 选择典型安装 2.3 选择稍后安装 2.4 选择操作系统类型和版本 2.5 虚拟机名称和安装路径 2.6 指定磁盘大小 2.7 配置硬件信息 2.8 进入系统安装界面 2.9 选择系统版本 2.10 选择自…

LeetCode精选200道--二叉树篇(二)

二叉树篇&#xff08;二&#xff09;前言完全二叉树的节点个数普通二叉树逻辑递归完全二叉树逻辑平衡二叉树题外话递归二叉树的所有路径思路递归相同的树100. 相同的树另一棵树的子树左叶子之和思路找树左下角的值思路112. 路径总和思路106. 从中序与后序遍历序列构造二叉树根据…

FLASH:一种高效的Transformer设计

背景 近年来&#xff0c;Transformer凭借其优秀的设计&#xff0c;在文本、图像、语音等方向大杀四方。但是由于其attention的二次复杂度限制了其在长序列上的应用。本文提出了一种快(速度快)、省(省显存)的模型FLASH(Fast Linear Attention with a Single Head)&#xff0c;在…

SpringBoot 和 Vue前后端分离在线工具项目实战,源码+超详细讲解

一、前言 主要通过SpringBoot和Vue来实现一个前后端分离的在线工具平台&#xff0c;包含PDF转换、图片处理、文本处理、图表展示、二维码工具等功能。 为了更直观展示项目效果&#xff0c;也给大家提供了在线体验地址&#xff1a;http://49.234.28.149, 源码资源见文末。 通过…

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): 无效的绑定语句(未找到),就是写的sql 方法找不到sql。解决: 1 namespace 指向是否正确 路径与引用的方法的路径保持一致a.namespace 没有指向Dao b. id ,方法名没有对应上2 引用的方法…

记录Kettle连不上mysql8

如图所示&#xff0c;mysql升级到8了。 在很早之前&#xff0c;我一直用的是Mysql 5的驱动包去连接数据库&#xff0c;今天发现突然连接不上了&#xff0c;想了一下&#xff0c;应该是我以前升级mysql后的原因&#xff0c;换了mysql8的驱动后依旧没个卵用。 报错如下&#xff…

远程Debug远端服务器JVM配置

远程调试非本机的Java进程 远端Java进程启动的JVM参数 注意&#xff1a;以下配置尽量不要在线上生产环境开启&#xff0c;或者 JDK4: -Xdebug -Xrunjdwp:transportdt_socket,servery,suspendn,address{port} JDK5-JDK8: -agentlib:jdwptransportdt_socket,servery,suspen…

Python——LeetCode刷题——【383. 赎金信】

题目描述&#xff1a; 解题思路&#xff1a; 用字典记录字符串magazine中每个字符出现的次数。然后看看字典中magazine的各个字符的出现次数是否“够”字符串ransomNote中各个字符出现的次数。如果够&#xff0c;return True。如果存在有点字符不够&#xff0c;return False。…

学习:Python进阶 冒泡排序

#原理 列表每两个相邻的数,如果前面的数比后面的数大,则交换这两个数 一趟排序完成后,则无序曲减少一个数,有序区增加一个数 每循环一趟,从无序区冒出来一个最大的数,放入有序区,最终得到一个升序的列表

认真研究ConcurrentHashMap中的元素统计策略

这里我们想研究的是jdk1.8中ConcurrentHashMap的addCount(long x, int check)方法。如下所示在put方法的最后会触发addCount(long x, int check)方法进行元素个数的统计。 我们再回顾一下另一个参数binCount &#xff1a; 在操作链表的分支if (fh > 0)中 用于统计put前链表…

TinyRenderer学习笔记--Lesson 3、4

Lesson 3 zbuffer 无论怎样&#xff0c;生活中的显示器基本上都是平面&#xff0c;是一个2D的场景&#xff0c;而我们的模型却是3D的&#xff0c;是有深度的&#xff0c;实际上我们看见的都只是离我们的眼睛最近的那一个平面&#xff0c;一个不透明的3D物体的内部和背面是我们…

河北稳控科技使用标准信号检测 VM振弦采集模块测量精度

河北稳控科技使用标准信号检测 VM振弦采集模块测量精度(一) (1)电源1.1VDD 引脚电源必须使用 LDO 稳压或者低纹波线性电源, LDO 推荐使用 AM1117_3.3V 芯片,测试时发现 SPX 生产的 LDO会造成非常严重的干扰(其它品牌应该也会有类似的问题)。1.2VSEN 引脚电源单通道模块…

阿里、滴滴、华为等一线互联网分布式消息中间件:RocketMQ核心笔记

本篇介绍了RocketMQ的基本使用方法及其各个组件的基本原理&#xff0c;讲解原理时&#xff0c;都是采用先整体架构后详细分解的方式。详细分解时不会深入源码逐段讲&#xff0c;而是从代码结构出发梳理整个运行过程。 这份RocketMQ分布式消息中间件—核心原理与最佳实践的完整…

Android Studio应用基础,手把手教你从入门到精通(小白学习)总结2 之 常用界面布局和ListView

总结1链接&#xff1a; (156条消息) Android Studio应用基础&#xff0c;手把手教你从入门到精通&#xff08;小白学习&#xff09;总结1_好喜欢吃红柚子的博客-CSDN博客 学习视频链接&#xff1a; &#xff08;学完必会&#xff09;Android studio基础&#xff0c;从入门到…

尚好房 07_前端房源展示

尚好房&#xff1a;前端房源展示 一、分页显示房源列表 1、效果 2、项目搭建 2.1 创建项目 在web项目中创建子工程web-front 2.2 pom.xml <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0&…

stm32学习(二)|ADC电压采集DMA

利用ADC通道采集外部传感器数值,ADC通道选择依据实际查询芯片手册可得,相关配置利用Cubemx完成。 ADC参数配置首先选择需要使用的ADC通道,并设置对应的引脚ADC_IN0X.ADC参数设置(Paremeter setting)Mode : Independent mode,只使用一个ADC通道 Clock Prescaler,Resolut…

OpenGL 反色

目录 一.OpenGL 反色 1.IOS Object-C 版本2.Windows OpenGL ES 版本3.Windows OpenGL 版本 二.OpenGL 反色 GLSL Shader三.猜你喜欢 零基础 OpenGL ES 学习路线推荐 : OpenGL ES 学习目录 >> OpenGL ES 基础 零基础 OpenGL ES 学习路线推荐 : OpenGL ES 学习目录 >&…

Windows OpenGL ES 图像反色

目录 一.OpenGL ES 图像反色 1.原始图片2.效果演示 二.OpenGL ES 图像反色源码下载三.猜你喜欢 零基础 OpenGL ES 学习路线推荐 : OpenGL ES 学习目录 >> OpenGL ES 基础 零基础 OpenGL ES 学习路线推荐 : OpenGL ES 学习目录 >> OpenGL ES 特效 零基础 OpenGL E…

责任链模式

1、责任链模式是什么 行为模式&#xff0c;一个对象产生的消息会被另外的对象处理。对象发出消息后&#xff0c;不管被哪种、多少个其他对象收到和处理消息。【客户端和handler解耦】 2、为什么使用 如果不使用责任链&#xff0c;则client要知道有多少个handler、什么情况调…

2.IP子网划分

IP子网划分地址分类网络位与主机位一个网段可以容纳多少IPIP地址&#xff1a;互联网中计算机的‘身份证号’&#xff0c;唯一标识一台网络设备的身份ID NAT技术&#xff1a;网络地址转换&#xff0c;节约公网IP 例: IP地址 192.168.1.1 192.168.1 …