使用 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. 【VS开发】C语言遍历文件夹

    // StdCFIndAllFiles.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> ...

  2. python-Re模块用法

    主要函数:match().search().compile() re.compile compile 函数用于编译正则表达式,生成一个正则表达式( Pattern )对象,供 match() 和 se ...

  3. 2019牛客暑期多校训练营(第七场)-C Governing sand

    题目链接:https://ac.nowcoder.com/acm/contest/887/C 题意:有n种树,给出每种数的高度.移除的花费和数量,求最小花费是多少使得剩下树中最高的树的数量占一半以上. ...

  4. 【Python】【demo实验9】【练习实例】【三数排序】

    原题: 输入三个整数x,y,z,请把这三个数由小到大输出. 我的解法: #!/usr/bin/python # encoding=utf-8 # -*- coding: UTF-8 -*- # 输入三 ...

  5. 洛谷 P2018 消息传递 题解

    题面 总体来说是一道从下往上的DP+贪心: 设f[i]表示将消息传给i,i的子树全部接收到所能消耗的最小时间: 那么对于i的所有亲儿子节点j,我们会贪心地先给f[j]大的人传递,然后次大..... 可 ...

  6. curl post请求封装

    /* POST /servlet/ICBCCMPAPIReqServlet?userID=jyi.y.1001&PackageID=201807311347539185&SendTim ...

  7. MySQL 数据库的备份和恢复

    1.DOS命令 mysqldump /*DOS命令生成文本文件*/ mysqldump -u username -h host -ppassword dbname [tbanme1,tbname2,. ...

  8. C# WebApi日期格式化

    WebApi中日期格式化:在WebApiConfig文件中加入如下代码即可,之前遇到的问题,日期中总带有T,现在记录一下解决的方法. 代码: private static void ReturnDat ...

  9. Tika提取文件元数据

    Tika可以从文件中提取元数据. 什么是元数据: 元数据是文件所提供的的附件信息即文件的属性. word文档的元数据: Tika提取元数据: 我们可以使用文件parse()方法提取元数据,传递一个空的 ...

  10. js二维数组转一维数组

    方法一 利用es5的arr.reduce(callback[, initialValue])实现 var arr1 = [[0, 1], [2, 3], [4, 5]]; var arr2 = arr ...