Mastering Feign Timeout Configuration: From Builder to Ribbon and Hystrix
This article explains how to configure Feign's timeout settings in various scenarios—including standalone usage, Spring Cloud integration, Ribbon, and Hystrix—detailing builder options, method‑level parameters, bean declarations, and configuration‑file approaches while highlighting priority rules and potential pitfalls.
Hello everyone, I'm San You.
Today we discuss how to set Feign timeout, which varies depending on usage and may have pitfalls.
Prerequisite Knowledge
Brief overview of Feign: a declarative HTTP client that generates dynamic proxy objects from annotated interfaces, handling request assembly and execution.
Feign Purpose
When many third‑party HTTP calls are needed, writing raw HttpClient code becomes repetitive; Feign simplifies this by allowing you to declare an interface with annotations.
Feign Core Components
The Feign.Builder creates a dynamic proxy; the Client interface executes HTTP requests. Its Response execute(Request request, Options options) method receives a Request (URL, method, headers, body) and an Options object that holds timeout settings.
In default mode the timeout values come from the Options passed to the client; if none are provided, Feign falls back to Ribbon’s configuration.
Setting Timeout When Using Feign Alone
1. Via Feign.Builder
Configure the options property of the builder:
<code>Feign.builder()
.options(new Request.Options(5, TimeUnit.SECONDS, 5, TimeUnit.SECONDS, true))
.target(UserApi.class, "http://localhost:8088");</code>2. Via Method Parameter
Add an Options parameter to the interface method and pass it when invoking:
<code>@RequestLine("GET /user/{userId}")
User queryUser(@Param("userId") Integer userId, Request.Options options);</code>Calling:
<code>User user = client.queryUser(123, new Request.Options(3, TimeUnit.SECONDS, 3, TimeUnit.SECONDS, true));</code>Feign in Spring Cloud
When Feign is used inside Spring Cloud, you can still set timeouts via the builder, but Spring creates the builder for you. Two convenient ways are provided:
1. Declare an Options bean
<code>@Configuration
public class FeignConfiguration {
@Bean
public Request.Options options() {
return new Request.Options(8, TimeUnit.SECONDS, 8, TimeUnit.SECONDS, true);
}
}</code>2. Configure in application.yml
<code>feign:
client:
config:
default:
connectTimeout: 10000
readTimeout: 10000</code>Configuration‑file settings have higher priority than the bean.
Feign with Ribbon
If Ribbon is used, its ConnectTimeout and ReadTimeout become Feign’s defaults (1 s by default). You can override them in application.yml:
<code>ribbon:
ConnectTimeout: 5000
ReadTimeout: 5000</code>Timeout priority order: method parameter > Feign config file > Options bean > Ribbon config.
Feign with Hystrix
When Hystrix protects a Feign call, its own timeout (default 1 s) may trigger fallback before Feign’s timeout. Increase Hystrix’s timeout, e.g.:
<code>hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 30000</code>Ensure Hystrix timeout ≥ (connect + read) × retry count.
Summary
Standalone Feign: set timeout via Feign.Builder or method‑level Options .
Spring Cloud Feign: method‑level Options , configuration file, or Options bean.
Feign + Ribbon: configure Ribbon’s timeout properties.
Feign + Hystrix: enlarge Hystrix timeout to exceed Feign’s combined timeout.
Sanyou's Java Diary
Passionate about technology, though not great at solving problems; eager to share, never tire of learning!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.