重点是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. ios开发入门- WebView使用

    转自:http://mahaile.blog.51cto.com/2891586/1021515/ 目标 : webview使用 在一些移动app中,有时程序会载入 外表的html 界面, andro ...

  2. JQuery中操作元素的属性_对象属性

    我们主要是通过attr去获取元素的属性: 看body内容: <body> <p> 账号:<input type="text" id="una ...

  3. CoderForces Round526 (A~E)题解

    A. The Fair Nut and Elevator time limit per test 1 second memory limit per test 256 megabytes input ...

  4. 使用 Ocelot 匹配路由的方法匹配路由

    使用 Ocelot 匹配路由的方法匹配路由 Intro 之前我们在 Ocelot 网关的基础上自定义了一个认证授权的 Ocelot 中间件,根据请求的路径和 Method 进行匹配,找到对应的权限配置 ...

  5. mui 顶部选项卡的两种切换方式

    mui 顶部选项卡的两种切换方式 第一种main页面 <!DOCTYPE html> <html> <head> <meta charset="ut ...

  6. ARTS-S golang panic返回默认值

    package main import "fmt" func fn_test_panic() (a int) { a = 2 panic("This is panic&q ...

  7. Socket无法通过防火墙的问题

    无论是配好端口还是例外的应用程序都不行 更改本地终结点为 socket.Bind()); IPAddress.Any 不要使用127.0.0.1 不要使用127.0.0.1 不要使用127.0.0.1

  8. centos7 redis 6379端口telnet不通

    1.查看redis服务是否启动,如图所示,redis已经启动 2.查看是否监听正确的ip和端口 发现问题:端口号6379没错,但是ip是127.0.0.1,表示只能本地访问,问题就出在这. 3.修改r ...

  9. 《Java基础知识》Java技术总结

    1. Java 知识点总结 Java标示符.保留字和数制:https://www.cnblogs.com/jssj/p/11114041.html Java数据类型以及变量的定义:https://ww ...

  10. js问题记录(一) -- 关于for in, sort(), 及prototype

    1.关于for in for in : 遍历对象中的可枚举的属性 例子1:for in 遍历对象的键为String类型,所以调用时用Object[key]形式,而不用Object.key形式 < ...