When using Feign Client, you can set the timeout in two ways:
1. Set timeout for the entire Feign Client
The timeout time can be set by modifying the object in the configuration class of Feign Client. The object has two properties, connectTimeoutMillis is used to set the connection timeout, and readTimeoutMillis is used to set the read timeout.
Here is an example:
@Configuration public class FeignClientConfig { @Bean public requestOptions() { return new (5000, 5000); } }
In the example above, the connection timeout and the read timeout are both set to 5000 milliseconds.
Set timeout for a single Feign interface method
You can use the configuration attribute of the @FeignClient annotated on the Feign interface method to specify a configuration class, and then set the timeout by modifying the object in the configuration class.
Here is an example:
@FeignClient(name = "example-client") public interface ExampleClient { @GetMapping("/example") @Headers("Content-Type: application/json") @RequestLine("GET /example") void getExample( options); } //Custom interface timeout time (20 seconds) options =new (20, ,20,,true); // Execute the callexampleClient .getExample(options );
In the above example, the timeout time of the getExample() method in the ExampleClient interface is configured to 20 seconds.
It should be noted that the timeout setting of Feign Client is only valid for the requested connection and read phases, and is invalid for the processing time of the response. If you need to set the timeout for the entire request-response, it can be done by using Hystrix or other means.
Possible reasons for not taking effect
Search for rewrite Options in the project, as shown below:
@Bean public Options options() { return new Options(); }
Options class
public static class Options { private final int connectTimeoutMillis; private final int readTimeoutMillis; public Options(int connectTimeoutMillis, int readTimeoutMillis) { = connectTimeoutMillis; = readTimeoutMillis; } public Options() { this(10000, 60000); } public int connectTimeoutMillis() { return ; } public int readTimeoutMillis() { return ; } }
You can see that the default constructor of the Options class connectTimeout is 10000ms and the readTimeout is 60000ms. If the settings in your configuration file do not take effect, it may be overwritten.
feign: client: config: default: connectTimeout: 5000 readTimeout: 150000
1. We need to force rewrite it to take effect
@Primary @Bean public requestOptions(ConfigurableEnvironment env) { String connectTime = (""); String readTime = (""); if (connectTime != null && readTime != null) { Integer connectTimeout = (connectTime); Integer readTimeout = (readTime); return new (connectTimeout, readTimeout); } return new (); }
2. Set the timeout time for a certain method separately.
@FeignClient(name = "example-client") public interface ExampleClient { @GetMapping("/example") @Headers("Content-Type: application/json") @RequestLine("GET /example") void getExample( options,String params); } //Custom interface timeout time (20 seconds) options =new (20, ,20,,true); // Execute the callexampleClient .getExample(options );
This is the end of this article about the solution to the timeout setting of Feign Client does not take effect. For more related contents of Feign Client timeout setting, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!