Today we share with you the timeout and retry configuration of feign.

Timeout

1
2
3
4
5
6
feign:
  client:
    config:
      default:
        connectTimeout: 1000
        readTimeout: 1000

The following points need to be noted.

  1. The connection timeout (connectTimeout) and the read timeout (readTimeout) will take effect when configured at the same time.
  2. The timeout unit is milliseconds.
  3. The timeout can be defined individually according to the service name.

For example, if the provider-get service provides a query interface, the timeout can be set shorter as follows.

1
2
3
4
5
6
feign:
  client:
    config:
      provider-get:
        connectTimeout: 1000
        readTimeout: 6000

The provider-post service provides the data processing interface, and the timeout can be set longer.

1
2
3
4
5
6
feign:
  client:
    config:
      provider-post:
        connectTimeout: 1000
        readTimeout: 20000

Retries

Implement the feign.Retryer interface

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public class MyRetryer implements Retryer {
    @Override
    public void continueOrPropagate(RetryableException e) {
        throw e;
    }

    @Override
    public Retryer clone() {
        return new Default(100, TimeUnit.SECONDS.toMillis(1), 5);
    }
}

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

After that, we can configure.

1
2
3
4
5
feign:
  client:
    config:
      default:
        retryer: com.fengwenyi.springclouddemo.demospringcloudfeignsentinel.consumerservice.MyRetryer

Reference https://springboot.io/t/topic/4797