天天看点

SpringCloud重试retry 20220927

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配置如下

SpringCloud重试retry 20220927
  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配置如下

SpringCloud重试retry 20220927

zuul的重试比较简单,不需要任何代码,直接在yml里配置即可。

注意,配置时,ribbon开头的在yml里是不给提示的,不要以为不提示就是没效果,其实是可以用的。

SpringCloud重试retry 20220927

这个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. * Cloned for each invocation to {@link Client#execute(Request, feign.Request.Options)}.
  4. * Implementations may keep state to determine if retry operations should continue or not.
  5. */
  6. public interface Retryer extends Cloneable {
  7. /**
  8. * if retry is permitted, return (possibly after sleeping). Otherwise propagate the exception.
  9. */
  10. void continueOrPropagate(RetryableException e);
  11. Retryer clone();
  12. public static class Default implements Retryer {
  13. private final int maxAttempts;
  14. private final long period;
  15. private final long maxPeriod;
  16. int attempt;
  17. long sleptForMillis;
  18. public Default() {
  19. this(100, SECONDS.toMillis(1), 5);
  20. }
  21. public Default(long period, long maxPeriod, int maxAttempts) {
  22. this.period = period;
  23. this.maxPeriod = maxPeriod;
  24. this.maxAttempts = maxAttempts;
  25. this.attempt = 1;
  26. }
  1. @Bean
  2. Retryer feignRetryer() {
  3. return Retryer.NEVER_RETRY;
  4. }
  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. return new Request.Options(ribbonConnectionTimeout, ribbonReadTimeout);
  6. }