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是无效的。配置方式参考以下代码
@Bean
@LoadBalanced
RestTemplate restTemplate() {
	HttpComponentsClientHttpRequestFactory httpRequestFactory =  new HttpComponentsClientHttpRequestFactory();
	httpRequestFactory.setReadTimeout(5000);
	httpRequestFactory.setConnectTimeout(5000);
	return new RestTemplate(httpRequestFactory);
}

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次

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

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

转载于:https://my.oschina.net/sean1989/blog/904016

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值