关于spring resttemplate超时设置_张大仙是个妖怪的博客-程序员秘密

技术标签: spring  java  超时设置  restTemplate  # Spring Boot  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);
    }
}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/NDKHBWH/article/details/104772463

智能推荐

Linux终端查看“我是谁”(“妈妈喜欢谁”)_b1ta的博客-程序员秘密

太好玩了这是Linux下的用户查询指令“妈妈喜欢谁”$ who am iadmin pts/0 2015-07-07 19:35 (:1.0)$ who mom likesadmin pts/0 2015-07-07 19:35 (:1.0)

java程序实现小球来回运动_编写一个程序实现一个球来回摆动_风起时_想你的博客-程序员秘密

java基础:窗口右上角有一个静小白球,左下角有一个动小蓝球,我们让动小蓝球去找静小白球,找到以后动小蓝球在原路返回,这样来回运动。背景是下雪场景。import java.awt.Component;import javax.swing.JFrame;public class MyStarFrame { //给私有化JFrame起一个名称,名叫frame private JFrame

nodeJS 操作MongoDB count方法的正确姿势_nodejs中的类.count()_大叔0o0的博客-程序员秘密

今天项目中遇到了需要通过MongoDB的count方法获取数值的情况,在网上查遍的各个论坛,然而都是轻描淡写一笔带过(其实就是扯淡),可能是MongoDB版本的原因db.collection(&quot;blogs&quot;).find({author: 'f', type: '1'}).count()根本不会返回数值 ,而db.blogs.find({author: 'f', type: '1'}).count(...

随便推点

了解硬盘分区_daichanglin的博客-程序员秘密

         你新买来的硬盘是不能直接使用的,必须对它进行分区并进行格式化的才能储存数据。  硬盘分区是操作系统安装过程中经常谈到的话题。对于一些简单的应用,硬盘分区并不成为一种障碍,但对于一些复杂的应用,就不能不深入理解硬盘分区机制的某些细节。  硬盘的崩溃经常会遇见,特别是病毒肆虐的时代,关于引导分区的恢复与备份的技巧,你一定要掌握。  在使用电脑时,你往往会使用几个操作系统。如何在硬

【预览pdf】:uniapp在微信小程序预览pdf,兼容安卓/IOS_uniapp pdf 兼容_乐语_miss的博客-程序员秘密

前言提示:因最近项目需求,项目需要用uniapp来开发微信小程序预览pdf的功能, 后端以链接形式返回pdf地址。一、方法概述开始用uniapp提供微信自带方法:wx.downloadFile({ }) + wx.openDocument({ }),但发现android正常打开,IOS打不开的情况 换种方式使用: web-view,uni-app 中web-view做链接跳转,但是出现IOS可以正常打开,android打不开的情况。综上:安卓手机用原生,ios手机用web-view二、方法使

EndNote 高校_如何用Endnote分分钟搞定参考文献_weixin_39842918的博客-程序员秘密

解螺旋公众号·陪伴你科研的第1924天Endnote教程第二弹来啦!写论文离不了对参考文献的引用,Endnote在文献撰写中发挥着重要作用。下面我们介绍一些在文章撰写中常用的Endnote功能。1插入和删除参考文献插入参考文献,可以根据在Endnote中选中要插入的一个或多个文献,点击Endnote X9插件-Insert Citation-点击Inserted selected citation...

Java将docx转换为html格式字符串_docx转html java_Kiaaaa的博客-程序员秘密

docx文件转换为html格式的字符串,类似于"xxxxxxx"

awk详细教程-基础篇_awk 教程_surpassLiang的博客-程序员秘密

1.前言awk是Unix环境下一个强大的文本分析工具,相对于grep和sed,awk在其对数据分析并生成报告时,显得尤为强大。它还有许多精心设计的特性,支持awk脚本语言执行,从而极大程度提高重用率。其功能就是把文件默认逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理。这里之所以说默认,是因为这里也是有配置项进行修改的。2.环境准备这里主要准备一个文本文件,名称建议为awk.txt,内容如下:1 this is a test2 Are you like awk3

AR内容开发解密,带你从深挖掘AR技术_ARqq1234的博客-程序员秘密

2016年被称为VR元年,可见火爆程度,但是我要告诉你,其实还有一种技术AR(增强现实)技术,才是下一个真正的“风口”技术。可以预见的是,未来AR应用爆发之时,必将超越VR产业规模,开拓千亿级市场空间!     AR技术不仅在与VR技术相类似的应用领域,诸如尖端武器、飞行器的研制与开发、数据模型的可视化、虚拟训练、娱乐与艺术等领域具有广泛的应用,而且由于其来的发展趋势,因为它能够带给人们更...

推荐文章

热门文章

相关标签