码迷,mamicode.com
首页 > 编程语言 > 详细

关于spring resttemplate超时设置

时间:2019-11-03 10:34:27      阅读:476      评论:0      收藏:0      [点我收藏+]

标签:expires   man   uri   tde   构造器   component   关于   not   set   

  • 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超时设置

标签:expires   man   uri   tde   构造器   component   关于   not   set   

原文地址:https://www.cnblogs.com/CoolSoul/p/11785021.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!