![](/img/trans.png)
[英]Not able to run SpringBoot 1.5.10.RELEASE on local tomcat(spring boot embedded) server
[英]Gzip compression not working in my project with Spring boot 1.5.10.RELEASE
我正在使用 Spring Boot 1.5.10.RELEASE 版本。 Gzip 压缩不起作用。
http://localhost:9000 --> http://localhost:8080/api/..
Angularjs & rest api 在不同的端口上。 启用 CrossOrigin 以接受来自 angularjs ui 的请求。
使用嵌入式 tomcat 服务器部署 spring 启动应用程序。 不使用 http2 属性,即server.http2.enabled=true
Angualrjs 调用 rest api。 以下是 $http 服务
$http({
method: method,
url: url,
params: params,
data: body,
headers: {
Authorization: token,
"Content-type": 'application/json'
}
});
Rest api 响应大小大约 25 MB,所以我想压缩响应。
我在application.properties中添加了众所周知的属性来应用 gzip 压缩。 Spring boot 1.5.10 支持的属性
# Enable response compression
server.compression.enabled=true
# The comma-separated list of mime types that should be compressed
server.compression.mime-types=text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json
# Compress the response only if the response size is at least 1KB
server.compression.min-response-size=1024
我观察到网络选项卡并没有观察到内容编码:响应 header 的 gzip 。
要求
Request URL: http://localhost:9081/employee
Request Method: GET
Status Code: 200
Remote Address: [::1]:9081
Referrer Policy: no-referrer-when-downgrade
响应 Header
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://localhost:7000
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Type: application/json;charset=UTF-8
Date: Sun, 28 Jun 2020 18:15:17 GMT
Expires: 0
Pragma: no-cache
Set-Cookie: JSESSIONID=6E7C07874D0329E18A0C07E5E303F005; Path=/; HttpOnly
Transfer-Encoding: chunked
Vary: Origin
X-Application-Context: application
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
请求 Header
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Authorization: Bearer eyJhbGciOiJIUzINiJ9.eyJyb2xlIjiU0VDVE9SSEVBRCIsImxldmVsRG93biI6IkVENzA0MTI7TU04MzcyNDtKTDgzNTwO0RNNDAwNzE7Skc3MzA0NjtFQzM0NjEzO05OMTY5Nzk7QUs2MDYzNztTVDE4NTg4O0FTMjczNTE7Q0I4MTg3OTtWQTc4MTk5O0NNOTM3MDA7QVkyMzYzNztKUzcwMDY4O0NCMTc2NzE7TksyMTU2MDtMUzg4OTg0O0FQNTg3MDg7VFcyjk0NTtKSzI1Nzc3O01TNDk5MjE7SkI4OTcyOTtNSDAyMTI3O01CMTUwODk7SU0xMjgwODtNQzcxOTc2O1JSMjAzMDI7TFM1ODk4MiIsImxldmVsVXAiOm51bGwsImRlbGVnYXRlZCI6bnVsbCwic29lSWQiOiJTUjQ0MTg1I0.*************
Cache-Control: no-cache
Connection: keep-alive
Content-type: application/json
Host: localhost:9081
Origin: http://localhost:7000
Pragma: no-cache
Referer: http://localhost:7000/build/standalone.html
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36
我没有得到响应 header 的预期标题
Content-Encoding: gzip
Vary: Accept-Encoding
客户端/服务器端需要进行任何更改吗?
---[Edit-1] ------------- 在单个项目中尝试了 gzip,但在我的项目中不起作用。
以下是从浏览器调用 rest api 时的响应 header
Content-Encoding: gzip
Content-Type: application/json;charset=UTF-8
Date: Sun, 28 Jun 2020 18:12:29 GMT
Transfer-Encoding: chunked
Vary: Accept-Encoding
-----[Edit-2]-----Ziplet----
使用ziplet依赖我能够压缩响应,但我想使用 spring 引导 gzip 压缩。
响应 header - 使用 Ziplet 时
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://localhost:7000
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Encoding: gzip
Content-Type: application/json;charset=UTF-8
Date: Mon, 06 Jul 2020 18:31:07 GMT
Expires: 0
Pragma: no-cache
Set-Cookie: JSESSIONID=8465D2E81A1A9CE146255B6C545FBE30; Path=/; HttpOnly
Transfer-Encoding: chunked
Vary: Accept-Encoding
Vary: Origin
X-Application-Context: application
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
当使用 Spring 启动 gzip 压缩时,没有观察到任何我可以假设启用 gzip 压缩的地方。
使用嵌入的 tomcat 调试 spring 引导 gzip 压缩的任何属性? 像 logging.level.org.eclipse.jetty.server.handler.gzip=TRACE
我如何验证 server.compression.enabled?
我的项目的客户端/服务器端需要进行任何更改吗?
提前致谢。
作为您的问题,您使用的是 Spring Boot 1.5.10.RELEASE。
对于该版本的框架,class TomcatEmbeddedServletContainerFactory
负责启动嵌入式 Tomcat 容器。
class 的源代码和版本可以在这里找到:
在这个 class 中,您可以找到方法customizeCompression
:
private void customizeCompression(Connector connector) {
ProtocolHandler handler = connector.getProtocolHandler();
if (handler instanceof AbstractHttp11Protocol) {
AbstractHttp11Protocol<?> protocol = (AbstractHttp11Protocol<?>) handler;
Compression compression = getCompression();
protocol.setCompression("on");
protocol.setCompressionMinSize(compression.getMinResponseSize());
configureCompressibleMimeTypes(protocol, compression);
if (getCompression().getExcludedUserAgents() != null) {
protocol.setNoCompressionUserAgents(
StringUtils.arrayToCommaDelimitedString(
getCompression().getExcludedUserAgents()));
}
}
}
尝试在此方法上设置断点并调试您的应用程序以查看是否实际启用了压缩。
如果没有,很可能是您的项目中存在某种错误配置。
如果它有效,则表明您的配置是正确的并且问题不同。
如果是这种情况,在查看其他任何内容之前,最好尝试另一台服务器,例如 Jetty 或 Undertow,它们也支持此压缩功能,并查看那里是否一切正常。
例如,要配置 Undertow 而不是 Tomcat,您可以使用以下命令:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
您也可以尝试以编程方式配置 Tomcat 压缩; 在这个 stackoverflow 问题中查看 matsev 的回答:
您必须在 spring 引导属性中启用 http2 支持
server.http2.enabled=true
然后尝试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.