Providing Hystrix fallback

We may provide a fallback response for every single route defined in Zuul's configuration, in case a circuit is opened. To do that, we should create a bean of type ZuulFallbackProvider (which is currently deprecated) or FallbackProvider. Inside that implementation, we have to specify the route ID pattern to match all the routes that should be handled by the fallback bean. The second step is to return the implementation of the ClientHttpResponse interface as a response in the fallbackResponse method.

Here's a simple fallback bean that maps every exception to the HTTP status 200 OK and sets the errorCode and errorMessage in the JSON response. Fallback is only executed for the account-service route:

public class AccountFallbackProvider ...

Get Mastering Spring Cloud now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.