1
votes

Ok so I couldn't find any helpful materials on this topic, a big chunk of articles I found had one method that was annotated with @HystrixCommand and had defined a fallback method.

The other solution I found was using @DefaultProperties(defaultFallback = "fallbackMethod") but the problem with this is that the methods need to have compatible return types.

Unfortunately for me in my service I have many methods with completely different signatures and also I need to get hold of the throwable (in docs it is mentioned that you cannot have any parameters for a default fallback method). The methods look something like this:

@Service
@RequiredArgsConstructor
public class MyService {

  private final FeignClient feignClient;

  @Override
  public String methodA(final CustomObjectA o, final String entity) {
    ...
  }


  @Override
  public String methodB(final String collection, final Map<String, Object> requestBody) {
    ...
  }

  @Override
  public String methodC(final String collection, final String id, final Map<String, Object> requestBody) {
    ...
  }
}

And ofc I have more than 3 methods def in the service...

The thing I really want to avoid is making 20 hystrix default fallback methods.

Is there a way where I could def a standard fallback for all methods, no matter what the signatures they have, or am I stuck with defining a fallback method for every single method?

Thanks in advance!!

2

2 Answers

1
votes

You will have to implement a fall back for each method.

However using the FallbackFactory might make this easier and allow each method to call one reusable method.

Maybe you don't really want hystrix fallbacks if they are the same for each method. All try catch might solve the same problem.

1
votes

Let me share the code snippet used in my project.

To call an api like http://www.baidu.com/xxx, you have below steps to follow.

1.Api Definition (fallback = WebServiceApiFallback.class)

@Component
@FeignClient(value = "webServiceApi", configuration = FeignConfiguration.class, fallback = WebServiceApiFallback.class)
public interface WebServiceApi {

  @Headers(value = {"Content-Type: application/json", "Accept-Encoding: gzip,deflate"})
  @GetMapping(value = "/xxx")
  BaseResponse<YourResponse> xxx(YourRequest request);

2.Fallback Definition

@Component
public class WebServiceApiFallback implements WebServiceApi {

  @Override
  public BaseResponse<YourResponse> xxx(YourRequest request) {
    // Your Fallback Code here, when api request failed.
  }

3.api host configuration, maybe application.properties...

webServiceApi.ribbon.listOfServers=http://www.baidu.com

4.use it

  @Autowired
  private WebServiceApi webServiceApi;

For any api, you can just define you request and response, and feign will do the request、 encode、and decode.

[Ref] https://github.com/spring-cloud/spring-cloud-netflix/issues/762