一般来讲我们访问外部资源时,需要做一个保护,比如最常见的添加一个超时设置,避免一直被阻塞,RestTemplate可以通过SimpleClientHttpRequestFactory
来处理超时设置
I. RestTemplate超时设置
博文测试项目完全基于【WEB系列】RestTemplate基础用法小结的项目环境,建议配合查看
基本环境:IDEA
+ maven
+ SpringBoot 2.2.1.RELEASE
1. 超时端点
添加一个超时模拟的端点如下
1 | private String getHeaders(HttpServletRequest request) { |
2. 超时设置
主要是通过设置SimpleClientHttpRequestFactory
来设置超时
1 | /** |
输出如下:
1 | (timeOut start: 1593420406204 |
II. 其他
1. 源码&系列博文
博文
源码
- 工程:https://github.com/liuyueyi/spring-boot-demo
- 源码: https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/221-web-resttemplate
1. 一灰灰Blog
尽信书则不如,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激
下面一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛
- 一灰灰Blog个人博客 https://blog.hhui.top
- 一灰灰Blog-Spring专题博客 http://spring.hhui.top
Be the first person to leave a comment!