一.壓縮相關(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-size 與 mime-types 均包含默認(rèn)值,可以不手動(dòng)指定,如有其他需要可按需指定
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è)試全局壓縮)
啟動(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
目前響應(yīng)大小size為20.9KB
查看請(qǐng)求頭信息
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)行壓縮)
打開壓縮
compression:
enabled: true
再次請(qǐng)求
http://127.0.0.1:8081/feignTest
此時(shí)大小已經(jīng)發(fā)生變化 為698B
再次查看響應(yīng)頭信息( 出現(xiàn) Content-Encoding: gzip 標(biāo)識(shí), 此次請(qǐng)求響應(yīng)已被壓縮,大小發(fā)生變化)
2.2準(zhǔn)備consumer (測(cè)試微服務(wù)之間通過feign調(diào)用壓縮)
剛剛的服務(wù)不變,新增項(xiàng)目
注入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方法
查看控制臺(tái)feign日志
查看到通過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)求
包含 content-encoding: gzip ,文章來源:http://www.zghlxwxcb.cn/news/detail-412804.html
且 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)!