使用 httpclient.4.5.6

springboot 2.0.8RELEASE

RetryExec.java

CloseableHttpResponse execute()

try {

return this.requestExecutor.execute(route, request, context, execAware);

} catch(final IOException ex) {

if (retryHandler.retryRequest(ex.execCount,  context) {

} else {

if (ex instanceof NoHttpResponseException) {

}

}

}

@Configuration
public class RestTemplateConfig { // builder.build();的并发量是5,不如 new RestTemplate() 默认200
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
}

这样建的RestTemplate 没有重发 NoHttpResponseException和org.apache.http.conn.ConnectTimeoutException 需要自定义 RetryHandler

NoHttpResponseException 服务端断开连接,客户端使用了断开的连接发送,导致报错,默认的RestTemplate 会有connection有效性检查,默认2秒检查一次

要设置客户端的Keep-Alive,客户端应该设置的比服务端短,默认RestTemplate不会设置

带Keep-Alive的头部 示例

HTTP/1.1 200 OK
Connection: Keep-Alive
Content-Encoding: gzip
Content-Type: text/html; charset=utf-8
Date: Thu, 11 Aug 2016 15:23:13 GMT
Keep-Alive: timeout=5, max=1000
Last-Modified: Mon, 25 Jul 2016 04:32:39 GMT
Server: Apache (body)

增加 Keep-Alive,设置可以减少NoHttpResponseException

String url = "http://***";
long startTime = System.currentTimeMillis();
//JSONObject json = restTemplate.getForObject("url", JSONObject.class);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Keep-Alive", "timeout=30, max=1000");
JSONObject json = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(null, httpHeaders), JSONObject.class).getBody();

spring RestTemplate 出现 NoHttpResponseException 和 ConnectionTimeoutException的更多相关文章

  1. Spring RestTemplate: 比httpClient更优雅的Restful URL访问, java HttpPost with header

    { "Author": "tomcat and jerry", "url":"http://www.cnblogs.com/tom ...

  2. Spring RestTemplate介绍

    http://www.cnblogs.com/rollenholt/p/3894117.html RestTemplate 这篇文章打算介绍一下Spring的RestTemplate.我这边以前设计到 ...

  3. How to Send an HTTP Header With Every Request With Spring RestTemplate

    In Know Which Apps Are Hitting Your Web Service, I showed how to write a servlet filter that enforce ...

  4. Spring RestTemplate详解

    Spring RestTemplate详解   1.什么是REST? REST(RepresentationalState Transfer)是Roy Fielding 提出的一个描述互联系统架构风格 ...

  5. Spring RestTemplate中几种常见的请求方式GET请求 POST请求 PUT请求 DELETE请求

    Spring RestTemplate中几种常见的请求方式 原文地址: https://blog.csdn.net/u012702547/article/details/77917939   版权声明 ...

  6. How to disable SSL certificate checking with Spring RestTemplate?(使用resttemplate访问https时禁用证书检查)

    How to disable SSL certificate checking with Spring RestTemplate?(使用resttemplate访问https时禁用证书检查) **** ...

  7. Spring RestTemplate 小结

    关于RestTemplate 首先,你可以把它理解为一个发起请求并接收响应的工具类(功能类似浏览器). 其次,它其实是一个壳,具体还是通过调用别的接口来实现(如jdk自带的连接,或者HttpClien ...

  8. spring RestTemplate用法详解

    spring RestTemplate用法详解 spring 3.2.3 框架参考有说明 21.9 Accessing RESTful services on the Client

  9. 使用HttpClient4来构建Spring RestTemplate

    Spring RestTemplate简单说明 现在REST服务已经很普及了,在我们的程序中,经常会需要调用REST API,这时候会有很多选择,原始一点的JDK自带的,再进一步点使用HttpClie ...

随机推荐

  1. Redis(1.12)Redis cluster搭建常见错误

    [1]gem install redis 报错 redis-cluster安装需要通过gem install redis来安装相关依赖.否则报错.通过gem install redis执行后会出现两个 ...

  2. Java判断指定日期是否为工作日

    Java判断指定日期是否为工作日 转自:https://www.jianshu.com/p/966659492f2f 转:https://www.jianshu.com/p/05ccb5783f65转 ...

  3. oracle报:ORA-01034和ORA-27101的解决办法

    出现ORA-01034和ORA-27101的原因是多方面的:主要是oracle当前的服务不可用,shared memory realm does not exist,是因为oracle没有启动或没有正 ...

  4. spring-boot war包部署(二)

    环境 jdk 8 tomcat 8.5 sts 4.4.2 maven 3.6.1 背景 有时候,服务器已经有了,我们必须要使用 war 包进行部署,所以需要 spring boot 支持打包和部署成 ...

  5. # [洛谷1337] 吊打XXX/平衡点 (模拟退火)

    [洛谷1337] 吊打XXX/平衡点 (模拟退火) 题意 n个重物(x,y,w),求平衡时x的位置(x,y) 分析 模拟退火基础题,基于随机数的优化算法,时间复杂度玄学,参数玄学,能不能AC看脸,当然 ...

  6. X-UA-Compatibles

    今天在看京东网页代码的时候,发现了X-UA-Compatibles 这个元信息属性,不是很清楚,百度了一下,做下记录 X-UA-Compatible 属性是 IE 浏览器在 IE8 版本开始提供的一个 ...

  7. windows 控制台默认为UTF-8显示的方法

    这里需要先了解些内容: CHCP CHCP是MS DOS中的命令,用来显示或设置活动代码页编号的.用法是: CHCP [nnn] 其中nnn指定的是代码页的编号.这个参数是可选的,在命令行下如果不指定 ...

  8. X86逆向13:向程序中插入Dll

    本章我们将学习Dll的注入技巧,我们将把一个动态链接库永久的插入到目标程序中,让程序在运行后直接执行这个Dll文件,这一章的内容也可以看作是第八课的加强篇,第八课中我们向程序中插入了一个弹窗,有木有发 ...

  9. 长沙理工大学第十二届ACM大赛-重现赛 大家一起来数二叉树吧 (组合计数)

    大意: 求n结点m叶子二叉树个数. 直接暴力, $dp[i][j][k][l]$表示第$i$层共$j$节点, 共$k$叶子, 第$i$层有$l$个叶子的方案数, 然后暴力枚举第$i$层出度为1和出度为 ...

  10. 交替方向乘子法(ADMM)的原理和流程的白话总结

    交替方向乘子法(ADMM)的原理和流程的白话总结 2018年08月27日 14:26:42 qauchangqingwei 阅读数 19925更多 分类专栏: 图像处理   作者:大大大的v链接:ht ...