我正在创建 HttpComponentsMessageSender bean,如下所示
@Bean
public HttpComponentsMessageSender reservationHttpComponent() {
HttpComponentsMessageSender httpComponentsMessageSender = new HttpComponentsMessageSender();
httpComponentsMessageSender.setConnectionTimeout(reservationConnectionTimeOut);
httpComponentsMessageSender.setReadTimeout(reservationReadTimeOut);
return httpComponentsMessageSender;
}
在这里,我遇到了间歇性读取超时问题 - 就像我在休息 30 分钟后第一次尝试时,我遇到了读取超时,之后所有进一步的交易都成功了。如果再次休息 30 分钟,那么第一次交易又因读取超时问题而失败,然后所有进一步都成功......
我尝试修复如下代码 -
@Bean
public HttpComponentsMessageSender reservationHttpComponent() {
RequestConfig requestBuilder = RequestConfig.custom()
.setSocketTimeout(reservationReadTimeOut)
.setConnectionRequestTimeout(reservationConnectionTimeOut)
.setConnectTimeout(reservationConnectionTimeOut)
.setCircularRedirectsAllowed(false)
.build();
org.apache.http.client.HttpClient httpClient = HttpClientBuilder.create()
.setConnectionManager(getConnManager())
.addInterceptorFirst(new HttpComponentsMessageSender.RemoveSoapHeadersInterceptor())
.setDefaultRequestConfig(requestBuilder)
.build();
HttpComponentsMessageSender messageSender = new HttpComponentsMessageSender();
messageSender.setHttpClient(httpClient);
return messageSender;
}
private PoolingHttpClientConnectionManager getConnManager() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setDefaultMaxPerRoute(connectionManagerDefaultMaxPerRoute);
connectionManager.setMaxTotal(connectionManagerMaxTotal);
connectionManager.setDefaultSocketConfig(SocketConfig.custom()
.setSoTimeout(reservationReadTimeOut).build());
return connectionManager;
}
上面已经解决了读取超时问题,但后来我开始遇到以下问题-
message:Error Occurred While Retrieving Reservation - I/O error: Timeout waiting for connection from pool; nested exception is org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool
我正在寻找修复,然后我尝试使用 TTL 初始化 PoolingHttpClientConnectionManager -
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(1, TimeUnit.MINUTES)
然后我运行了多个性能 jmx 脚本,我在我的服务上触发了 70 - 80 TPS,上面有外部依赖的代码,一切看起来都很好。
但是我不确定在 PoolingHttpClientConnectionManager 构造函数中是否有 TTL 是非常合适的解决方案。所以在这里我正在寻找有关此解决方案是否会导致任何进一步问题或其他任何可能是比这更好的方法的建议。