You can add a Retryer in the FeignClientConfig
@Configuration
public class FeignClientConfig {
@Bean
public Retryer retryer() {
return new Custom();
}
}
class Custom implements Retryer {
private final int maxAttempts;
private final long backoff;
int attempt;
public Custom() {
this(2000, 3);
}
public Custom(long backoff, int maxAttempts) {
this.backoff = backoff;
this.maxAttempts = maxAttempts;
this.attempt = 1;
}
public void continueOrPropagate(RetryableException e) {
if (attempt++ >= maxAttempts) {
throw e;
}
try {
Thread.sleep(backoff);
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
}
}
@Override
public Retryer clone() {
return new Custom(backoff, maxAttempts);
}
}
Updated with sample Retryer example config based on the Retryer.Default.
GitHub
github.com › OpenFeign › feign › blob › master › core › src › main › java › feign › Retryer.java
feign/core/src/main/java/feign/Retryer.java at master · OpenFeign/feign
* Cloned for each invocation to {@link Client#execute(Request, feign.Request.Options)}. * Implementations may keep state to determine if retry operations should continue or not. */ public interface Retryer extends Cloneable { · /** * if retry is permitted, return (possibly after sleeping).
Author: OpenFeign
Top answer 1 of 8
23
You can add a Retryer in the FeignClientConfig
@Configuration
public class FeignClientConfig {
@Bean
public Retryer retryer() {
return new Custom();
}
}
class Custom implements Retryer {
private final int maxAttempts;
private final long backoff;
int attempt;
public Custom() {
this(2000, 3);
}
public Custom(long backoff, int maxAttempts) {
this.backoff = backoff;
this.maxAttempts = maxAttempts;
this.attempt = 1;
}
public void continueOrPropagate(RetryableException e) {
if (attempt++ >= maxAttempts) {
throw e;
}
try {
Thread.sleep(backoff);
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
}
}
@Override
public Retryer clone() {
return new Custom(backoff, maxAttempts);
}
}
Updated with sample Retryer example config based on the Retryer.Default.
2 of 8
5
Just new a contructor Default
@Configuration
public class FeignClientConfig {
@Bean
public Retryer retryer() {
return new Retryer.Default(100, 2000, 3);
}
}
Springcloud
springcloud.io › post › 2022-01 › spring-cloud-openfeign-timeout-and-retry
Spring Cloud OpenFeign timeout and retry - Spring Cloud
January 11, 2022 - Implement the feign.Retryer interface · Understanding of the three parameters. period: period, retry interval time · maxPeriod: maximum period, the retry interval time gradually increases according to certain rules, but cannot exceed the maximum period · maxAttempts: the maximum number of attempts, the number of retries ·
Javadoc.io
javadoc.io › doc › io.github.openfeign › feign-core › 9.2.0 › feign › Retryer.html
Retryer - feign-core 9.2.0 javadoc
Latest version of io.github.openfeign:feign-core · https://javadoc.io/doc/io.github.openfeign/feign-core · Current version 9.2.0 · https://javadoc.io/doc/io.github.openfeign/feign-core/9.2.0 · package-list path (used for javadoc generation -link option) https://javadoc.io/doc/io.github.openfeign/feign-core/9.2.0/package-list ·
Tech Me More
diehardtechy.com › 2020 › 05 › openfeign-retry-request-based-on-custom.html
OpenFeign : retry request based on custom HTTP response status - Tech Me More
*/ public FeignRetyer() { this(10000, 5); } public FeignRetyer(long backoff, int maxAttempts) { this.backoff = backoff; this.maxAttempts = maxAttempts; this.attempt = 1; } public void continueOrPropagate(RetryableException e) { if (attempt++ >= maxAttempts) { throw e; } try { TimeUnit.MILLISECONDS.sleep(backoff); } catch (InterruptedException ex) { } log.info("Retrying: " + e.request().url() + " attempt " + attempt); } @Override public Retryer clone() { return new FeignRetyer(backoff, maxAttempts); } } In the above example, we are retrying 5 times before throwing the exception and the interval between the request is 10 seconds.
Javadoc.io
javadoc.io › doc › io.github.openfeign › feign-core › 10.7.0 › feign › Retryer.html
Retryer - feign-core 10.7.0 javadoc
Latest version of io.github.openfeign:feign-core · https://javadoc.io/doc/io.github.openfeign/feign-core · Current version 10.7.0 · https://javadoc.io/doc/io.github.openfeign/feign-core/10.7.0 · package-list path (used for javadoc generation -link option) https://javadoc.io/doc/io.github.openfeign/feign-core/10.7.0/package-list ·
DEV Community
dev.to › sabyasachi › spring-feign-client-3853
Spring Feign Client - DEV Community
August 28, 2022 - public class CustomRetryer extends Retryer.Default{ public CustomRetryer(long period, long maxPeriod, int maxAttempts){ super(period, maxPeriod, maxAttempts); } @Override public void continueOrPropagate(RetryableException e){ log.info("Going to retry for ", e); super.continueOrPropagate(e); } @Override public Retryer clone(){ return new CustomRetryer(5,SECONDS.toMillis(1), 5); } } One important fact is feign Retry works either on IOException or RetryableException thrown from some errorDecoder .
Spring
docs.spring.io › spring-cloud-openfeign › docs › current › reference › html
Spring Cloud OpenFeign
Spring Cloud OpenFeign does not provide the following beans by default for feign, but still looks up beans of these types from the application context to create the feign client: ... A bean of Retryer.NEVER_RETRY with the type Retryer is created by default, which will disable retrying.
Javadoc.io
javadoc.io › static › io.github.openfeign › feign-core › 10.7.0 › feign › Retryer.html
Retryer (Feign Core 10.7.0 API)
feign · All Superinterfaces: Cloneable · All Known Implementing Classes: Retryer.Default · public interface Retryer extends Cloneable · Cloned for each invocation to Client.execute(Request, feign.Request.Options). Implementations may keep state to determine if retry operations should continue or not.
GitHub
github.com › OpenFeign › feign › issues › 1966
Feign retries event if Retryer.NEVER_RETRY is configured in the AsyncFeign.<HttpClientContext>builder() · Issue #1966 · OpenFeign/feign
March 1, 2023 - import static feign.Retryer.NEVER_RETRY; @Bean @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) public AsyncFeign.AsyncBuilder<HttpClientContext> defaultAsyncBuilder(AsyncClient<HttpClientContext> httpClient, Decoder decoder, Encoder encoder, Contract contract, ErrorDecoder errorDecoder, GlobalFeignRequestInterceptor globalFeignRequestInterceptor) { return AsyncFeign.<HttpClientContext>builder() .client(httpClient) .decoder(decoder) .errorDecoder(errorDecoder) .encoder(encoder) .dismiss404() .retryer(NEVER_RETRY) // Disable Feign retries .contract(contract) .requestInterceptor(globalFeignRequestInterceptor); }
Author: OpenFeign
Matez
matez.de › index.php › 2017 › 04 › 12 › exploring-feign-retrying
Exploring Feign – Retrying - matez blog
April 12, 2017 - What most developers don’t know, Feign has a default retryer built-in.
GitConnected
levelup.gitconnected.com › feign-client-retry-and-timeout-configurations-214eaf14a4d1
Feign Client Retry and Timeout configurations | by Ivan Polovyi | Level Up Coding
April 4, 2025 - The Feign client has two configurations that can handle erroneous responses by retrying and giving up on requests that take too long. We…