SpringBoot啟用GZIP壓縮的代碼工程
1.為什么是需要gzip壓縮?
經(jīng)常我們都會(huì)與服務(wù)端進(jìn)行大數(shù)據(jù)量的文本傳輸,例如 JSON 就是常見(jiàn)的一種格式。通過(guò) REST API 接口進(jìn)行 GET 和 POST 請(qǐng)求,可能會(huì)有大量的文本格式數(shù)據(jù)提交、返回。然后對(duì)于文本,它有很高的壓縮率,如果在 GET/POST 請(qǐng)求時(shí)候?qū)ξ谋具M(jìn)行壓縮會(huì)節(jié)省大量的網(wǎng)絡(luò)帶寬,減少網(wǎng)絡(luò)時(shí)延。 HTTP 協(xié)議在相應(yīng)部分支持 Content-Encoding: gzip ,瀏覽器請(qǐng)求時(shí)帶上 Accept-Encoding: gzip 即可,服務(wù)端對(duì)返回的 response body 進(jìn)行壓縮,并在 response 頭帶上 Content-Encoding: gzip,瀏覽器會(huì)自動(dòng)解析。 然而 HTTP 沒(méi)有壓縮 request body 的設(shè)計(jì),因?yàn)樵诳蛻?hù)端發(fā)起請(qǐng)求時(shí)并不知道服務(wù)器是否支持壓縮。因此沒(méi)法通過(guò) HTTP 協(xié)議來(lái)解決,只能在服務(wù)端做一些過(guò)濾器進(jìn)行判斷,人為約束。壓縮和解壓在提升網(wǎng)絡(luò)帶寬的同時(shí),會(huì)帶來(lái) CPU 資源的損耗。
2.代碼工程
實(shí)驗(yàn)?zāi)康?/h3>
對(duì)返回的json啟用gzip壓縮
對(duì)返回的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
請(qǐng)求和響應(yīng)都是同一個(gè)user對(duì)象,方便后面測(cè)試對(duì)比它們的大小
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)鍵代碼,所有代碼請(qǐng)參見(jiàn)下面代碼倉(cāng)庫(kù)
代碼倉(cāng)庫(kù)
3.測(cè)試
用postman請(qǐng)求http://127.0.0.1:8088/hello

可以看到 request body 285B,respnse body 64B ,將近5倍的差距。
以上就是SpringBoot啟用GZIP壓縮的代碼工程的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot啟用GZIP壓縮的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring AOP方法內(nèi)部調(diào)用不生效的解決方案
最近有個(gè)需求,統(tǒng)計(jì)某個(gè)方法的調(diào)用次數(shù),開(kāi)始使用 Spring AOP 實(shí)現(xiàn),后來(lái)發(fā)現(xiàn)當(dāng)方法被內(nèi)部調(diào)用時(shí),切面邏輯將不會(huì)生效,所以本文就給大家介紹了Spring AOP方法內(nèi)部調(diào)用不生效的解決方案,需要的朋友可以參考下2025-01-01
javaweb圖書(shū)商城設(shè)計(jì)之購(gòu)物車(chē)模塊(3)
這篇文章主要為大家詳細(xì)介紹了javaweb圖書(shū)商城設(shè)計(jì)之購(gòu)物車(chē)模塊的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Java并發(fā)之原子性 有序性 可見(jiàn)性及Happen Before原則
一提到happens-before原則,就讓人有點(diǎn)“丈二和尚摸不著頭腦”。這個(gè)涵蓋了整個(gè)JMM中可見(jiàn)性原則的規(guī)則,究竟如何理解,把我個(gè)人一些理解記錄下來(lái)。下面可以和小編一起學(xué)習(xí)Java 并發(fā)四個(gè)原則2021-09-09
Java簡(jiǎn)單實(shí)現(xiàn)對(duì)一串?dāng)?shù)字采用相應(yīng)的加密策略后傳輸
下面小編就為大家?guī)?lái)一篇Java簡(jiǎn)單實(shí)現(xiàn)對(duì)一串?dāng)?shù)字采用相應(yīng)的加密策略后傳輸。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09

