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

相关文章
相关标签/搜索