Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set custom Feign client connection timeout?

I have Spring Boot application with this Gradle dependencies:

compile("org.springframework.cloud:spring-cloud-starter-eureka")
compile("org.springframework.cloud:spring-cloud-starter-feign")
compile("org.springframework.cloud:spring-cloud-starter-ribbon")
compile("org.springframework.cloud:spring-cloud-starter-hystrix")
compile("org.springframework.cloud:spring-cloud-starter-config")

Also I have Feign client:

@FeignClient(name = "client")
public interface FeignService {

    @RequestMapping(value = "/path", method = GET)
    String response();

}

My application.properties:

client.ribbon.listOfServers = http://localhost:8081
ribbon.eureka.enabled=false

When query time is more than 1 second I get exception:

com.netflix.hystrix.exception.HystrixRuntimeException: response timed-out and no fallback available.

So my question is: how can I set custom Feign client connection timeout? For example to 2 seconds.

like image 389
Roman Cherepanov Avatar asked Jan 04 '17 17:01

Roman Cherepanov


People also ask

How do I set timeout in feign client?

Feign Client is pretty configurable. In terms of a timeout, it allows us to configure both read and connection timeouts. Connection timeout is the time needed for the TCP handshake, while the read timeout needed to read data from the socket. Connection and read timeouts are by default 10 and 60 seconds, respectively.

What is the difference between connection timeout and read timeout?

The connection timeout is the timeout in making the initial connection; i.e. completing the TCP connection handshake. The read timeout is the timeout on waiting to read data1.

What is read timeout?

From the client side, the “read timed out” error happens if the server is taking longer to respond and send information. This could be due to a slow internet connection, or the host could be offline. From the server side, it happens when the server takes a long time to read data compared to the preset timeout.

What is @FeignClient in spring boot?

Feign is a declarative web service client. It makes writing web service clients easier. To use Feign create an interface and annotate it. It has pluggable annotation support including Feign annotations and JAX-RS annotations. Feign also supports pluggable encoders and decoders.


2 Answers

I solved my problem using this answer on question: Hystrix command fails with “timed-out and no fallback available”.

I added hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=4000 to my application.properties to set custom timeout.

like image 118
Roman Cherepanov Avatar answered Oct 11 '22 11:10

Roman Cherepanov


You can be configured timeout using configuration properties on application.yaml file:

feign:
  client:
    config:
      default:
        connectTimeout: 5000
        readTimeout: 5000

Notice that this will change your default feign configuration, if you want to update the timeouts just for your client replace default with the name configured in @FeignClient in your case it will be client, another thing is that you must specify both connectTimeout and readTimeout for this to take effect.

For more detail see this: documentation

like image 27
Francesco Lo Cascio Avatar answered Oct 11 '22 12:10

Francesco Lo Cascio