2

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.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
snmaddula
  • 1,111
  • 1
  • 7
  • 21

1 Answers1

1

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.

narendra-choudhary
  • 4,582
  • 4
  • 38
  • 58
  • If you do not wish to retry after failure on current server, you can set `MaxAutoRetries` to 0. – narendra-choudhary Dec 24 '18 at 21:10
  • I tried this approach, and it works fine when both the instances are up and running. When I bring down one instance, I see the exception message `com.netflix.zuul.exception.ZuulException: Forwarding error`popping up intermittently. I have tried adding connect timeout, read timeout and similar configuration properties, but the exception always pops up when the instance is down and then redirects to the next instance in the `listOfServers`. Is there anything missing that we need to add to skip this exception scenario. – snmaddula Dec 26 '18 at 16:25
  • 1
    Yes, this is a issue in Zuul. Zuul throws error when it can't find the upstream service. So, you need to extend `ErrorFilter` and provide custom logic to return `404` or `503` status code. This is probably the same issue: https://github.com/spring-cloud/spring-cloud-netflix/issues/406 – narendra-choudhary Dec 26 '18 at 16:38
  • check this for how to customise `ZuulException` handling: https://stackoverflow.com/questions/36461493/customizing-zuul-exception – narendra-choudhary Dec 26 '18 at 16:42
  • Is there a way to redirect the request to other server in the `listOfServers` instead of returning `404` or `503`? What is the purpose of `listOfServers` when the fallback doesn't take place as expected. – snmaddula Dec 26 '18 at 17:46
  • 1
    Spring Retry must be added to classpath for retry functionality. Is it added in the classpath? If it is added, then Zuul should retry on 404/503. – narendra-choudhary Dec 26 '18 at 18:06
  • No. spring-retry is not added to the classpath. Will add it and test the same and update to you. – snmaddula Dec 26 '18 at 18:20
  • It works as expected after adding spring-retry as you suggested. Thank you so much. – snmaddula Dec 26 '18 at 18:27