Feign 的請求壓縮

今天看到有關於Feign的請求壓縮,這裏記錄一下...html

首先Feign 是經過 http 調用的,那麼就牽扯到一個數據大小的問題。若是不通過壓縮就發送請求、獲取響應,那麼會由於流量過大致使浪費流量,這時就須要使用數據壓縮,將大流量壓縮成小流量。java

Feign GZIP 壓縮

源碼:https://gitee.com/laiyy0728/spring-cloud/tree/master/spring-cloud-feign/spring-cloud-feign-gzipgit

Feign的請求壓縮配置:spring

feign:
  compression:
    request:
      enabled: true
      mime-type: text/html,application/xml,application/json
      min-request-size: 2048
    response:
      enabled: true

# 開啓日誌
logging:
  level:
    com.laiyy.gitee.feign.springcloudfeigngzip.feign.GiteeFeignClient: debug

因爲使用 gzip 壓縮,壓縮後的數據是二進制,那麼在獲取 Response 的時候,就不能和以前同樣直接使用 String 來接收了,須要使用 ResponseEntity<byte[]> 接收json

@FeignClient(name = "xxx-service", url = "https://www.xxx.com/", configuration = GiteeFeignConfiguration.class)
public interface GiteeFeignClient {

    @RequestMapping(value = "/search", method = RequestMethod.GET)
    ResponseEntity<byte[]> searchRepo(@RequestParam("q") String query);

}

對應的Controller 也須要改成 ResponseEntity<byte[]>app

@GetMapping(value = "feign-gitee")
public ResponseEntity<byte[]> feign(String query){
    return giteeFeignClient.searchRepo(query);
}

更具體地可參考博客: https://www.jianshu.com/p/e29d7f6be6e3url

相關文章
相關標籤/搜索