feign 启用压缩二三事

feign启用压缩二三事

项目中启用gzip压缩

compression.request.enabled=true
compression.request.enabled=true
compression.request.mime-types=text/xml,application/xml,application/json
compression.request.min-request-size=2048
compression.response.enabled=true
compression.response.useGzipDecoder=true

请求时报错

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens; nested exception is com.fasterxml.jackson.core.JsonParseException: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens
 at [Source: (PushbackInputStream); line: 1, column: 2]

问题分析

spring-cloud-starter-openfeign版本 2.2.4.release
由于 spring-cloud-starter-openfeign 是pom 真实的处理是在 spring-cloud-open-feign-core中
compression.request 在META-INF中的 spring-configuration-metadata.json定义如下

{
  "name": "feign.compression.request",
  "type": "org.springframework.cloud.openfeign.encoding.FeignClientEncodingProperties",
  "sourceType": "org.springframework.cloud.openfeign.encoding.FeignClientEncodingProperties"
}

找到 FeignClientEncodingProperties
能看到 有个参数定义为
private int minRequestSize = 2048;
继续找到使用的地方

org.springframework.cloud.openfeign.encoding.FeignContentGzipEncodingInterceptor
	private boolean requiresCompression(RequestTemplate template) {

		final Map<String, Collection<String>> headers = template.headers();
		return matchesMimeType(headers.get(HttpEncoding.CONTENT_TYPE))
				&& contentLengthExceedThreshold(headers.get(HttpEncoding.CONTENT_LENGTH));
	}
这个方法就是判断是否启用gzip 比较的字段是 CONTENT_LENGTH 也就是 Content-Length

于是我们可以得到这个结论
compression.request.min-request-size是针对的 HTTP协议 head中的 Content-Length 字段,如果超过长度就启用压缩 单位是B,默认是2KB就启用压缩

再看作用范围

public void apply(RequestTemplate template) {

	if (requiresCompression(template)) {
		addHeader(template, HttpEncoding.CONTENT_ENCODING_HEADER,
				HttpEncoding.GZIP_ENCODING, HttpEncoding.DEFLATE_ENCODING);
	}
}

方法说明是 Called for every request. Add data using methods on the supplied RequestTemplate.
只要是 RequestTemplate支持的类型 均可启用压缩

补充

如果启用gzip压缩,对应的controller方法api需要显示的声明header启用 gzip 例如:

@Headers({"acceptEncoding: gzip","contentType: application/json"})

同理 如果response也启用 那么返回的内容也需要前端支持gzip解析才行
所以 需要支持gzip的时候要注意了

Feign默认并没有提供数据压缩的功能,但我们可以通过自定义编解码器的方式来实现数据压缩。 具体实现步骤如下: 1. 创建自定义编解码器 可以使用GZIP进行数据压缩,然后使用Base64进行编码。创建一个实现了`feign.codec.Encoder`和`feign.codec.Decoder`接口的编解码器实现类,具体实现如下: ```java public class GzipEncoderDecoder implements Encoder, Decoder { private final Encoder delegateEncoder; private final Decoder delegateDecoder; public GzipEncoderDecoder(Encoder delegateEncoder, Decoder delegateDecoder) { this.delegateEncoder = delegateEncoder; this.delegateDecoder = delegateDecoder; } @Override public Object decode(Response response, Type type) throws IOException, DecodeException, FeignException { String encoding = response.headers().getOrDefault("Content-Encoding", ""); if (encoding.contains("gzip")) { GZIPInputStream gzipInputStream = new GZIPInputStream(response.body().asInputStream()); return delegateDecoder.decode(response.toBuilder().body(gzipInputStream).build(), type); } return delegateDecoder.decode(response, type); } @Override public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream)) { delegateEncoder.encode(object, bodyType, new RequestTemplateAdapter(template, gzipOutputStream)); } catch (IOException e) { throw new EncodeException(e.getMessage(), e); } template.header("Content-Encoding", "gzip"); template.body(outputStream.toByteArray(), null); } private static class RequestTemplateAdapter extends RequestTemplate { private final RequestTemplate delegate; private final OutputStream outputStream; private RequestTemplateAdapter(RequestTemplate delegate, OutputStream outputStream) { this.delegate = delegate; this.outputStream = outputStream; } @Override public void body(byte[] data, Charset charset) { try { outputStream.write(data); outputStream.flush(); } catch (IOException e) { throw new RuntimeException(e); } } @Override public Collection<String> headerNames() { return delegate.headerNames(); } @Override public Collection<String> headers(String key) { return delegate.headers(key); } @Override public void header(String key, String value) { delegate.header(key, value); } @Override public String method() { return delegate.method(); } @Override public void method(String method) { delegate.method(method); } @Override public void uri(String uri) { delegate.uri(uri); } } } ``` 2. 注册自定义编解码器 在Feign客户端配置类中注册自定义编解码器,如下所示: ```java @Configuration public class FeignClientConfiguration { @Bean public Decoder decoder() { return new GzipEncoderDecoder(new JacksonDecoder(), new JacksonDecoder()); } @Bean public Encoder encoder() { return new GzipEncoderDecoder(new JacksonEncoder(), new JacksonDecoder()); } } ``` 这样就可以在Feign客户端中使用数据压缩功能了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值