關於spring resttemplate超時設置

  • 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);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章