SpringBoot啟用GZIP壓縮的代碼工程
1.為什么是需要gzip壓縮?
經(jīng)常我們都會(huì)與服務(wù)端進(jìn)行大數(shù)據(jù)量的文本傳輸,例如 JSON 就是常見的一種格式。通過 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 沒有壓縮 request body 的設(shè)計(jì),因?yàn)樵诳蛻舳税l(fā)起請(qǐng)求時(shí)并不知道服務(wù)器是否支持壓縮。因此沒法通過 HTTP 協(xié)議來解決,只能在服務(wù)端做一些過濾器進(jìn)行判斷,人為約束。壓縮和解壓在提升網(wǎng)絡(luò)帶寬的同時(shí),會(huì)帶來 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)參見下面代碼倉庫
代碼倉庫
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)文章
Java技巧分享之利用RxJava打造可觀測(cè)數(shù)據(jù)RxLiveData
這篇文章主要來和大家分享一個(gè)Java技巧,那就是利用RxJava打造可觀測(cè)數(shù)據(jù)RxLiveData,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-06-06
java實(shí)現(xiàn)波雷費(fèi)密碼算法示例代碼
這篇文章主要介紹了java實(shí)現(xiàn)波雷費(fèi)密碼算法示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
Java中字節(jié)流和字符流的理解(超精簡(jiǎn)!)
Java通過稱為流的抽象來執(zhí)行I/O操作,下面這篇文章主要給大家介紹了關(guān)于Java中字節(jié)流和字符流理解,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
Java如何判斷一個(gè)字符串是否包含某個(gè)字符串
這篇文章主要給大家介紹了關(guān)于Java如何判斷一個(gè)字符串是否包含某個(gè)字符串的相關(guān)資料,在實(shí)際編程中,經(jīng)常需要判斷一個(gè)字符串中是否包含某個(gè)子串,需要的朋友可以參考下2023-07-07
創(chuàng)建并運(yùn)行一個(gè)java線程方法介紹
這篇文章主要介紹了創(chuàng)建并運(yùn)行一個(gè)java線程,涉及線程代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11

