- Spring org.springframework.web.client.RestTemplate 使用 org.springframework.http.client.SimpleClientHttpRequestFactory建立 java.net.HttpURLConnection
- 后者采用 HttpURLConnection 的默认超时配置
HttpURLConnection 超时属性
ConnectTimeout(ms)
- a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection.
- If the timeout expires before the connection can be established, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.
ReadTimeout(ms)
- a specified timeout, in milliseconds.
- A non-zero value specifies the timeout when reading from Input stream when a connection is established to a resource.
- If the timeout expires before there is data available for read, a java.net.SocketTimeoutException is raised.
- A timeout of zero is interpreted as an infinite timeout
RestTemplate 超时设置
Command Line Args (不修改代码,启动时配置)
覆盖 HttpURLConnection 默认设置.
-Dsun.net.client.defaultConnectTimeout=<TimeoutInMiliSec> -Dsun.net.client.defaultReadTimeout=<TimeoutInMiliSec>
使用 SimpleClientHttpRequestFactory 建立 HttpURLConnection
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; @Configuration public class RestTemplateConfiguration { @Bean public RestTemplate restTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException { SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory(); clientHttpRequestFactory.setConnectTimeout(10 * 1000); clientHttpRequestFactory.setReadTimeout(10 * 1000); return new RestTemplate(clientHttpRequestFactory); } }
使用 HttpComponentsClientHttpRequestFactory 建立 HttpURLConnection(推荐)
- org.springframework.http.client.HttpComponentsClientHttpRequestFactory
- HttpComponentsClientHttpRequestFactory 的构造器和 setter 方法支持自定义配置的 org.apache.http.client.HttpClient
- 通过 HttpClient 的构建类 org.apache.http.impl.client.HttpClientBuilder.setConnectionManager(pollingConnectionManager)方法设置连接池
- HttpClientBuilder.setDefaultRequestConfig 设置超时
- HttpClientBuilder.setDefaultHeaders(headers) 设置默认 headers
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; @Configuration public class RestTemplateConfiguration { @Bean public RestTemplate restTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException { HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(); httpRequestFactory.setConnectionRequestTimeout(30 * 1000); httpRequestFactory.setConnectTimeout(2 * 60 * 1000); httpRequestFactory.setReadTimeout(10 * 60 * 1000); return new RestTemplate(httpRequestFactory); } }