重点是HttpRequestRetryHandler.retryRequest()方法

public static String callHttpServer(String contentType,String json, String url) {
String result = "";
CloseableHttpClient httpclient = null;
CloseableHttpResponse response = null;
try { HttpClientBuilder httpClientBuilder = HttpClients.custom();
httpClientBuilder.setRetryHandler(new HttpRequestRetryHandler() {
@Override
public boolean retryRequest(IOException arg0, int retryTimes, HttpContext arg2) {
if (retryTimes > 3) {
return false;
}
if (arg0 instanceof UnknownHostException || arg0 instanceof ConnectTimeoutException
|| !(arg0 instanceof SSLException) || arg0 instanceof NoHttpResponseException) {
return true;
} HttpClientContext clientContext = HttpClientContext.adapt(arg2);
HttpRequest request = clientContext.getRequest();
boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
if (idempotent) {
return true;
}
return false;
}
});
httpclient = httpClientBuilder.build();
HttpPost httppost = new HttpPost(url); httppost.setHeader("Content-Type", contentType);
httppost.setHeader("Expect", "100-continue");
httppost.setHeader("Accept-Encoding", "gzip,deflate");
httppost.setHeader("Connection", "Keep-Alive");
//如果json 为null,会出现异常
if (null != json) {
StringEntity stringEntity = new StringEntity(json, "utf-8");
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
httppost.setEntity(stringEntity);
} response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity(); if (entity != null) {
int status = response.getStatusLine().getStatusCode();
if ((status >= 200 && status < 300)) {
result = EntityUtils.toString(entity);
} else {
result = null; }
}
} catch (Exception ex) {
logger.error("call http service error:{}", ex);
result = null;
ex.printStackTrace();
} finally {
if (null != response) {
try {
response.close();
} catch (IOException e) {
logger.error("call http service response close error:{}", e);
e.printStackTrace();
}
}
}
return result;
}

更多参考: https://blog.csdn.net/zmx729618/article/details/78352879

Httpclient 4.5.2 请求重试机制的更多相关文章

  1. Spring Cloud 请求重试机制核心代码分析

    场景 发布微服务的操作一般都是打完新代码的包,kill掉在跑的应用,替换新的包,启动. spring cloud 中使用eureka为注册中心,它是允许服务列表数据的延迟性的,就是说即使应用已经不在服 ...

  2. 精讲响应式WebClient第6篇-请求失败自动重试机制,强烈建议你看一看

    本文是精讲响应式WebClient第6篇,前篇的blog访问地址如下: 精讲响应式webclient第1篇-响应式非阻塞IO与基础用法 精讲响应式WebClient第2篇-GET请求阻塞与非阻塞调用方 ...

  3. 精讲RestTemplate第8篇-请求失败自动重试机制

    本文是精讲RestTemplate第8篇,前篇的blog访问地址如下: 精讲RestTemplate第1篇-在Spring或非Spring环境下如何使用 精讲RestTemplate第2篇-多种底层H ...

  4. Volley超时重试机制

    基础用法 Volley为开发者提供了可配置的超时重试机制,我们在使用时只需要为我们的Request设置自定义的RetryPolicy即可. 参考设置代码如下: int DEFAULT_TIMEOUT_ ...

  5. 使用Apache HttpClient 4.x进行异常重试

    在进行http请求时,难免会遇到请求失败的情况,失败后需要重新请求,尝试再次获取数据. Apache的HttpClient提供了异常重试机制,在该机制中,我们可以很灵活的定义在哪些异常情况下进行重试. ...

  6. 为Spring Cloud Ribbon配置请求重试(Camden.SR2+)

    当我们使用Spring Cloud Ribbon实现客户端负载均衡的时候,通常都会利用@LoadBalanced来让RestTemplate具备客户端负载功能,从而实现面向服务名的接口访问. 下面的例 ...

  7. SpringCloud | FeignClient和Ribbon重试机制区别与联系

    在spring cloud体系项目中,引入的重试机制保证了高可用的同时,也会带来一些其它的问题,如幂等操作或一些没必要的重试. 今天就来分别分析一下 FeignClient 和 Ribbon 重试机制 ...

  8. Ribbon重试机制与Hystrix熔断机制的配置问题1

    Ribbon超时与Hystrix超时问题,为了确保Ribbon重试的时候不被熔断,我们就需要让Hystrix的超时时间大于Ribbon的超时时间,否则Hystrix命令超时后,该命令直接熔断,重试机制 ...

  9. Ribbon重试机制与Hystrix熔断机制的配置问题

    Ribbon超时与Hystrix超时问题,为了确保Ribbon重试的时候不被熔断,我们就需要让Hystrix的超时时间大于Ribbon的超时时间,否则Hystrix命令超时后,该命令直接熔断,重试机制 ...

随机推荐

  1. 回文自动机pam

    目的:类似回文Trie树+ac自动机,可以用来统计一些其他的回文串相关的量 复杂度:O(nlogn) https://blog.csdn.net/Lolierl/article/details/999 ...

  2. Python学习笔记—自动化部署【新手必学】

      前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:唯恋殊雨   目录 pexpect fabric pexpect P ...

  3. Git下载安装及github基本操作

    Windows下Git下载 官网提供的Git下载地址: 官网下载地址,一般直接从官网下载会出现无反应等情况,这里提供windows32和64位版本的百度网盘资源:windows下的Git下载地址.有需 ...

  4. python学习-tuple

    # 元组.关键字:tuple# 定义好了,就不可以修改.只能读.数据之间全部都是用,隔开.# 定义:()my_tuple = () # 空元组my_tuple2 = ("xj",& ...

  5. 人生苦短,我用Python(2)

    1.for循环遍历字符串: string="人生苦短,我用Python" print(string) for ch in string: print(ch) for 循环语句还可以 ...

  6. Real World CTF一日游

    今天去感受了长亭举办的RWCTF现场,参加了技术论坛,也学到了很多的知识 比较有印象的就是 智能安全在Web防护中的探索和实践 阿里云安全防护构建的AI架构体系: 基线检测 基础过滤 异常检测 攻击识 ...

  7. Linux下shell通用脚本启动jar(微服务)

    Linux下shell通用脚本启动jar(微服务) vim app_jar.sh #!/bin/bash #source /etc/profile # Auth:Liucx # Please chan ...

  8. Thread.Sleep线程休眠-小白向

    try { Thread.sleep(); } catch (InterruptedException e) { } 首先这段代码的作用是使当前进程沉睡2S,展现给用户的结果就是画面维持两秒,有个“正 ...

  9. wpf键盘

    一.键盘类和键盘事件 WPF提供了基础的键盘类(System.Input.Keyboard类),该类提供与键盘相关的事件.方法和属性,这些事件.方法和属性提供有关键盘状态的信息.Keyboard的事件 ...

  10. c++-变量,this指针,全局函数,成员函数,自定义数组类

    区分变量属于哪个对象 c++对象管理模型初探 C++类对象中的成员变量和成员函数是分开存储的,C中内存四区仍然有效 C++编译器对普通成员函数的内部处理(隐藏this指针) this指针解决函数形参和 ...