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