猿问

使用 Spring-ws 和 4.5.x Http 客户端忽略 DNS 轮询以获得可靠的超时

我们正在使用带有 Spring-Ws 的 Http Client 4.5.x 并使用该webServiceTemplate.marshalSendAndReceive(requestObject)方法发出请求。我们想要一个可靠的连接超时值,但目前遇到了第 8 节(DNS 循环)中描述的问题,其中尝试了多个 IP 地址,因此超时是不可预测的。是否有简单的方法可以仅使用 Spring-ws 和 Http Client 库在一段时间后设置硬超时,或者是否需要设置某种自定义超时?


案例:连接超时设置为 1 秒(该方法的实际超时为 4 秒——是否可以使用 Spring/Http 客户端库将方法超时设置为 1 秒?)


应用程序日志(Http 客户端日志设置为DEBUG):


16:45:02 (org.apache.http.impl.execchain.MainClientExec) Opening connection {}->http://salesforce.com:448 

16:45:02 (org.apache.http.impl.conn.DefaultHttpClientConnectionOperator) Connecting to salesforce.com/96.43.149.26:448 

16:45:03 (org.apache.http.impl.conn.DefaultHttpClientConnectionOperator) Connect to salesforce.com/96.43.149.26:448 timed out. Connection will be retried using another IP address 

16:45:03 (org.apache.http.impl.conn.DefaultHttpClientConnectionOperator) Connecting to salesforce.com/96.43.145.26:448 

16:45:04 (org.apache.http.impl.conn.DefaultHttpClientConnectionOperator) Connect to salesforce.com/96.43.145.26:448 timed out. Connection will be retried using another IP address 

16:45:04 (org.apache.http.impl.conn.DefaultHttpClientConnectionOperator) Connecting to salesforce.com/96.43.144.26:448 

16:45:05 (org.apache.http.impl.conn.DefaultHttpClientConnectionOperator) Connect to salesforce.com/96.43.144.26:448 timed out. Connection will be retried using another IP address 

16:45:05 (org.apache.http.impl.conn.DefaultHttpClientConnectionOperator) Connecting to salesforce.com/96.43.148.26:448

16:45:06 (org.apache.http.impl.conn.DefaultManagedHttpClientConnection) http-outgoing-0: Shutdown connection 

Http客户端bean:


<bean id="httpClientBean" class="org.apache.http.client.HttpClient" factory-bean="httpClientFactory" factory-method="getHttpClient" />


服务代码(我们希望这个方法调用需要 X 秒,而不是 2x 或 3x 秒):


// we want this method call to take ~1 second, not ~4 seconds (i.e. similar to the connection timeout value, not a multiplier)

Object obj = webServiceTemplate.marshalSendAndReceive(requestDocument);


陪伴而非守候
浏览 156回答 1
1回答

蛊毒传说

有两种选择建立自定义 ClientConnectionOperator构建自定义DnsResolver. 这个选项要简单得多。CloseableHttpClient client = HttpClients.custom()&nbsp; &nbsp;.setDnsResolver(host -> new InetAddress[] { InetAddress.getByName(host) })&nbsp; &nbsp;.build();
随时随地看视频慕课网APP

相关分类

Java
我要回答