Search code examples
spring-cloudnetflix-zuulspring-cloud-netflixhystrixapi-gateway

Configuration based fallback for route


There are two instances of an application: instance-1 and instance-2.

Let us assume that

  1. instance-1 is reachable at localhost:8090
  2. instance-2 is reachable at localhost:9080

How do I configure zuul proxy so that --- First visit instance-1 and in case of any exception / failure, switch to instance-2

Note: Not using Eureka

I was able to get it work using hystrix with a facade controller and in the fallback, calling instance-2 via RestTemplate.

But I am looking for some better approach wherein the routing is taken care by Zuul along with mirroring of HTTPHeaders, HttpMethod and other request attributes.

If anyone have tried similar thing, please suggest me.


Solution

  • You can configure Zuul to retry on current and next instance.

    zuul:
      retryable: true
    
    ribbon:
      MaxAutoRetries: 1
      MaxAutoRetriesNextServer: 3
      OkToRetryOnAllOperations: true
    
    yourApplication:
        ribbon:
            listOfServers: localhost:8090, localhost:9080
    

    As per above configuration, if routing to 8090 instance fails Zuul will try one more time to connect to 8090 and if that call also fails, Zuul will route to 9080 for the next call. You can read more about these retry configurations here.