RestTemplate(httpclient)动态设置超时时间

基本实现

httpclient版本

1
2
3
4
5
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>

RestTemplate构造

1
2
3
4
5
6
7
8
9
10
11
12
HttpClientBuilder httpClientBuilder = HttpClients.custom();
// HttpClient相关配置
HttpClient httpClient = httpClientBuilder.build();

// 使用自定义HttpClientRequestFactory
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpClientRequestFactory(httpClient);
clientHttpRequestFactory.setConnectTimeout(5000);
// 即为 SocketTimeout
clientHttpRequestFactory.setReadTimeout(30000);
clientHttpRequestFactory.setConnectionRequestTimeout(5000);

RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory)

HttpClientRequestFactory类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.protocol.HttpContext;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;

import java.net.URI;

public class HttpClientRequestFactory extends HttpComponentsClientHttpRequestFactory {

public static ThreadLocal<Integer> socketTimeoutThreadLocal = new ThreadLocal<>();

public HttpClientRequestFactory(HttpClient httpClient) {
super(httpClient);
}

@Override
protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
HttpContext context = HttpClientContext.create();

RequestConfig config = createRequestConfig(getHttpClient());

// 从ThreadLocal中获取超时时间,并设置到context中
Integer socketTimeout = socketTimeoutThreadLocal.get();
if (null != socketTimeout) {
RequestConfig.Builder builder = RequestConfig.copy(config);
builder.setSocketTimeout(socketTimeout);
config = builder.build();
}

context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
return context;
}
}

使用方法

1
2
3
4
5
6
7
8
// 设置当前请求超时时间
HttpClientRequestFactory.socketTimeoutThreadLocal.set(15000);
try {
restTemplate.***()
} finally {
// 清理ThreadLocal中超时时间
HttpClientRequestFactory.socketTimeoutThreadLocal.remove();
}

原理解析

1
org.apache.http.impl.execchain.MainClientExec.execute(...)

httpclient在发起请求时会优先使用context中的超时时间设置

1
2
3
4
5
6
final RequestConfig config = context.getRequestConfig();
...
final int timeout = config.getSocketTimeout();
if (timeout >= 0) {
managedConn.setSocketTimeout(timeout);
}

使用案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.protocol.HttpContext;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;

import java.net.URI;

public class HttpClientRequestFactory extends HttpComponentsClientHttpRequestFactory {

private static ThreadLocal<Integer> SOCKET_TIMEOUT_THREAD_LOCAL = new ThreadLocal<>();

public HttpClientRequestFactory(HttpClient httpClient) {

super(httpClient);
}

@Override
protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {

HttpContext context = HttpClientContext.create();
RequestConfig config = createRequestConfig(getHttpClient());

Integer socketTimeout = SOCKET_TIMEOUT_THREAD_LOCAL.get();
if (null != socketTimeout) {
RequestConfig.Builder builder = RequestConfig.copy(config);
builder.setSocketTimeout(socketTimeout);
config = builder.build();
}

context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
return context;
}

@Override
protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {

if (httpMethod == HttpMethod.GET) {
return new HttpGetRequestForBody(uri);
}
return super.createHttpUriRequest(httpMethod, uri);
}

private static final class HttpGetRequestForBody extends HttpEntityEnclosingRequestBase {
public HttpGetRequestForBody(final URI uri) {

super.setURI(uri);
}

@Override
public String getMethod() {

return HttpMethod.GET.name();
}
}

public static void set(Integer timeoutMills) {

SOCKET_TIMEOUT_THREAD_LOCAL.set(timeoutMills);
}

public static void remove() {

SOCKET_TIMEOUT_THREAD_LOCAL.remove();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
HttpClientRequestFactory.set(6000);
Result<ChannelsPayResponse> payCreateResult;
try {
HttpEntity<ChannelsPayRequest> requestEntity = new HttpEntity<>(request, getHttpHeaders());
ParameterizedTypeReference<Result<ChannelsPayResponse>> parameterizedTypeReference =
new ParameterizedTypeReference<Result<ChannelsPayResponse>>() {};
ResponseEntity<Result<ChannelsPayResponse>> responseEntity = restTemplate.exchange(String.format("http://%s/channels/pay", channelService.getServiceAppName()),
HttpMethod.POST, requestEntity, parameterizedTypeReference);
payCreateResult = responseEntity.getBody();
log.info(JsonUtils.beanToJson(payCreateResult));
} catch (Exception e) {
if (e.getCause() instanceof SocketTimeoutException) {
Result<ChannelsPayQueryResponse> payQueryResult;
try {
HttpEntity<ChannelsPayQueryRequest> requestEntity = new HttpEntity<>(request, getHttpHeaders());
ParameterizedTypeReference<Result<ChannelsPayQueryResponse>> parameterizedTypeReference =
new ParameterizedTypeReference<Result<ChannelsPayQueryResponse>>() {};
ResponseEntity<Result<ChannelsPayQueryResponse>> responseEntity = restTemplate.exchange(String.format("http://%s/channels/pay/query", channelService.getServiceAppName()),
HttpMethod.POST, requestEntity, parameterizedTypeReference);
payQueryResult = responseEntity.getBody();
log.info(JsonUtils.beanToJson(payQueryResult));
} catch (Exception e) {
log.error("", e);
throw e;
}
}
} finally {
HttpClientRequestFactory.remove();
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2021 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :