简单定制restTemplate(超时处理)

简单定制restTemplate(超时处理)

基于上个例子–===> 简单的RestTemplate示例:(暂时理解URL请求示例)
pom.xml
添加commons-lang3的支持需要里面的StringUtils方法,
比较String字符( StringUtils.equalsAnyIgnoreCase)以及 是否是数字(StringUtils.isNumeric),

添加httpclient作为底层http库。**

简单定制restTemplate(超时处理)_第1张图片
main:
HttpComponentsClientHttpRequestFactory配置:
连接池配置==存活时间–>最大链接数–>最大Route连接。
CloseableHttpClient:
使用custom(),设置Idle(闲置)最大时间,
evictIdleConnections禁用重发请求,
keep-live策略:使用自己定制的策略CustomConnectionKeepAliveStrategy
简单定制restTemplate(超时处理)_第2张图片
CustomConnectionKeepAliveStrategy:
获取HTTP.CONN_KEEP_ALIVE,查找有”timeout“的字符并且存在值,
遍历并把值转换成Long类型,没有直接赋值默认值,
最后找不到还是使用默认值。
简单定制restTemplate(超时处理)_第3张图片
restTemplate设置连接超时时间,读超时时间,使用前面配置的HttpComponentsClientHttpRequestFactory:
简单定制restTemplate(超时处理)_第4张图片

设置连接时间是1ms或者Read 的时间是1ms,连接超时会报错。。。。timeout

你可能感兴趣的:(简单定制restTemplate(超时处理))