-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Configuring a fallback in feign clients #298
Comments
can you explain how this request is different than feign-hystrix? (probably
with code is best). Also there have been many requests around hystrix, so
if you do an issue search and find something similar, this will help too!
|
Hi! I searched the repository for something related and didn't find anything. This is related to an issue I opened in Spring Cloud Netflix repo: I am using their Feign integration through the use of
I have a rest controller that calls this client:
The point is that there is no way of configuring a Hystrix callback for Feign clients. Under the hood feign-hystrix is used:
I have been looking for a way of defining hystrix callback methods in the native HystrixFeignBuilder but it seems there isn't any way to do it. |
@adriancole this if for defining a Hystrix fallback when there are errors. It was intentionally left out because I didn't know how to define a fallback and it wasn't needed to get simple hystrix support working. |
I was wondering about this, too. Maybe something with an @fallback annotation that can be added to methods that points to static methods on an extension of the client interface. public interface FooClient {
void doSomething(String withArg);
}
@FeignClient("foo-client")
public interface FeignFooClient extends FooClient {
@Override
@RequestMapping(method = RequestMethod.GET, value = "/something/{withArg}")
@Fallback("trySomethingElse")
SomethingResponse doSomething(String withArg);
static SomethingResponse trySomethingElse(String withArg) {
...
}
} This assumes you don't own the FooClient interface. If you do own that code, you could put the annotations directly on that interface. |
@dnathanson a static fallback won't be enough for me. I need it to be an instance that is looked up by some mechanism, so it can be properly configured. |
How about a fallback implementation of the interface? public interface FooClient {
void doSomething(String withArg);
}
public class FooFallback implements FooClient {
public void doSomething(String withArg){
System.out.println(withArg);
}
}
@FeignClient(name="foo-client", fallback=FooFallback.class)
public interface FeignFooClient extends FooClient {
@Override
@RequestMapping(method = RequestMethod.GET, value = "/something/{withArg}")
void doSomething(String withArg);
} |
I like that. There would need to be some kind of factory interface to create the impl. Default to new it using reflection, but allow spring or guice to look it up. |
FeignClient is specific to spring Cloud Netflix BTW. |
|
Fallbacks are known values, which you return when there's an error invoking an http method. For example, you can return a cached result as opposed to raising an error to the caller. To use this feature, pass a safe implementation of your target interface as the last parameter to `HystrixFeign.Builder.target`. Here's an example: ```java // When dealing with fallbacks, it is less tedious to keep interfaces small. interface GitHub { @RequestLine("GET /repos/{owner}/{repo}/contributors") List<String> contributors(@param("owner") String owner, @param("repo") String repo); } // This instance will be invoked if there are errors of any kind. GitHub fallback = (owner, repo) -> { if (owner.equals("Netflix") && repo.equals("feign")) { return Arrays.asList("stuarthendren"); // inspired this approach! } else { return Collections.emptyList(); } }; GitHub github = HystrixFeign.builder() ... .target(GitHub.class, "https://api.github.com", fallback); ``` Credit to the idea goes to @stuarthendren! Fixes #298
I like this.when this implementation can we use?
|
Sounds reasonable, just needs to get into the right issues list
https://github.com/spring-cloud/spring-cloud-netflix/issues
|
In fact the FooFallback must implement FeignFooClient interface to make it work. I'm leaving this comment here in case someone follows this as a guide :) |
It would be great if we could configure a Hystrix fallback in Feign clients. Any plans to implement this funcionality?
The text was updated successfully, but these errors were encountered: