SpringBoot啟用GZIP壓縮的代碼工程
1.為什么是需要gzip壓縮?
經(jīng)常我們都會與服務(wù)端進行大數(shù)據(jù)量的文本傳輸,例如 JSON 就是常見的一種格式。通過 REST API 接口進行 GET 和 POST 請求,可能會有大量的文本格式數(shù)據(jù)提交、返回。然后對于文本,它有很高的壓縮率,如果在 GET/POST 請求時候?qū)ξ谋具M行壓縮會節(jié)省大量的網(wǎng)絡(luò)帶寬,減少網(wǎng)絡(luò)時延。 HTTP 協(xié)議在相應(yīng)部分支持 Content-Encoding: gzip ,瀏覽器請求時帶上 Accept-Encoding: gzip 即可,服務(wù)端對返回的 response body 進行壓縮,并在 response 頭帶上 Content-Encoding: gzip,瀏覽器會自動解析。 然而 HTTP 沒有壓縮 request body 的設(shè)計,因為在客戶端發(fā)起請求時并不知道服務(wù)器是否支持壓縮。因此沒法通過 HTTP 協(xié)議來解決,只能在服務(wù)端做一些過濾器進行判斷,人為約束。壓縮和解壓在提升網(wǎng)絡(luò)帶寬的同時,會帶來 CPU 資源的損耗。
2.代碼工程
實驗?zāi)康?/h3>
對返回的json啟用gzip壓縮
對返回的json啟用gzip壓縮
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-demo</artifactId>
<groupId>com.et</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>gzip</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
</project>
controller
請求和響應(yīng)都是同一個user對象,方便后面測試對比它們的大小
package com.et.gzip.controller;
import com.et.gzip.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@Slf4j
public class HelloWorldController {
@PostMapping("/hello")
public User showHelloWorld(@RequestBody User user){
log.info("user:"+ user);
return user;
}
}
application.yaml
server:
port: 8088
compression:
enabled: true
mime-types: application/json,application/xml,text/html,text/plain,text/css,application/x-javascript
以上只是一些關(guān)鍵代碼,所有代碼請參見下面代碼倉庫
代碼倉庫
3.測試
用postman請求http://127.0.0.1:8088/hello

可以看到 request body 285B,respnse body 64B ,將近5倍的差距。
以上就是SpringBoot啟用GZIP壓縮的代碼工程的詳細內(nèi)容,更多關(guān)于SpringBoot啟用GZIP壓縮的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java技巧分享之利用RxJava打造可觀測數(shù)據(jù)RxLiveData
這篇文章主要來和大家分享一個Java技巧,那就是利用RxJava打造可觀測數(shù)據(jù)RxLiveData,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2023-06-06

