使用 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) {

}

}

}

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

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

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

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

带Keep-Alive的头部 示例

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

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

  1. String url = "http://***";
  2. long startTime = System.currentTimeMillis();
  3. //JSONObject json = restTemplate.getForObject("url", JSONObject.class);
  4. HttpHeaders httpHeaders = new HttpHeaders();
  5. httpHeaders.add("Keep-Alive", "timeout=30, max=1000");
  6. 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. Java学习笔记-反射机制

    Java反射机制实在运行状态时,对于任意一个类,都能够知道这个类的属性和方法,对于任意一个对象,都能够调用他的任意一个属性和方法 获取Class对象的三种方式 Object类中的getClass()方 ...

  2. Go语言中的打包和工具链

    包 所有Go语言的程序都会组织成若干组文件,每组文件被称为一个包.这样每个包的代码都可以作为很小的复用单元,被其他项目引用. 包名惯例 给包命名的惯例是使用包所在目录的名字.并不需要所有包的名字都与别 ...

  3. day32 网络编程之粘包问题

    1.最大半连接数 什么是最大半连接数 半连接:在进行TCP协议通信时,客户端与服务器端进行三次握手建立连接,但是有时客户端与服务器端进行了连接申请,服务器端也同意了申请(既已经完成三次握手的两次),此 ...

  4. A New 3-bit Programming Algorithm using SLC-to-TLC Migration for 8MBs High Performance TLC NAND Flash Memory

    背景 1.2012年左右的数据SLC.MLC.TLC闪存芯片的区别:SLC = Single-Level Cell ,即1bit/cell,速度快寿命长,价格超贵(约MLC 3倍以上的价格),约10万 ...

  5. FTL-SLC&MTC&TLC

    1.博客 SLC.. http://diybbs.zol.com.cn/67/231_661182.html 2.FTL --作者在普及了一些FTL基本知识后,主要分析了在linux上实现的途径 ht ...

  6. [转帖]Nginx 容器教程

    Nginx 容器教程 http://www.ruanyifeng.com/blog/2018/02/nginx-docker.html 里面有证书. 作者: 阮一峰 日期: 2018年2月27日 感谢 ...

  7. python报错及处理 -- 不断总结

    ModuleNotFoundError: No module named 'PIL' 解决方法: 运行命令:pip install Pillow IndentationError: expected ...

  8. hdu 4471 区间条件统计 区间 不超过 x 的元素的个数

    题目传送门//res tp hdu 目的 对长度为n的区间,m次询问,每次提供一个区间两端点与一个值x,求区间内不超过x的元素个数 n 1e5 m 1e5 ai [1,1e9] (i∈[1,n]) 多 ...

  9. springboot2.0处理任何异常返回通用数据格式

    异常分为以下三种 自定义异常 可预知异常 不可预知异常 下面具体说明如何分类处理,从而保证无论触发什么异常均可返回理想的自定义数据格式 ResultCode /** * Created by mrt ...

  10. Docker结合Jenkins构建持续集成环境

    1.环境说明: jenkins+svn:192.168.71.142 测试环境:192.168.71.145 生产环境:192.168.71.148 操作系统:centos7. Maven3. Tom ...