SpringCloud重试机制配置详解

网友投稿 256 2023-02-09


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);

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring重试支持Spring Retry的方法
下一篇:详解如何用babel转换es6的class语法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~