SpringCloud重试机制配置

SpringCloud重试retry是一个很赞的功能,能够有效的处理单点故障的问题。主要功能是当请求一个服务的某个实例时,譬如你的User服务启动了2个,它们都在eureka里注册了,那么正常情况下当请求User服务时,ribbon默认会轮询这两个实例。此时如果其中一个实例故障了,发生了宕机或者超时等,如果没有配置启用重试retry策略,那么调用方就会得到错误信息或者超时无响应或者是熔断返回的信息。我们希望的自然是一个故障了,会自动切换到另一个去访问。

最简单的方法就是retry。

需要先在pom.xml里加入

  1.  
    <dependency>
  2.  
    <groupId>org.springframework.retry</groupId>
  3.  
    <artifactId>spring-retry</artifactId>
  4.  
    </dependency>

ribbon、zuul、feign都可以配置各自的retry方式。

1 ribbon配置如下

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

2 zuul配置如下

zuul的重试比较简单,不需要任何代码,直接在yml里配置即可。
注意,配置时,ribbon开头的在yml里是不给提示的,不要以为不提示就是没效果,其实是可以用的。
这个ReadTimeout和ConnectTimeout区别是很大的,ConnectTimeout是指建立连接的时间,如果目标服务宕机或网络故障,那么响应的就是ConnectTimeout,无法连接。而ReadTimeout则是连接建立后,等待目标服务返回响应的时间,譬如目标服务做了一个复杂操作导致耗时较长,那么会触发ReadTimeout。
譬如zuul路由了/user路径到user服务上,如果User1实例宕机了,那么配置了retry的zuul就会在重试MaxAutoRetries次数后,切换到另一个实例User2上。如果User2也故障了,那么返回404.
retryableStatusCodes里面有几个错误码,意思就是遇到哪些错误码时触发重试。默认是404,我多配了几个,仅供参考。

3 feign配置如下

feign默认是通过自己包下的Retryer进行重试配置,默认是5次
  1.  
    import static java.util.concurrent.TimeUnit.SECONDS;
  2.  
     
  3.  
    /**
  4.  
    * Cloned for each invocation to {@link Client#execute(Request, feign.Request.Options)}.
  5.  
    * Implementations may keep state to determine if retry operations should continue or not.
  6.  
    */
  7.  
    public interface Retryer extends Cloneable {
  8.  
     
  9.  
    /**
  10.  
    * if retry is permitted, return (possibly after sleeping). Otherwise propagate the exception.
  11.  
    */
  12.  
    void continueOrPropagate(RetryableException e);
  13.  
     
  14.  
    Retryer clone();
  15.  
     
  16.  
    public static class Default implements Retryer {
  17.  
     
  18.  
    private final int maxAttempts;
  19.  
    private final long period;
  20.  
    private final long maxPeriod;
  21.  
    int attempt;
  22.  
    long sleptForMillis;
  23.  
     
  24.  
    public Default() {
  25.  
    this(100, SECONDS.toMillis(1), 5);
  26.  
    }
  27.  
     
  28.  
    public Default(long period, long maxPeriod, int maxAttempts) {
  29.  
    this.period = period;
  30.  
    this.maxPeriod = maxPeriod;
  31.  
    this.maxAttempts = maxAttempts;
  32.  
    this.attempt = 1;
  33.  
    }
  34.  
     
feign取消重试
  1.  
    @Bean
  2.  
    Retryer feignRetryer() {
  3.  
    return Retryer.NEVER_RETRY;
  4.  
    }
feign请求超时设置
  1.  
    @Bean
  2.  
    Request. 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.  
    }
参考:http://www.cnblogs.com/zhangjianbin/p/7228606.html

猜你喜欢

转载自www.cnblogs.com/dauber/p/9316750.html