[英]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.