How to enable retry for selected methods in OpenFeign

Mohammed Hewedy
2 min readMay 30, 2023
Photo by Egor Vikhrev on Unsplash

In OpenFeign you can enable retry by adding a bean of type `Retryer` to your configuration class (in the case of spring). But this configuration will affect ALL the methods in the feign interface.

So to control which methods are retriable and which are not, I’ve created one annotation `FeignRetryable`.

Meet FeignRetryable

It works simply by accepting a delegate `Retryer` and applying it when the method is annotated by this annotation.

So you can have one Feign interface containing 5 methods, and only one is retriable because it is annotated by the `@FeignRetryable` and the other methods are not.

As the Javadoc on top of the annotation works, it works by first creating a Retryer bean of type `AnnotationRetryer` that accepts a delegate Retryer (could be new `Retryer.Default()`) and then just placing the annotation `@FeignRetryable` on top of the method you need to retry it on failures.

// inside your Feign Configuration class:
@Bean
public Retryer feignRetryer() {
return new AnnotationRetryer(new Retryer.Default());
}

// the place the annotation on top of the method in the Feign Client interface:
@FeignRetryable
@PostMapping("/api/url")
ResponseDto callTheApi(@RequestBody RequestDto request);

By default, the retry function works in specific cases, one case is when the server returns the header “retry-after”, another case is when the response exceeds the configured read-timeout, or you can throw the `RetryableException` in the `ErrorDecoder`.

This annotation is Feign related, so it works with/without Spring

You can learn more about feign Retryer here https://www.baeldung.com/feign-retry

--

--