gpt4 book ai didi

java - Spring RestTemplate SocketException 连接在快速连续执行时重置

转载 作者:搜寻专家 更新时间:2023-11-01 03:46:48 26 4
gpt4 key购买 nike

设置

本地运行的简单服务器和客户端应用程序。服务器端点收到带有 sleep 时间的 POST 请求以模拟工作。客户端是一个使用 RestTemplate 进行 HTTP 调用的 SpringBoot 应用程序。在服务器上模拟每个请求有 500 毫秒的延迟并发出 700 个多线程请求。

问题

快速连续执行客户端程序时,我得到java.net.SocketException: Connection reset。堆栈跟踪:

org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://localhost:8081/server/site": Connection reset; nested exception is java.net.SocketException: Connection reset

java.util.concurrent.ExecutionException: org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://localhost:8081/server/site": Connection reset; nested exception is java.net.SocketException: Connection reset
at java.util.concurrent.FutureTask.report(FutureTask.java:122) ~[na:1.8.0_74]
at java.util.concurrent.FutureTask.get(FutureTask.java:192) ~[na:1.8.0_74]
at com.sample.client.rest.RestClient.invokeServer(RestClient.java:75) ~[classes/:na]
at com.sample.client.SampleClientApplication.main(SampleClientApplication.java:13) [classes/:na]

Caused by: org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://localhost:8081/server/site": Connection reset; nested exception is java.net.SocketException: Connection reset
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:673) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:620) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:414) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at com.sample.client.rest.RestClient.lambda$invokeServer$0(RestClient.java:68) ~[classes/:na]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[na:1.8.0_74]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_74]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_74]
at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_74]

Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:209) ~[na:1.8.0_74]
at java.net.SocketInputStream.read(SocketInputStream.java:141) ~[na:1.8.0_74]
at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:137) ~[httpcore-4.4.8.jar:4.4.8]
at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:153) ~[httpcore-4.4.8.jar:4.4.8]
at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:282) ~[httpcore-4.4.8.jar:4.4.8]
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:138) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259) ~[httpcore-4.4.8.jar:4.4.8]
at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:163) ~[httpcore-4.4.8.jar:4.4.8]
at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:165) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:273) ~[httpcore-4.4.8.jar:4.4.8]
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125) ~[httpcore-4.4.8.jar:4.4.8]
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:272) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56) ~[httpclient-4.5.3.jar:4.5.3]
at org.springframework.http.client.HttpComponentsClientHttpRequest.executeInternal(HttpComponentsClientHttpRequest.java:89) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:659) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]

如果客户端没有快速连续执行,则不会出错。

调试/分析

  • 分析 Wireshark 捕获。服务器正在发送 RST 数据包
  • 没有关闭 PoolingHttpClientConnectionManagerCloseableHttpClient?添加了用于正常关机的 @PreDestroy 关机方法。运气不好。
  • 端口耗尽?配置为仅 200 个同时连接。

Spring RestTemplate 配置

@Configuration
public class Config {

private static final int CONNECT_TIMEOUT = 5000;

private static final int CONNECTION_MANAGER_CONNECTION_REQUEST_TIMEOUT = 0;

private static final int SOCKET_TIMEOUT = 5000;

private static final int MAX_TOTAL = 200;

private static final int MAX_PER_ROUTE = 200;


@Bean
public PoolingHttpClientConnectionManager poolingHttpClientConnectionManager() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(MAX_TOTAL);
connectionManager.setDefaultMaxPerRoute(MAX_PER_ROUTE);
connectionManager.setValidateAfterInactivity(CONNECT_TIMEOUT);
return connectionManager;
}

@Bean
public RequestConfig requestConfig() {
RequestConfig result = RequestConfig.custom()
.setConnectionRequestTimeout(CONNECTION_MANAGER_CONNECTION_REQUEST_TIMEOUT)
.setConnectTimeout(CONNECT_TIMEOUT)
.setSocketTimeout(SOCKET_TIMEOUT)
.build();
return result;
}

@Bean
public CloseableHttpClient httpClient(PoolingHttpClientConnectionManager poolingHttpClientConnectionManager, RequestConfig requestConfig) {
CloseableHttpClient result = HttpClientBuilder
.create()
.setConnectionManager(poolingHttpClientConnectionManager)
.setDefaultRequestConfig(requestConfig)
.build();
return result;
}

@Bean
public RestTemplate restTemplate() {
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setHttpClient(httpClient(poolingHttpClientConnectionManager(), requestConfig()));

RestTemplate restTemplate = new RestTemplate();
restTemplate.setRequestFactory(factory);
return restTemplate;
}

朴素的客户端程序:

public void invokeServer(long sleepTime, int concurrentThreads) {

Site site = new Site();
site.setMerchantId("MERC");
site.setSiteId("SITE 100");
CreateSiteRequest request = new CreateSiteRequest();
request.setSite(site);
request.setSleepTime(sleepTime);

ResponseEntity<Site> response = null;
if (concurrentThreads > 1) {

ExecutorService executor = Executors.newFixedThreadPool(concurrentThreads);
Future<ResponseEntity<Site>>[] futures = new Future[concurrentThreads];
Callable<ResponseEntity<Site>> callable;

for (int i = 1; i <= concurrentThreads; i++) {

callable = () -> restTemplate.postForEntity(SAMPLE_SERVER_URL + "site", request, Site.class);

futures[i - 1] = executor.submit(callable);
}

for (int i = 0; i < futures.length; i++) {
try {
response = futures[i].get();
} catch (Exception e) {
log.error("Thread " + i + " " + e.getMessage(), e);
}
}
}
}

@PreDestroy
private void shutdown() throws IOException {
client.close();
connectionManager.close();
}

感谢任何输入。谢谢。

最佳答案

我们遇到了类似的间歇性问题 - 我们移出了 RestTemplate 并使用了 HttpUrlConnection。

关于java - Spring RestTemplate SocketException 连接在快速连续执行时重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48107615/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com