Skip to content

Commit

Permalink
Remove deprecated ZuulFallbackProvider in favor of FallbackProvider. …
Browse files Browse the repository at this point in the history
…Add route parameter to fallbackResponse. Fixes #2655 (#2664)
  • Loading branch information
ryanjbaxter committed Jan 18, 2018
1 parent 7b7cf7e commit 6bb5830
Show file tree
Hide file tree
Showing 20 changed files with 118 additions and 231 deletions.
92 changes: 19 additions & 73 deletions docs/src/main/asciidoc/spring-cloud-netflix.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.cloud.netflix.zuul.filters.route.RestClientRibbonCommandFactory;
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandFactory;
import org.springframework.cloud.netflix.zuul.filters.route.ZuulFallbackProvider;
import org.springframework.cloud.netflix.zuul.filters.route.apache.HttpClientRibbonCommandFactory;
import org.springframework.cloud.netflix.zuul.filters.route.okhttp.OkHttpRibbonCommandFactory;
import org.springframework.context.annotation.Bean;
Expand All @@ -51,7 +51,7 @@ public class RibbonCommandFactoryConfiguration {
protected static class RestClientRibbonConfiguration {

@Autowired(required = false)
private Set<ZuulFallbackProvider> zuulFallbackProviders = Collections.emptySet();
private Set<FallbackProvider> zuulFallbackProviders = Collections.emptySet();

@Bean
@ConditionalOnMissingBean
Expand All @@ -68,7 +68,7 @@ public RibbonCommandFactory<?> ribbonCommandFactory(
protected static class OkHttpRibbonConfiguration {

@Autowired(required = false)
private Set<ZuulFallbackProvider> zuulFallbackProviders = Collections.emptySet();
private Set<FallbackProvider> zuulFallbackProviders = Collections.emptySet();

@Bean
@ConditionalOnMissingBean
Expand All @@ -84,7 +84,7 @@ public RibbonCommandFactory<?> ribbonCommandFactory(
protected static class HttpClientRibbonConfiguration {

@Autowired(required = false)
private Set<ZuulFallbackProvider> zuulFallbackProviders = Collections.emptySet();
private Set<FallbackProvider> zuulFallbackProviders = Collections.emptySet();

@Bean
@ConditionalOnMissingBean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,25 @@
import org.springframework.http.client.ClientHttpResponse;

/**
* Extension of {@link ZuulFallbackProvider} which adds possibility to choose proper response
* based on the exception which caused the main method to fail.
* Provides fallback when a failure occurs on a route.
*
* @author Ryan Baxter
* @author Dominik Mostek
*/
public interface FallbackProvider extends ZuulFallbackProvider {
public interface FallbackProvider {

/**
* The route this fallback will be used for.
* @return The route the fallback will be used for.
*/
public String getRoute();

/**
* Provides a fallback response based on the cause of the failed execution.
*
* @param cause cause of the main method failure
* @param route The route the fallback is for
* @param cause cause of the main method failure, may be <code>null</code>
* @return the fallback response
*/
ClientHttpResponse fallbackResponse(Throwable cause);
ClientHttpResponse fallbackResponse(String route, Throwable cause);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ public RestClientRibbonCommand(String commandKey, RestClient client,

public RestClientRibbonCommand(String commandKey, RestClient client,
RibbonCommandContext context, ZuulProperties zuulProperties,
ZuulFallbackProvider zuulFallbackProvider) {
FallbackProvider zuulFallbackProvider) {
super(commandKey, client, context, zuulProperties, zuulFallbackProvider);
}

public RestClientRibbonCommand(String commandKey, RestClient client,
RibbonCommandContext context, ZuulProperties zuulProperties,
ZuulFallbackProvider zuulFallbackProvider, IClientConfig config) {
FallbackProvider zuulFallbackProvider, IClientConfig config) {
super(commandKey, client, context, zuulProperties, zuulFallbackProvider, config);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public class RestClientRibbonCommandFactory extends AbstractRibbonCommandFactory
private ZuulProperties zuulProperties;

public RestClientRibbonCommandFactory(SpringClientFactory clientFactory) {
this(clientFactory, new ZuulProperties(), Collections.<ZuulFallbackProvider>emptySet());
this(clientFactory, new ZuulProperties(), Collections.<FallbackProvider>emptySet());
}

public RestClientRibbonCommandFactory(SpringClientFactory clientFactory,
ZuulProperties zuulProperties,
Set<ZuulFallbackProvider> zuulFallbackProviders) {
Set<FallbackProvider> zuulFallbackProviders) {
super(zuulFallbackProviders);
this.clientFactory = clientFactory;
this.zuulProperties = zuulProperties;
Expand All @@ -54,7 +54,7 @@ public RestClientRibbonCommandFactory(SpringClientFactory clientFactory,
@SuppressWarnings("deprecation")
public RestClientRibbonCommand create(RibbonCommandContext context) {
String serviceId = context.getServiceId();
ZuulFallbackProvider fallbackProvider = getFallbackProvider(serviceId);
FallbackProvider fallbackProvider = getFallbackProvider(serviceId);
RestClient restClient = this.clientFactory.getClient(serviceId,
RestClient.class);
return new RestClientRibbonCommand(context.getServiceId(), restClient, context,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.springframework.cloud.netflix.ribbon.apache.RibbonLoadBalancingHttpClient;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.ribbon.support.RibbonCommandContext;
import org.springframework.cloud.netflix.zuul.filters.route.ZuulFallbackProvider;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.cloud.netflix.zuul.filters.route.support.AbstractRibbonCommand;
import com.netflix.client.config.IClientConfig;

Expand All @@ -43,15 +43,15 @@ public HttpClientRibbonCommand(final String commandKey,
final RibbonLoadBalancingHttpClient client,
final RibbonCommandContext context,
final ZuulProperties zuulProperties,
final ZuulFallbackProvider zuulFallbackProvider) {
final FallbackProvider zuulFallbackProvider) {
super(commandKey, client, context, zuulProperties, zuulFallbackProvider);
}

public HttpClientRibbonCommand(final String commandKey,
final RibbonLoadBalancingHttpClient client,
final RibbonCommandContext context,
final ZuulProperties zuulProperties,
final ZuulFallbackProvider zuulFallbackProvider,
final FallbackProvider zuulFallbackProvider,
final IClientConfig config) {
super(commandKey, client, context, zuulProperties, zuulFallbackProvider, config);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.netflix.ribbon.apache.RibbonLoadBalancingHttpClient;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.cloud.netflix.zuul.filters.route.support.AbstractRibbonCommandFactory;
import org.springframework.cloud.netflix.ribbon.support.RibbonCommandContext;
import org.springframework.cloud.netflix.zuul.filters.route.ZuulFallbackProvider;

/**
* @author Christian Lohmann
Expand All @@ -37,19 +37,19 @@ public class HttpClientRibbonCommandFactory extends AbstractRibbonCommandFactory
private final ZuulProperties zuulProperties;

public HttpClientRibbonCommandFactory(SpringClientFactory clientFactory, ZuulProperties zuulProperties) {
this(clientFactory, zuulProperties, Collections.<ZuulFallbackProvider>emptySet());
this(clientFactory, zuulProperties, Collections.<FallbackProvider>emptySet());
}

public HttpClientRibbonCommandFactory(SpringClientFactory clientFactory, ZuulProperties zuulProperties,
Set<ZuulFallbackProvider> fallbackProviders) {
Set<FallbackProvider> fallbackProviders) {
super(fallbackProviders);
this.clientFactory = clientFactory;
this.zuulProperties = zuulProperties;
}

@Override
public HttpClientRibbonCommand create(final RibbonCommandContext context) {
ZuulFallbackProvider zuulFallbackProvider = getFallbackProvider(context.getServiceId());
FallbackProvider zuulFallbackProvider = getFallbackProvider(context.getServiceId());
final String serviceId = context.getServiceId();
final RibbonLoadBalancingHttpClient client = this.clientFactory.getClient(
serviceId, RibbonLoadBalancingHttpClient.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.springframework.cloud.netflix.ribbon.okhttp.OkHttpRibbonResponse;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.ribbon.support.RibbonCommandContext;
import org.springframework.cloud.netflix.zuul.filters.route.ZuulFallbackProvider;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.cloud.netflix.zuul.filters.route.support.AbstractRibbonCommand;
import com.netflix.client.config.IClientConfig;

Expand All @@ -43,15 +43,15 @@ public OkHttpRibbonCommand(final String commandKey,
final OkHttpLoadBalancingClient client,
final RibbonCommandContext context,
final ZuulProperties zuulProperties,
final ZuulFallbackProvider zuulFallbackProvider) {
final FallbackProvider zuulFallbackProvider) {
super(commandKey, client, context, zuulProperties, zuulFallbackProvider);
}

public OkHttpRibbonCommand(final String commandKey,
final OkHttpLoadBalancingClient client,
final RibbonCommandContext context,
final ZuulProperties zuulProperties,
final ZuulFallbackProvider zuulFallbackProvider,
final FallbackProvider zuulFallbackProvider,
final IClientConfig config) {
super(commandKey, client, context, zuulProperties, zuulFallbackProvider, config);
}
Expand Down
Loading

0 comments on commit 6bb5830

Please sign in to comment.