使用 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. TS问题:属性'filter'在类型'Vue'上不存在

    在编译时候报‘Property 'filter' does not exist on type 'Element'.’的错,但是在浏览器中运行正常.如下图: 经过不懈的努力,终于找到了原因.是因为ts ...

  2. Hive概述

    HIVE是什么    开发调试麻烦    只能用java开发     需要对hadoop的底层及api比较了解才能开发复杂代码 HQL Hive是基于Hadoop的一个数据仓库工具.可以将结构化的数据 ...

  3. 博客C语言I作业11

    一.本周教学内容&目标 第5章 函数 要求学生掌握各种类型函数的定义.调用和申明,熟悉变量的作用域.生存周期和存储类型. 二.本周作业头 这个作业属于哪个课程 c语言程序设计II 这个作业要求 ...

  4. 阿里EMR部署

    选自定义购买: 选择master配置: 选择core配置: 下一步,选高级里在jdbc后填RDS的url, 用户名,密码: jdbc:mysql://rm-d7o7x76l11u0434zn.mysq ...

  5. vsphere6.7-虚拟机与ESXI时间同步

    环境介绍 esxi 6.7+vsphere6.7 需求配置 设置虚拟机时间与esxi时间同步.esxi时间与NTP服务器同步 配置方式 在esxi上开启NTP服务器时间同步,如下图: 修改虚拟服务器的 ...

  6. 使用Spring-boot-admin对Spring boot的服务进行监控

    要进行监控,需要两个Project,一个是Admin Server端,负责监控Spring boot的项目,另一个是Admin Client端,是被监控的Spring boot服务. Admin Se ...

  7. PAT B1026.程序运行时间

    AC代码 #include <cstdio> #define CLK_TCK 100 int main() { int C1, C2, C3; scanf("%d%d" ...

  8. STL queue 常见用法详解

    <算法笔记>学习笔记 queue 常见用法详解 queue翻译为队列,在STL中主要则是实现了一个先进先出的容器. 1. queue 的定义 //要使用queue,应先添加头文件#incl ...

  9. 接口踩坑:Status (blocked:other)

    1.请求接口时出现 Status (blocked:other) 2.原因分析:安装了Adblock 3.解决办法 1)关掉Adblock2)修改接口名称,不能用 ad 或者 XX ad XX 等名称 ...

  10. docker-扩展

    #设置容器监听TCP端口: 重启dockersystemctl restart docker 查看docker监听的235端口netstat -nltp    curl -s http://192.1 ...