关于spring resttemplate超时设置

本文详细介绍了Spring RestTemplate中关于超时设置的方法,包括HttpURLConnection的ConnectTimeout和ReadTimeout属性,以及如何通过Command Line Args、SimpleClientHttpRequestFactory和HttpComponentsClientHttpRequestFactory进行配置。特别推荐使用HttpComponentsClientHttpRequestFactory并自定义HttpClient来设置超时和连接池。
摘要由CSDN通过智能技术生成
  • 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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值