Spring Cloud Ribbon retry mechanism

Source: Internet
Author: User
Preface

In a cluster environment, multiple nodes to provide services, there will inevitably be a node failure

Use Nginx to do load balancing, if your application is stateless, can be rolled out, that is, a platform to restart the application

This actually affects the user relatively little, because Nginx forwards the request to another instance after the forwarding request has failed Zuul

When we built the API gateway with Zuul, we found that the service in Eureka was hung up because the ribbon default load mechanism was polling

If a service is offline, Eureka will not immediately clear the information, unlike zookeeper, based on watcher can monitor the offline real-time

So when the service that is hung in the Eureka is not emptied, the Zuul forwards to the failed machine, causing the request to fail

Of course this will not last for a long time, when the continuous failure hystrix will be open, even if there is a failure, I think it is intolerable

So we need to have a retry mechanism like Nginx to ensure the success of the request, even if it delays a hundreds of millisecond response to the consumer

In Zuul we can configure the Ribbon retry mechanism to implement and must rely on a Spring retry

Official Document Address: http://cloud.spring.io/spring-cloud-static/Dalston.SR4/multi/multi_netflix-metrics.html

The specific configuration is as follows: pom.xml

<!--retry mechanism, must match, otherwise retry does not take effect-->
<dependency>
    <groupid>org.springframework.retry</groupid >
    <artifactId>spring-retry</artifactId>
</dependency>
application.properties
# The timeout for Hystrix must be greater than the Ribbon time-out period
hystrix.command.default.execution.isolation.thread.timeoutinmilliseconds= 10000
# turn
on retry zuul.retryable=true
spring.cloud.loadbalancer.retry.enabled=true
# Request Connection Timeout
ribbon.connecttimeout=2000
# Request processing Timeout
ribbon.readtimeout=5000
# number
of retries for the current instance Ribbon.maxautoretries=1
# Toggle Retry Count of Instance
ribbon.maxautoretriesnextserver=3
# retry
all operation requests Ribbon.oktoretryonalloperations=true

To be effective in Zuul in addition to relying on spring-retry, you need to configure Zuul.retryable=true test steps the same service Registration 2 to Eureka start Zuul Gateway Access API Stop a service continue access API

The specific code can refer to my github:

Https://github.com/yinjihuan/spring-cloud

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.