Springcloud中feign是支持对请求与响应进行GZIP压缩的,以此来减少通信中的性能损耗.
但是在spring-cloud-openfeign-core.jar文件中,默认对请求和相应压缩是禁用的。
在org.springframework.cloud.openfeign.encoding. FeignAcceptGzipEncodingAutoConfiguration类中可以看到响应压缩是禁用的,
@Configuration
@EnableConfigurationProperties({FeignClientEncodingProperties.class})
@ConditionalOnClass({Feign.class})
@ConditionalOnBean({Client.class})
@ConditionalOnProperty(
value = {"feign.compression.response.enabled"},
matchIfMissing = false
)
@ConditionalOnMissingBean(
type = {"okhttp3.OkHttpClient"}
)
org.springframework.cloud.openfeign.encoding;.FeignContentGzipEncodingAutoConfiguration类也是
@ConditionalOnProperty(
value = {"feign.compression.response.enabled"},
matchIfMissing = false
)
@ConditionalOnMissingBean(
type = {"okhttp3.OkHttpClient"}
)
@AutoConfigureAfter({FeignAutoConfiguration.class})
public class FeignAcceptGzipEncodingAutoConfiguration {
}
而在 org.springframework.cloud.openfeign.encoding.FeignClientEncodingProperties中配置了一些默认的编码属性,包括待压缩的文件格式,及启用压缩功能的最小文件大小.
所以我们可以在feign项目中修改applicatoin.yml配置开启压缩
feign:
compression:
request:
enabled: true
mime-types: # 可以被压缩的类型
- text/xml
- application/xml
- application/json
min-request-size: 2048 # 超过2048的字节进行压缩