Add support for fallback factories in feign client annotation. (#1373)

Adds `@FeignClient.fallbackFactory` to define a `feign.hystrix.FallbackFactory`.

fixes gh-1117
This commit is contained in:
bpicode
2016-10-05 02:46:33 +02:00
committed by Spencer Gibb
parent 0adbd5662f
commit 8e18c3d3b6
7 changed files with 242 additions and 31 deletions

View File

@@ -1012,6 +1012,30 @@ static class HystrixClientFallback implements HystrixClient {
}
----
If one needs access to the cause that made the fallback trigger, one can use the `fallbackFactory` attribute inside `@FeignClient`.
[source,java,indent=0]
----
@FeignClient(name = "hello", fallbackFactory = HystrixClientFallbackFactory.class)
protected interface HystrixClient {
@RequestMapping(method = RequestMethod.GET, value = "/hello")
Hello iFailSometimes();
}
@Component
static class HystrixClientFallbackFactory implements FallbackFactory<HystrixClient> {
@Override
public HystrixClient create(Throwable cause) {
return new HystrixClientWithFallBackFactory() {
@Override
public Hello iFailSometimes() {
return new Hello("fallback; reason was: " + cause.getMessage());
}
};
}
}
----
WARNING: There is a limitation with the implementation of fallbacks in Feign and how Hystrix fallbacks work. Fallbacks are currently not supported for methods that return `com.netflix.hystrix.HystrixCommand` and `rx.Observable`.
[[spring-cloud-feign-inheritance]]