国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

OpenFeign / SpringBoot 響應(yīng)使用gzip壓縮(含例子)

這篇具有很好參考價(jià)值的文章主要介紹了OpenFeign / SpringBoot 響應(yīng)使用gzip壓縮(含例子)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

一.壓縮相關(guān)配置

全局壓縮(接口與瀏覽器響應(yīng)壓縮)

server:
  compression:
    enabled: true
    min-response-size: 1
    mime-types:
      - image/png
      - image/jpeg
      - image/jpg
      - text/html
      - application/octet-stream
      - application/json

min-response-sizemime-types 均包含默認(rèn)值,可以不手動(dòng)指定,如有其他需要可按需指定

OpenFeign / SpringBoot 響應(yīng)使用gzip壓縮(含例子)

server:
  port: 8081
  compression:
    enabled: true

局部壓縮(微服務(wù)之間利用feign請(qǐng)求及響應(yīng)壓縮)

feign:
  compression:
    request:
      enabled: true //開啟請(qǐng)求壓縮
      mime-types: text/xml,application/xml,application/json
      min-request-size: 1024 //設(shè)置請(qǐng)求大小,1024kb以上開始?jí)嚎s
    response:
      enabled: true //響應(yīng)壓縮
      useGzipDecoder: true //響應(yīng)解碼

二.例子

2.1準(zhǔn)備product (測(cè)試全局壓縮)

OpenFeign / SpringBoot 響應(yīng)使用gzip壓縮(含例子)

啟動(dòng)類添加注解@EnableFeignClients 指定目錄

//@EnableRetry
@EnableAsync
@SpringBootApplication
@EnableFeignClients(basePackages = {"com.joker.cloud.linserver.server"})
@MapperScan({"com.joker.cloud.linserver.mapper","com.joker.cloud.linserver.conf"})
public class LinServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(LinServerApplication.class, args);
    }
}

接口類添加注解@FeignClient

@FeignClient(name = "cloud-lin", url = "http://localhost:8081/", fallback = FeignFallBack.class , contextId = "FeignServer", configuration = FeignConfig.class)
public interface FeignServer {

    @GetMapping("/feignTest")
    @ApiOperation("feignTest")
    Result<List<FeignTestVo>> feignTest();
}

實(shí)現(xiàn)類(制造測(cè)試數(shù)據(jù)

@Slf4j
@RestController
@CrossOrigin
@Api(tags = "用戶詳情")
public class FeignController implements FeignServer {

    @Override
    public Result<List<FeignTestVo>> feignTest() {
        ArrayList<FeignTestVo> arrayList = new ArrayList<>();
        String str = "Google 瀏覽器打開 F12,切換到 NetWork 下,右鍵表頭選擇 Response Headers 下的Content-Encoding,如果開啟了 Gzip,對(duì)應(yīng)接口中的Content-Encoding中會(huì)有顯示";
        for (int i = 0; i < 100; i++) {
            FeignTestVo testVo = new FeignTestVo();
            testVo.setName(str);
            testVo.setSex(1);
            arrayList.add(testVo);
        }
        return Result.success(arrayList);
    }

}

為方便查看日志,創(chuàng)建FeignConfig配置類(需要在@FeignClient注解內(nèi)configuration 指定,configuration = FeignConfig.class

@Slf4j
@Configuration
public class FeignConfig {

    /**
     * feign 日志記錄級(jí)別
     * NONE:無日志記錄(默認(rèn))
     * BASIC:只記錄請(qǐng)求方法和 url 以及響應(yīng)狀態(tài)代碼和執(zhí)行時(shí)間。
     * HEADERS:記錄請(qǐng)求和響應(yīng)頭的基本信息。
     * FULL:記錄請(qǐng)求和響應(yīng)的頭、正文和元數(shù)據(jù)。
     *
     * @return Logger.Level
     */
    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

}

application 配置類

server:
  port: 8081
  compression:
    enabled: true
spring:
  application:
    name: joker-cloud-lin
logging:
  level:
    com.joker.cloud.linserver.server.FeignServer: debug

測(cè)試壓縮效果:

  compression:
    enabled: false

瀏覽器訪問http://127.0.0.1:8081/feignTest

OpenFeign / SpringBoot 響應(yīng)使用gzip壓縮(含例子)

目前響應(yīng)大小size為20.9KB

查看請(qǐng)求頭信息

OpenFeign / SpringBoot 響應(yīng)使用gzip壓縮(含例子)

Accept-Encoding:

表示Http響應(yīng)是否進(jìn)行壓縮,一般的瀏覽器在訪問網(wǎng)頁時(shí),是默認(rèn)在請(qǐng)求頭中加入

Accept-Encoding: gzip, deflate, br 表示這個(gè)請(qǐng)求的內(nèi)容希望被壓縮,壓縮的目的是為了減少網(wǎng)絡(luò)流量。

但是這個(gè)只是協(xié)議,只能是要求而不是強(qiáng)制的,如果服務(wù)器不支持壓縮或者沒有開啟壓縮,則不能起到作用。

如果服務(wù)器也是支持壓縮或者開啟壓縮,則會(huì)在響應(yīng)頭中加入Content-Encoding: gzip 頭部。

查看響應(yīng)頭信息(發(fā)現(xiàn)暫未出現(xiàn)Content-Encoding: gzip, 表示此次請(qǐng)求未進(jìn)行壓縮

OpenFeign / SpringBoot 響應(yīng)使用gzip壓縮(含例子)

打開壓縮

compression:
  enabled: true

再次請(qǐng)求

http://127.0.0.1:8081/feignTest

OpenFeign / SpringBoot 響應(yīng)使用gzip壓縮(含例子)

此時(shí)大小已經(jīng)發(fā)生變化 為698B

再次查看響應(yīng)頭信息( 出現(xiàn) Content-Encoding: gzip 標(biāo)識(shí), 此次請(qǐng)求響應(yīng)已被壓縮,大小發(fā)生變化

OpenFeign / SpringBoot 響應(yīng)使用gzip壓縮(含例子)

2.2準(zhǔn)備consumer (測(cè)試微服務(wù)之間通過feign調(diào)用壓縮)

剛剛的服務(wù)不變,新增項(xiàng)目

OpenFeign / SpringBoot 響應(yīng)使用gzip壓縮(含例子)

注入product 項(xiàng)目FeignServer 接口類

@Service
public class LinBroker {

    @Resource
    private FeignServer feignServer;

    public List<FeignTestVo> feignTest(){
        List<FeignTestVo> data = feignServer.feignTest().getData();
        return data;
    }
}

新建測(cè)試類(注入LinBroker類 調(diào)用方法)

@RestController
public class UserController {

    @Resource
    private LinBroker linBroker;

    @ApiOperation("測(cè)試")
    @GetMapping("/test")
    public Result<List<FeignTestVo>> test(){
        List<FeignTestVo> vos = linBroker.feignTest();
        return Result.success(vos);
    }
}

配置文件 application.yml

server:
  port: 8082
  compression:
      enabled: true
feign:
  compression:
    request:
      enabled: true
      mime-types: text/xml,application/xml,application/json
      min-request-size: 1024
    response:
      enabled: true
      useGzipDecoder: true

logging:
  level:
    com.joker.cloud.linserver.server.FeignServer: debug

首先關(guān)閉響應(yīng)壓縮配置文件如下

server:
  port: 8082
  compression:
      enabled: true
feign:
  compression:
    request:
      enabled: true
      mime-types: text/xml,application/xml,application/json
      min-request-size: 1024
    response:
      enabled: false
      useGzipDecoder: true

logging:
  level:
    com.joker.cloud.linserver.server.FeignServer: debug

使用postman 調(diào)用 consumer 項(xiàng)目 test方法

OpenFeign / SpringBoot 響應(yīng)使用gzip壓縮(含例子)

查看控制臺(tái)feign日志

OpenFeign / SpringBoot 響應(yīng)使用gzip壓縮(含例子)

查看到通過feign 調(diào)用 feignTest方法日志

未包含 content-encoding: gzip , 且 END HTTP (20665-byte body) 響應(yīng)大小未發(fā)生變化 為20665 byte

修改配置文件

server:
  port: 8082
  compression:
      enabled: true
feign:
  compression:
    request:
      enabled: true
      mime-types: text/xml,application/xml,application/json
      min-request-size: 1024
    response:
      enabled: true
      useGzipDecoder: true

logging:
  level:
    com.joker.cloud.linserver.server.FeignServer: debug

再次請(qǐng)求

OpenFeign / SpringBoot 響應(yīng)使用gzip壓縮(含例子)

包含 content-encoding: gzip ,

且 END HTTP (383-byte body) 響應(yīng)大小發(fā)生變化 為383 byte文章來源地址http://www.zghlxwxcb.cn/news/detail-412804.html

到了這里,關(guān)于OpenFeign / SpringBoot 響應(yīng)使用gzip壓縮(含例子)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Springboot啟用HTTP響應(yīng)壓縮

    Springboot啟用HTTP響應(yīng)壓縮

    官方文檔:https://docs.spring.io/spring-boot/docs/2.3.12.RELEASE/reference/htmlsingle/#how-to-enable-http-response-compression

    2024年02月13日
    瀏覽(15)
  • Linux 壓縮、解壓文件的 4 種方式。tar、gzip、gunzip、zip、unzip、7z命令使用方法

    Linux 壓縮、解壓文件的方式有如下幾種: tar 是一種常用的打包工具,可以將多個(gè)文件或目錄打包成一個(gè) tar 包,也可以將一個(gè) tar 包解壓縮到指定的目錄。 1.1. 壓縮: 1.2. 解壓: 1.3. tar 命令各參數(shù)含義 tar 命令是 Linux 下常用的打包和壓縮工具,用于將多個(gè)文件或目錄打包成一

    2024年02月10日
    瀏覽(37)
  • 【Linux筆記】壓縮、解壓文件的 4 種方式。tar、gzip、gunzip、zip、unzip、7z命令使用方法

    【Linux筆記】壓縮、解壓文件的 4 種方式。tar、gzip、gunzip、zip、unzip、7z命令使用方法

    目錄 1、使用 tar 命令: 1.1. 壓縮: 1.2. 解壓: 1.3. tar 命令各參數(shù)含義 2. gzip、gunzip gzip 命令: 壓縮文件: 保留原始文件,創(chuàng)建壓縮文件: 保留原始文件,顯示壓縮進(jìn)度: gunzip 命令: 解壓文件: 保留壓縮文件,創(chuàng)建原始文件: 保留壓縮文件,顯示解壓進(jìn)度: 3. zip、unzip

    2024年02月03日
    瀏覽(26)
  • Linux中_使用tar_gzip_zip_rar_命令_打包和解包_壓縮和解壓

    1.3.1、到當(dāng)前目錄下 1.3.2、到指定目錄下 3.1.1、壓縮當(dāng)前目錄: 3.1.2、壓縮指定目錄: 3.2.1、到當(dāng)前目錄 3.2.2、到指定目錄 4.2.1、壓縮文件本身: 4.2.2、遞歸壓縮,將指定目錄下所有文件和子目錄一并壓縮:

    2024年02月04日
    瀏覽(23)
  • 【SpringBoot應(yīng)用篇】Spring Boot 配置HTTP 響應(yīng)內(nèi)容壓縮

    5、默認(rèn)情況下,要執(zhí)行壓縮,響應(yīng)的長(zhǎng)度至少為 2048 字節(jié),可以通過 server.compression.min-response-size 屬性配置。 6、默認(rèn)情況下,僅當(dāng)響應(yīng)的內(nèi)容類型為以下內(nèi)容之一時(shí),才會(huì)對(duì)其進(jìn)行壓縮,可以通過 mime-types 屬性配置:text/html,text/xml,text/plain,text/css,text/javascript,application/javasc

    2024年02月16日
    瀏覽(22)
  • Nginx的Gzip壓縮

    Nginx的Gzip壓縮 ???? Nginx開啟Gzip壓縮功能, 可以使網(wǎng)站的css、js 、xml、html 文件在傳輸時(shí)進(jìn)行壓縮,提高訪問速度, 進(jìn)而優(yōu)化Nginx性能!在Nginx配置文件中可以配置Gzip的使用,相關(guān)指令可以在http區(qū)域 server區(qū)域、location區(qū)域配置。Nginx可以通過ngx_http_gzip_module模塊、ngx_http_gzip_st

    2024年02月05日
    瀏覽(27)
  • Python教程:Gzip解壓縮

    Python教程:Gzip解壓縮

    我們將介紹 Python 中的 gzip 解壓。我們還將介紹如何使用gzip解壓來解壓壓縮的內(nèi)容。 在Python中為壓縮和解壓目的建立了許多庫(kù),但我們將介紹Gzip 庫(kù)。它是一個(gè)流行的數(shù)據(jù)壓縮工具。 我們可以使用gzip ,通過對(duì)數(shù)據(jù)進(jìn)行特殊格式的編碼來減少文件的大小,這種格式不能被人類

    2024年02月11日
    瀏覽(64)
  • Nginx中配置GZIP壓縮詳解

    Nginx中配置GZIP壓縮詳解

    網(wǎng)站訪問速度對(duì)用戶來說是很重要的體驗(yàn),有時(shí)候除了增大帶寬外,還需要對(duì)文件進(jìn)行壓縮。 首先找到Nginx安裝路徑下的配置文件: 保存后,使用nginx -t檢查配置文件是否OK: ? ? ? ? 上述報(bào)錯(cuò)證明nginx在編譯安裝時(shí)候沒有連同http_ssl_module模塊一同編譯;現(xiàn)在的情況是nginx已經(jīng)

    2024年02月04日
    瀏覽(21)
  • Linux壓縮、解壓縮及歸檔工具(tar、zip、gzip...)

    Linux壓縮、解壓縮及歸檔工具(tar、zip、gzip...)

    壓縮、歸檔 解壓 文件后綴 備注 compress uncompress .Z 很老的命令 gzip gunzip .gz bzip2 bunzip2 .bz2 xz unxz .xz zip unzip .zip tar .tar/.tar.gz 常用命令 cpio 用于從歸檔包中存入和讀取文件,copy-in/copy-out ????????gzip [OPTION]...FILE... 生成壓縮文件,刪除原文件 ????????????????-d:解壓縮

    2024年02月12日
    瀏覽(18)
  • 【Linux命令詳解 | gzip命令】 gzip命令用于壓縮文件,可以顯著減小文件大小

    【Linux命令詳解 | gzip命令】 gzip命令用于壓縮文件,可以顯著減小文件大小

    在Linux中, gzip 命令是一款強(qiáng)大的文件壓縮工具,它可以通過壓縮文件的方式顯著減小文件大小,從而節(jié)省存儲(chǔ)空間并加速文件傳輸。無論是在文件備份、數(shù)據(jù)傳輸還是發(fā)布軟件包時(shí), gzip 命令都是一個(gè)非常有用的工具。在本文中,我們將深入探討 gzip 命令的各個(gè)方面,包括

    2024年02月12日
    瀏覽(21)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包