优化拦截器调用,微服务不开启gzip
This commit is contained in:
@@ -1,12 +1,17 @@
|
||||
package xtools.cloud.call.config;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import org.jspecify.annotations.NullMarked;
|
||||
import org.springframework.boot.restclient.RestClientCustomizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.client.ClientHttpRequest;
|
||||
import org.springframework.http.client.JdkClientHttpRequestFactory;
|
||||
import xtools.cloud.call.interceptor.ClientHttpInterceptor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.time.Duration;
|
||||
|
||||
@@ -42,8 +47,23 @@ public class RestClientConfig {
|
||||
HttpClient httpClient = HttpClient.newBuilder()
|
||||
.connectTimeout(Duration.ofSeconds(clientConfig.getConnectTimeout()))
|
||||
.version(HttpClient.Version.HTTP_2)
|
||||
// 设置NEVER策略来禁用自动解压
|
||||
.followRedirects(HttpClient.Redirect.NEVER)
|
||||
.build();
|
||||
restClientBuilder.requestFactory(new JdkClientHttpRequestFactory(httpClient));
|
||||
|
||||
// 需要自定义JdkClientHttpRequestFactory来禁用Accept-Encoding头
|
||||
restClientBuilder.requestFactory(new JdkClientHttpRequestFactory(httpClient) {
|
||||
@Override
|
||||
@NullMarked
|
||||
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
|
||||
ClientHttpRequest request = super.createRequest(uri, httpMethod);
|
||||
// 移除或覆盖Accept-Encoding头,防止服务器返回压缩内容
|
||||
request.getHeaders().remove("Accept-Encoding");
|
||||
// 或直接设置为空
|
||||
request.getHeaders().set("Accept-Encoding", "identity");
|
||||
return request;
|
||||
}
|
||||
});
|
||||
|
||||
// 配置拦截器
|
||||
restClientBuilder.requestInterceptor(clientHttpInterceptor());
|
||||
|
||||
@@ -2,7 +2,6 @@ package xtools.cloud.call.interceptor;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jspecify.annotations.NonNull;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
@@ -13,6 +12,7 @@ import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.util.StreamUtils;
|
||||
import xtools.boot.api.constant.BootCommonConstant;
|
||||
import xtools.boot.api.model.dto.log.LogTrack;
|
||||
import xtools.boot.core.utils.SpringContextUtils;
|
||||
import xtools.boot.log.LogBus;
|
||||
import xtools.boot.log.enums.LogBusBaseType;
|
||||
import xtools.boot.log.holder.LogTrackHolder;
|
||||
@@ -35,9 +35,6 @@ import java.util.Objects;
|
||||
@Slf4j
|
||||
public class ClientHttpInterceptor implements ClientHttpRequestInterceptor {
|
||||
|
||||
@Resource
|
||||
private CallInterceptor callInterceptor;
|
||||
|
||||
/**
|
||||
* 拦截请求
|
||||
*
|
||||
@@ -54,6 +51,7 @@ public class ClientHttpInterceptor implements ClientHttpRequestInterceptor {
|
||||
byte @NonNull [] body,
|
||||
@NonNull ClientHttpRequestExecution execution
|
||||
) throws IOException {
|
||||
CallInterceptor callInterceptor = SpringContextUtils.getBeanDefNull(CallInterceptor.class);
|
||||
// 请求前执行
|
||||
if (Objects.nonNull(callInterceptor)) {
|
||||
callInterceptor.before(request);
|
||||
|
||||
Reference in New Issue
Block a user