使用 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. mysql数据库之单表查询多表查询

    单表查询 前期表准备 create table emp( id int not null unique auto_increment, name varchar(20) not null, sex e ...

  2. Linux系统定时任务介绍

    定时任务Crond介绍 1)crond是什么? 守护进程:持续运行的程序,不退出的进程. 为什么要使用crond定时任务呢? 1)Linux下定时任务的种类 at crontab  anacron 2 ...

  3. 算法flink

    和Yarn-Cluster模式不同,Session模式的资源在启动Yarn-Session时候就已经启动了,后续提交的作业全都在已申请的资源空间内运行,比较适合小而多的作业 # 启动yarn-sess ...

  4. 什么是NameNode和DataNode?他们是如何协同工作的?

    [学习笔记] 什么是NameNode和DataNode?他们是如何协同工作的? 马克-to-win @ 马克java社区:一个HDFS集群包含一个NameNode和若干的DataNode(start- ...

  5. PAT A1006 Sign In and Sign Out (25)

    AC代码 #include <cstdio> #include <algorithm> #include <iostream> using namespace st ...

  6. dij 费用流

    #include <bits/stdc++.h> using namespace std; typedef long long lld; const int MAXN = 50010, M ...

  7. matplotlib库的基本使用与折线图

    matplotlib:最流行的Python底层绘图库,主要做数据可视化图表,名字取材于MATLAB,模仿MATLAB构建 基本使用: x和y的长度必须一致 figure()方法用来设置图片大小 x,y ...

  8. L1-025. 正整数A+B 简单复习一下,。

    本题的目标很简单,就是求两个正整数A和B的和,其中A和B都在区间[1,1000].稍微有点麻烦的是,输入并不保证是两个正整数. 输入格式: 输入在一行给出A和B,其间以空格分开.问题是A和B不一定是满 ...

  9. Java Web DNS域名解析

    一.什么是DNS DNS(Domain Name System,域名系统),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串 ...

  10. Javascript——数据类型 和 注释

    数据类型:JavaScript中包括如下7种数据类型:字符串.数字.布尔.数组.对象.null.undefined 字符串: 注意:字符串类型的数据需要使用单引号或双引号引起来. 数字: 注意:Jav ...