SpringCloud重试机制配置详解
更新时间:2018年04月06日 11:05:41 作者:张建斌
本篇文章主要介绍了SpringCloud重试机制配置详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
首先声明一点,这里的重试并不是报错以后的重试,而是负载均衡客户端发现远程请求实例不可到达后,去重试其他实例。

@Bean
@LoadBalanced
RestTemplate restTemplate() {
HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
httpRequestFactory.setReadTimeout(5000);
httpRequestFactory.setConnectTimeout(5000);
return new RestTemplate(httpRequestFactory);
}

feign重试机制
feign默认是通过自己包下的Retryer进行重试配置,默认是5次
package feign;
import static java.util.concurrent.TimeUnit.SECONDS;
/**
* Cloned for each invocation to {@link Client#execute(Request, feign.Request.Options)}.
* Implementations may keep state to determine if retry operations should continue or not.
*/
public interface Retryer extends Cloneable {
/**
* if retry is permitted, return (possibly after sleeping). Otherwise propagate the exception.
*/
void continueOrPropagate(RetryableException e);
Retryer clone();
public static class Default implements Retryer {
private final int maxAttempts;
private final long period;
private final long maxPeriod;
int attempt;
long sleptForMillis;
public Default() {
this(100, SECONDS.toMillis(1), 5);
}
public Default(long period, long maxPeriod, int maxAttempts) {
this.period = period;
this.maxPeriod = maxPeriod;
this.maxAttempts = maxAttempts;
this.attempt = 1;
}
feign取消重试
@Bean
Retryer feignRetryer() {
return Retryer.NEVER_RETRY;
}
feign请求超时设置
@Bean
Request.Options requestOptions(ConfigurableEnvironment env){
int ribbonReadTimeout = env.getProperty("ribbon.ReadTimeout", int.class, 6000);
int ribbonConnectionTimeout = env.getProperty("ribbon.ConnectTimeout", int.class, 3000);
return new Request.Options(ribbonConnectionTimeout, ribbonReadTimeout);
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
mybatis-plus 使用Condition拼接Sql语句各方法的用法
这篇文章主要介绍了mybatis-plus 使用Condition拼接Sql语句各方法的用法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-07-07
java List出现All elements are null问题及解决
这篇文章主要介绍了java List出现All elements are null问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-11-11
MyBatis-Plus MetaObjectHandler的原理及使用
MyBatis-Plus的MetaObjectHandler接口允许开发者自动填充实体类字段,如创建时间、更新时间等公共字段,减少代码重复,提高数据一致性和完整性,感兴趣的可以了解一下2024-10-10


最新评论