SpringCloud重试机制配置

    首先声明一点,这里的重试并不是报错以后的重试,而是负载均衡客户端发现远程请求实例不可到达后,去重试其他实例。

Table 1. ribbon重试配置

ribbon.OkToRetryOnAllOperations

false(`是否所有操作都重试`)

ribbon.MaxAutoRetriesNextServer

2(`重试负载均衡其他的实例最大重试次数,不包括首次server`)

ribbon.MaxAutoRetries

1(`同一台实例最大重试次数,不包括首次调用`)

spring.cloud.loadbalancer.retry.enabled

true(`重试机制开关`)

 

ribbon针对http的readTimeout和connectTimeCount直接用配置 ribbon.ReadTimeout和ribbon.ConnectTimeout是无效的。配置方式参考以下代码

1@Bean 2@LoadBalanced 3RestTemplate restTemplate() { 4 HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(); 5 httpRequestFactory.setReadTimeout(5000); 6 httpRequestFactory.setConnectTimeout(5000); 7 return new RestTemplate(httpRequestFactory); 8}

Table 2. zuul重试配置

zuul.retryable

true(`重试机制开关`)

ribbon.OkToRetryOnAllOperations

false(`是否所有操作都重试`)

ribbon.MaxAutoRetriesNextServer

2(`重试负载均衡其他的实例最大重试次数,不包括首次server`)

ribbon.MaxAutoRetries

1(`同一台实例最大重试次数,不包括首次调用`)

ribbon.ConnectTimeout

6000(`http建立socket超时时间`)

ribbon.ReadTimeout

6000(`http读取响应socket超时时间`)

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds

10000(断路器线程池超时时间,这个值一定要比ribbon超时时间长)

feign重试机制

feign默认是通过自己包下的Retryer进行重试配置,默认是5次

1package feign; 2 3import static java.util.concurrent.TimeUnit.SECONDS; 4 5/** 6 * Cloned for each invocation to {@link Client#execute(Request, feign.Request.Options)}. 7 * Implementations may keep state to determine if retry operations should continue or not. 8 */ 9public interface Retryer extends Cloneable { 10 11 /** 12 * if retry is permitted, return (possibly after sleeping). Otherwise propagate the exception. 13 */ 14 void continueOrPropagate(RetryableException e); 15 16 Retryer clone(); 17 18 public static class Default implements Retryer { 19 20 private final int maxAttempts; 21 private final long period; 22 private final long maxPeriod; 23 int attempt; 24 long sleptForMillis; 25 26 public Default() { 27 this(100, SECONDS.toMillis(1), 5); 28 } 29 30 public Default(long period, long maxPeriod, int maxAttempts) { 31 this.period = period; 32 this.maxPeriod = maxPeriod; 33 this.maxAttempts = maxAttempts; 34 this.attempt = 1; 35 }

feign取消重试

1 @Bean 2 Retryer feignRetryer() { 3 return Retryer.NEVER_RETRY; 4 }

feign请求超时设置

1@Bean 2Request.Options requestOptions(ConfigurableEnvironment env){ 3 int ribbonReadTimeout = env.getProperty("ribbon.ReadTimeout", int.class, 6000); 4 int ribbonConnectionTimeout = env.getProperty("ribbon.ConnectTimeout", int.class, 3000); 5 6 return new Request.Options(ribbonConnectionTimeout, ribbonReadTimeout); 7}

Last updated 2017-05-18 23:39:30 CST

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

springboot整合spring retry 重试机制

当我们调用一个接口可能由于网络等原因造成第一次失败,再去尝试就成功了,这就是重试机制,spring支持重试机制,并且在SpringCloud中可以与Hystaix结合使用,可以避免访问到已经不正常的实例。但是切记非幂等情况下慎用重试一 加入依赖<!重试机制<depen

Kafka设计

1.幂等消息为了解决重试导致的消息重复、乱序问题,kafka引入了幂等消息。幂等消息保证producer在一次会话内写入一个partition内的消息具有幂等性,可以通过重试来确保消息发布的ExactlyOnce语义。实现逻辑很简单:区分producer会话producer每次启动后,首先向broker申请一

Android So动态加载 优雅实现与原理分析

背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我