OpenFeign / SpringBoot 响应使用gzip压缩(含例子)

一.压缩相关配置

全局压缩(接口与浏览器响应压缩)

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 均包含默认值,可以不手动指定,如有其他需要可按需指定

server:
  port: 8081
  compression:
    enabled: true

局部压缩(微服务之间利用feign请求及响应压缩)

feign:
  compression:
    request:
      enabled: true //开启请求压缩
      mime-types: text/xml,application/xml,application/json
      min-request-size: 1024 //设置请求大小,1024kb以上开始压缩
    response:
      enabled: true //响应压缩
      useGzipDecoder: true //响应解码

二.例子

2.1准备product (测试全局压缩)

启动类添加注解@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();
}

实现类(制造测试数据

@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,对应接口中的Content-Encoding中会有显示";
        for (int i = 0; i < 100; i++) {
            FeignTestVo testVo = new FeignTestVo();
            testVo.setName(str);
            testVo.setSex(1);
            arrayList.add(testVo);
        }
        return Result.success(arrayList);
    }

}

为方便查看日志,创建FeignConfig配置类(需要在@FeignClient注解内configuration 指定,configuration = FeignConfig.class

@Slf4j
@Configuration
public class FeignConfig {

    /**
     * feign 日志记录级别
     * NONE:无日志记录(默认)
     * BASIC:只记录请求方法和 url 以及响应状态代码和执行时间。
     * HEADERS:记录请求和响应头的基本信息。
     * FULL:记录请求和响应的头、正文和元数据。
     *
     * @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

测试压缩效果:

  compression:
    enabled: false

浏览器访问http://127.0.0.1:8081/feignTest

目前响应大小size为20.9KB

查看请求头信息

Accept-Encoding:

表示Http响应是否进行压缩,一般的浏览器在访问网页时,是默认在请求头中加入

Accept-Encoding: gzip, deflate, br 表示这个请求的内容希望被压缩,压缩的目的是为了减少网络流量。

但是这个只是协议,只能是要求而不是强制的,如果服务器不支持压缩或者没有开启压缩,则不能起到作用。

如果服务器也是支持压缩或者开启压缩,则会在响应头中加入Content-Encoding: gzip 头部。

查看响应头信息(发现暂未出现Content-Encoding: gzip, 表示此次请求未进行压缩

打开压缩

compression:
  enabled: true

再次请求

http://127.0.0.1:8081/feignTest

此时大小已经发生变化 为698B

再次查看响应头信息( 出现 Content-Encoding: gzip 标识, 此次请求响应已被压缩,大小发生变化

2.2准备consumer (测试微服务之间通过feign调用压缩)

刚刚的服务不变,新增项目

注入product 项目FeignServer 接口类

@Service
public class LinBroker {

    @Resource
    private FeignServer feignServer;

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

新建测试类(注入LinBroker类 调用方法)

@RestController
public class UserController {

    @Resource
    private LinBroker linBroker;

    @ApiOperation("测试")
    @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

首先关闭响应压缩配置文件如下

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 调用 consumer 项目 test方法

查看控制台feign日志

查看到通过feign 调用 feignTest方法日志

未包含 content-encoding: gzip , 且 END HTTP (20665-byte body) 响应大小未发生变化 为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

再次请求

包含 content-encoding: gzip ,

且 END HTTP (383-byte body) 响应大小发生变化 为383 byte

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值