Remove deprecated ZuulFallbackProvider in favor of FallbackProvider. Add route parameter to fallbackResponse. Fixes #2655 (#2664)
This commit is contained in:
@@ -1943,39 +1943,47 @@ possible filters that are enabled. If you want to disable one, simply set
|
||||
=== Providing Hystrix Fallbacks For Routes
|
||||
|
||||
When a circuit for a given route in Zuul is tripped you can provide a fallback response
|
||||
by creating a bean of type `ZuulFallbackProvider`. Within this bean you need to specify
|
||||
by creating a bean of type `FallbackProvider`. Within this bean you need to specify
|
||||
the route ID the fallback is for and provide a `ClientHttpResponse` to return
|
||||
as a fallback. Here is a very simple `ZuulFallbackProvider` implementation.
|
||||
as a fallback. Here is a very simple `FallbackProvider` implementation.
|
||||
|
||||
[source,java]
|
||||
----
|
||||
class MyFallbackProvider implements ZuulFallbackProvider {
|
||||
class MyFallbackProvider implements FallbackProvider {
|
||||
|
||||
@Override
|
||||
public String getRoute() {
|
||||
return "customers";
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClientHttpResponse fallbackResponse() {
|
||||
public ClientHttpResponse fallbackResponse(String route, final Throwable cause) {
|
||||
if (cause instanceof HystrixTimeoutException) {
|
||||
return response(HttpStatus.GATEWAY_TIMEOUT);
|
||||
} else {
|
||||
return response(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
private ClientHttpResponse response(final HttpStatus status) {
|
||||
return new ClientHttpResponse() {
|
||||
@Override
|
||||
public HttpStatus getStatusCode() throws IOException {
|
||||
return HttpStatus.OK;
|
||||
return status;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRawStatusCode() throws IOException {
|
||||
return 200;
|
||||
return status.value();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getStatusText() throws IOException {
|
||||
return "OK";
|
||||
return status.getReasonPhrase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -2004,18 +2012,18 @@ zuul:
|
||||
----
|
||||
|
||||
If you would like to provide a default fallback for all routes than you can create a bean of
|
||||
type `ZuulFallbackProvider` and have the `getRoute` method return `*` or `null`.
|
||||
type `FallbackProvider` and have the `getRoute` method return `*` or `null`.
|
||||
|
||||
[source,java]
|
||||
----
|
||||
class MyFallbackProvider implements ZuulFallbackProvider {
|
||||
class MyFallbackProvider implements FallbackProvider {
|
||||
@Override
|
||||
public String getRoute() {
|
||||
return "*";
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClientHttpResponse fallbackResponse() {
|
||||
public ClientHttpResponse fallbackResponse(String route, Throwable throwable) {
|
||||
return new ClientHttpResponse() {
|
||||
@Override
|
||||
public HttpStatus getStatusCode() throws IOException {
|
||||
@@ -2053,68 +2061,6 @@ class MyFallbackProvider implements ZuulFallbackProvider {
|
||||
}
|
||||
----
|
||||
|
||||
If you would like to choose the response based on the cause of the failure use `FallbackProvider` which will replace `ZuulFallbackProvder` in future versions.
|
||||
|
||||
[source,java]
|
||||
----
|
||||
class MyFallbackProvider implements FallbackProvider {
|
||||
|
||||
@Override
|
||||
public String getRoute() {
|
||||
return "*";
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClientHttpResponse fallbackResponse(final Throwable cause) {
|
||||
if (cause instanceof HystrixTimeoutException) {
|
||||
return response(HttpStatus.GATEWAY_TIMEOUT);
|
||||
} else {
|
||||
return fallbackResponse();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClientHttpResponse fallbackResponse() {
|
||||
return response(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
private ClientHttpResponse response(final HttpStatus status) {
|
||||
return new ClientHttpResponse() {
|
||||
@Override
|
||||
public HttpStatus getStatusCode() throws IOException {
|
||||
return status;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRawStatusCode() throws IOException {
|
||||
return status.value();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getStatusText() throws IOException {
|
||||
return status.getReasonPhrase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getBody() throws IOException {
|
||||
return new ByteArrayInputStream("fallback".getBytes());
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
return headers;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
=== Zuul Timeouts
|
||||
|
||||
If you want to configure the socket timeouts and read timeouts for requests proxied through
|
||||
|
||||
Reference in New Issue
Block a user