Search code examples
springspring-cloudnetflix-zuul

Spring Cloud Zuul fallback service


Is it possible to setup a proxy to a main server/servers/service but when this is not available, redirect requests to a secondary server/servers/service?

The secondary service is not an instance of the same type of the primary one. Their API is compatible but not the same. The secondary is a less-performant last resource when the primary is not available.

We are not using Eureka, only fixed IPs yet.

zuul:
  routes:
    whatever:
      path: /whatever/**
      sensitiveHeaders: Cookie,Set-Cookie
      url: http://server1:8080/whatever

I had a look on ZuulFallbackProvider, but this interface is to provided a fixed response on case of error. I want, when http://server1:8080/whatever is not responding, redirect to http://server2:8080/whateverApi2.

Thanks.


Solution

  • If anyone is trying to do something similar, what actually works is Hystrix instead of Zuul component.

    In the Gateway API, we create a facade controller that respond to the service we want to set the fallback solution:

    @HystrixCommand(fallbackMethod = "fallbackWhatever")
    @PostMapping
    ResponseEntity<Object> whatever(final RequestEntity<?> request) {
        return defaultClient.searchSubmissions(request.getHeaders(), request.getBody());
    }
    
    ResponseEntity<Object> fallbackWhatever(final RequestEntity<?> request) {
        return fallbackClient.searchSubmissions(request.getHeaders(), request.getBody());
    }
    

    defaultClient and fallbackClient are two different FeignClient interfaces, each one pointing to each service endpoint.

    That's it! If you shutdown the main service, the Gateway API starts to call the fallback service without ever returning a single service unavailable and the change takes just a few milliseconds.

    Also, if you turn it back on, it's back responding also just a few milliseconds after being ready.