How to set the connect timeout parameter for FeignClient?
There are two ways to set the connectTimeout parameter in FeignClient.
- The configuration method of Spring Cloud can be used by adding the following settings in either application.yml or application.properties.
feign.client.config.default.connectTimeout=5000
The above code sets the default connectTimeout to 5000 milliseconds.
- Using the @RequestLine annotation on methods in the FeignClient interface:
You can use the @RequestLine annotation on methods in the FeignClient interface and set the connectTimeout parameter using the @Header annotation, for example:
@RequestLine("GET /api/endpoint")
@Headers("connectTimeout: 5000")
ResponseDto getEndpoint();
The above code sets the connectTimeout of this method to 5000 milliseconds.
It is important to note that the connectTimeout parameter represents the connection timeout, meaning that if the target server is not connected within the specified time, a connection timeout exception will be thrown.