Spring Cloud Retry Mechanism and retry Summary of various components, springcloud

Source: Internet
Author: User

Spring Cloud Retry Mechanism and retry Summary of various components, springcloud

SpringCloud Retry Mechanism Configuration

First, we declare that the retry here is not a retry after an error is reported. Instead, the Server Load balancer client will try another instance after it finds that the remote request instance is not reachable.

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

Feign Retry Mechanism

By default, feign uses the Retryer in its own package to retry configuration. The default value is 5 times.

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 cancel retry

@BeanRetryer feignRetryer() {return Retryer.NEVER_RETRY;}

Feign request timeout settings

@BeanRequest.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);}

Retry each component in Spring Cloud

Recently, many children's shoes asked me how to configure Spring Cloud xxx component retry. This article provides a summary.

The Retry Mechanism in Spring Cloud should be said to be chaotic. There are some differences between different versions and the implementation is not the same. Fortunately, after Spring Cloud Camden, the Retry Mechanism has basically stabilized, some improvements have been made in Dalston, and the details are not listed yet.

Next we will discuss in detail.

The version used by the author is Spring Cloud Dalston SR4, which is also applicable to Edgware and later versions. For earlier Dalston versions, we will not discuss this article. You can study it on your own.

Ribbon + RestTemplate retry

For RestTemplate that integrates Ribbon, for example, a RestTemplate adds the @ LoadBalanced annotation:

@Bean@LoadBalancedpublic RestTemplate restTemplate() { SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory(); simpleClientHttpRequestFactory.setConnectTimeout(1000); simpleClientHttpRequestFactory.setReadTimeout(1000); return new RestTemplate(simpleClientHttpRequestFactory);}

On this basis, you can try again by using the following Configuration:

Spring: cloud: loadbalancer: retry: enabled: trueribbon: # maximum number of Retries for the same instance, excluding the maximum number of Retries for the first call of MaxAutoRetries: 1 # maximum number of Retries for other instances, does not include the first selected server MaxAutoRetriesNextServer: 2 # whether all operations are retried OkToRetryOnAllOperations: false

Feign retry

Feign itself also has the retry capability. In the early Spring Cloud, Feign usedfeign.Retryer.Default#Default(), Retry 5 times. However, Feign integrates Ribbon and Ribbon has the ability to retry. In this case, behavior confusion may occur.

Spring Cloud realized this problem, so it improved and changed the retry of Feignfeign.Retryer#NEVER_RETRYTo use Feign retry, you only need to use Ribbon retry configuration. Therefore, for Camden and later versions, you can use the following attributes to configure Feign retry:

ribbon: MaxAutoRetries: 1 MaxAutoRetriesNextServer: 2 OkToRetryOnAllOperations: false

Refer to: https://github.com/spring-cloud/spring-cloud-netflix/issues/467 for related Issue

Zuul retry

Configuration:

Zuul: # enable Zuul retry retryable: trueribbon: MaxAutoRetries: 1 MaxAutoRetriesNextServer: 2 OkToRetryOnAllOperations: false

We usezuul.retryable=true You have enabled global retry for Zuul. In fact, you can also enable/disable retry for the specified route:

zuul.routes.<routename>.retryable=true

The local configuration has a higher priority.

Retry Based on HTTP response code

clientName: ribbon:  retryableStatusCodes: 404,502

Note:

The time-out period of Hystrix must be greater than the time-out period. Otherwise, once Hystrix times out, it cannot be retried.

Generallyribbon.OkToRetryOnAllOperations Set to true. Once this configuration is enabled, it means to retry any operations, including POST requests. Because the Request body is cached, this may affect server resources.

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.