总结-HttpClient-RetryHandler重试
来源:互联网 发布:淘宝 门禁ic卡 编辑:程序博客网 时间:2024/06/15 06:24
目前的项目接口都是http,因此在java项目中使用apache httpclient进行数据传输、访问。
目前程序中涉及到需要callback操作,product需要被动的接收consume的处理状态,为了最大程度的能够callback成功因此consume在http调用出现问题(如:服务不可用、异常、超时)情况下需要进行重试(retry request),在这里我列举出我找到的retry方案,有些成功有些不成功。
我是用的httpclient版本是4.5.2
。关于retry功能我在网上也找了不少的资料,但是都不是我对应的httpclient版本,大多是过时的。
在httpclient版本4.5.2
提供了以下几种retry方案:
StandardHttpRequestRetryHandler
这种方案没有测试通过,StandardHttpRequestRetryHandler
实际上是DefaultHttpRequestRetryHandler
的子类,这是官方提供的一个标准的retry方案,为了保证幂等性约定resetful接口必须是GET, HEAD, PUT, DELETE, OPTIONS, and TRACE
中的一种,如下,是我定义的httpclient pool
public static CloseableHttpClient getHttpClient() { PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(MAX_TOTAL); cm.setDefaultMaxPerRoute(MAX_PERROUTE); CloseableHttpClient httpClient = HttpClients .custom() .setRetryHandler(new StandardHttpRequestRetryHandler()) .setConnectionManager(cm) .build(); return httpClient;}
如下是我的测试代码
@Testpublic void test6(){ HttpPost httpPost=new HttpPost("http://127.0.0.1:8080/testjobs1"); try { rsp=httpClient.execute(httpPost); log.info(">> {}",rsp.getStatusLine().getStatusCode()); } catch (Exception e) { log.error(e.getMessage(),e); }finally{ HttpUtil.close(rsp); }}
运行测试,当url错误、后台报错、后台超时等情况的时候不能进行retry,因此放弃了此方案。
DefaultHttpRequestRetryHandler
这种方案没有测试通过,和上面的StandardHttpRequestRetryHandler
类似,它提供了一种默认的retry方案,并没有像StandardHttpRequestRetryHandler
一样约定接口必须是冥等的,如下,是我定义的httpclient pool
public static CloseableHttpClient getHttpClient() { PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(MAX_TOTAL); cm.setDefaultMaxPerRoute(MAX_PERROUTE); CloseableHttpClient httpClient = HttpClients .custom() .setRetryHandler(new DefaultHttpRequestRetryHandler()) .setConnectionManager(cm) .build(); return httpClient;}
如下是我的测试代码
@Testpublic void test6(){ HttpPost httpPost=new HttpPost("http://127.0.0.1:8080/testjobs1"); try { rsp=httpClient.execute(httpPost); log.info(">> {}",rsp.getStatusLine().getStatusCode()); } catch (Exception e) { log.error(e.getMessage(),e); }finally{ HttpUtil.close(rsp); }}
依然没有达到希望的效果。
HttpRequestRetryHandler
可以实现,但是不够完美。在官方文档有这么一段,如下,
HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() { public boolean retryRequest( IOException exception, int executionCount, HttpContext context) { if (executionCount >= 5) { // Do not retry if over max retry count return false; } if (exception instanceof InterruptedIOException) { // Timeout return false; } if (exception instanceof UnknownHostException) { // Unknown host return false; } if (exception instanceof ConnectTimeoutException) { // Connection refused return false; } if (exception instanceof SSLException) { // SSL handshake exception return false; } HttpClientContext clientContext = HttpClientContext.adapt(context); HttpRequest request = clientContext.getRequest(); boolean idempotent = !(request instanceof HttpEntityEnclosingRequest); if (idempotent) { // Retry if the request is considered idempotent return true; } return false; }};CloseableHttpClient httpclient = HttpClients.custom().setRetryHandler(myRetryHandler).build();
自定义retry实现,这比较灵活,可以根据异常自定义retry机制以及重试次数,并且可以拿到返回信息,如下,是我定义的httpclient pool
public static CloseableHttpClient getHttpClient() { HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() { public boolean retryRequest( IOException exception, int executionCount, HttpContext context) { if (executionCount >= 5) { // Do not retry if over max retry count return false; } if (exception instanceof InterruptedIOException) { // Timeout return false; } if (exception instanceof UnknownHostException) { // Unknown host return false; } if (exception instanceof ConnectTimeoutException) { // Connection refused return false; } if (exception instanceof SSLException) { // SSL handshake exception return false; } HttpClientContext clientContext = HttpClientContext.adapt(context); HttpRequest request = clientContext.getRequest(); boolean idempotent = !(request instanceof HttpEntityEnclosingRequest); if (idempotent) { // Retry if the request is considered idempotent return true; } return false; } }; PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(MAX_TOTAL); cm.setDefaultMaxPerRoute(MAX_PERROUTE); CloseableHttpClient httpClient = HttpClients .custom() .setRetryHandler(retryHandler) .setConnectionManager(cm) .build(); return httpClient;}
如下是我的测试代码
@Testpublic void test6(){ HttpPost httpPost=new HttpPost("http://127.0.0.1:8080/testjobs1"); try { rsp=httpClient.execute(httpPost); log.info(">> {}",rsp.getStatusLine().getStatusCode()); } catch (Exception e) { log.error(e.getMessage(),e); }finally{ HttpUtil.close(rsp); }}
这种方案,可以实现retry,并且可以根据我的需求进行retry,如:retry count,但是就不能控制retry时间的间隔,也只好放弃了,继续寻找找到了下面这个ServiceUnavailableRetryStrategy
。
ServiceUnavailableRetryStrategy
可以实现,满足需求,这具有HttpRequestRetryHandler
的所有有点,并且可以自定义retry时间的间隔,如下,是我定义的httpclient pool,
public static CloseableHttpClient getHttpClient() {ServiceUnavailableRetryStrategy serviceUnavailableRetryStrategy = new ServiceUnavailableRetryStrategy() { /** * retry逻辑 */ @Override public boolean retryRequest(HttpResponse response, int executionCount, HttpContext context) { return executionCount <= 3; } /** * retry间隔时间 */ @Override public long getRetryInterval() { return 2000L; } }; PoolingHttpClientConnectionManager C_M = new PoolingHttpClientConnectionManager(); //设置整个连接池最大连接数 C_M.setMaxTotal(250); //是路由的默认最大连接(该值默认为2),限制数量实际使用DefaultMaxPerRoute并非MaxTotal。 //设置过小无法支持大并发(ConnectionPoolTimeoutException: Timeout waiting for connection from pool),路由是对maxTotal的细分。 C_M.setDefaultMaxPerRoute(50); return HttpClients.custom().setServiceUnavailableRetryStrategy(serviceUnavailableRetryStrategy).setConnectionManager(C_M).build();}
如下是我的测试代码
@Testpublic void test6(){ HttpPost httpPost=new HttpPost("http://127.0.0.1:8080/testjobs1"); try { rsp=httpClient.execute(httpPost); log.info(">> {}",rsp.getStatusLine().getStatusCode()); } catch (Exception e) { log.error(e.getMessage(),e); }finally{ HttpUtil.close(rsp); }}
- 总结-HttpClient-RetryHandler重试
- HttpClient-RetryHandler重试
- ranong项目总结-HttpClient-RetryHandler重试(一)
- HttpClient通过RetryHandler实现超时重试
- 实现HttpClient重试
- java 重试机制总结
- Android 网络连接-重试机制-HttpRequestRetryHandler(HttpClient)
- Httpclient 设置读取超时及连接重试等配置
- HTTPCLIENT总结
- httpclient总结
- HttpClient总结
- HttpClient总结
- HttpClient总结
- HttpClient总结
- Spring Cloud各组件重试总结
- hbase总结:hbase client访问的超时时间、重试次数、重试间隔时间的配置
- 关于HttpClient的总结
- HttpClient使用总结
- 三大方法教你使用智能扫地机器人
- Java遍历包中所有类
- JSP中的EL表达式
- Struts2中的ModelDriven机制及其运用
- 修改线程的名字,添加线程的动作线程的动作
- 总结-HttpClient-RetryHandler重试
- PHP的几种排序算法的比较
- 启动浏览器步骤及其驱动
- 【hdu 1874】畅通工程续(注意重复路径的问题)
- 我的第一个Kotlin代码
- iOS 手机照片上传服务器方向不对的原因以及解决方法
- 嵌入式从业者应注意的问题
- CNN简介
- node.js 安装手册