关于spring resttemplate超时设置

Posted coolsoul

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于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);
    }
}

以上是关于关于spring resttemplate超时设置的主要内容,如果未能解决你的问题,请参考以下文章

设置RestTemplate的读取超时

Spring Cloud常用组件超时总结

Spring RestTemplate 连接超时不起作用

RestTemplate设置每个请求的超时

重学springboot系列番外篇之RestTemplate

关于Hystrix超时机制和线程状态的测试观察和个人理解