我们有两个应用程序,一个是服务器,另一个是客户端。
服务器在weblogic (应用程序A)上,客户端在春季启动(在tomcat上)。当我们进行A发送请求给B的负载测试时,在某一时刻有上百个绑定异常错误,比负载测试保持正常运行,比绑定异常,比正常运行等等。如果我们使用较高的TPS进行负载测试,则会更频繁地得到这些异常。这是一种情况:
错误是:
org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://XXXXXXX:9090/api/8252": Cannot assign requested address; nested exception is java.net.BindException: Cannot assign requested address
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:666)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
at org.springframework.web.client.RestTemplate.postForLocation(RestTemplate.java:355)
at com.ttech.tims.tes.pushws.impl.PushConsumerThread.tryToSendPushRequest(PushConsumerThread.java:207)
at com.ttech.tims.tes.pushws.impl.PushConsumerThread.pushMessage(PushConsumerThread.java:162)
at com.ttech.tims.tes.pushws.impl.PushConsumerThread.run(PushConsumerThread.java:350)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.BindException: Cannot assign requested address
at sun.nio.ch.Net.connect0(Native Method)
at sun.nio.ch.Net.connect(Net.java:454)
at sun.nio.ch.Net.connect(Net.java:446)
at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:648)
at weblogic.socket.NIOSocketMuxer.newSocket(NIOSocketMuxer.java:432)
at weblogic.socket.NIOSocketMuxer.newSocket(NIOSocketMuxer.java:364)
at weblogic.socket.ChannelSocketFactory.createSocket(ChannelSocketFactory.java:98)
at weblogic.net.http.HttpClient.openServer(HttpClient.java:384)
at weblogic.net.http.HttpClient.openServer(HttpClient.java:511)
at weblogic.net.http.HttpClient.New(HttpClient.java:313)
at weblogic.net.http.HttpClient.New(HttpClient.java:292)
at weblogic.net.http.HttpURLConnection.connect(HttpURLConnection.java:295)
at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:78)
at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:652)
... 6 more到目前为止,我已经尝试过:
到目前为止,还没有得到任何改进,直到一定程度上还得到了BindExceptions的支持。有什么建议吗?
发布于 2018-02-19 10:19:49
感谢EJP的建议。下面是我解决这个问题的方法。Spring模板正在等待unix关闭连接。因此,在达到最大套接字连接后,连接将处于TIME_WAIT状态。当我们将Apache ClientHttpRequestFactory作为spring RestTemplate的工厂发送时,Apache会使用它的连接池来处理请求。以下是实现:
@Bean("apacheRequestFactory")
public ClientHttpRequestFactory createRequestFactory() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
// maximum connections in the pool
connectionManager.setMaxTotal(this.systemPropertyBean.getPushConsumerThreadCnt());
// maximum concurrent connection to the hosts is equal to the our push thread count.
connectionManager.setDefaultMaxPerRoute(this.systemPropertyBean.getPushConsumerThreadCnt());
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(this.systemPropertyBean.getPushTimeoutMillis())// 3 sn
.setConnectionRequestTimeout(this.systemPropertyBean.getPushTimeoutMillis())
.setSocketTimeout(this.systemPropertyBean.getPushTimeoutMillis()).build(); // read timeout
/* the Connection Timeout (http.connection.timeout) – the time to establish the connection with the remote host
the Socket Timeout (http.socket.timeout) – the time waiting for data – after the connection was established; maximum time of inactivity between two data packets
the Connection Manager Timeout (http.connection-manager.timeout) – the time to wait for a connection from the connection manager/pool
*/
CloseableHttpClient httpClient = HttpClientBuilder.create().setConnectionManager(connectionManager).setDefaultRequestConfig(config).build();
return new HttpComponentsClientHttpRequestFactory(httpClient);
}
@Bean
public RestTemplate restTemplate() {
// I was using SimpleClientHttpRequestFactory before.
RestTemplate restTemplate = new RestTemplate(createRequestFactory());
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
return restTemplate;
}https://stackoverflow.com/questions/48800412
复制相似问题