Configure OpenFeign Interface Timeout

When using OpenFeign to call APIs, you can set the interface timeout by configuring Feign. Follow these steps to set the interface timeout:

  1. Create a Feign client configuration class that inherits from the FeignClientSpecification class, and override the apply method to set the timeout.
import feign.Client;
import feign.Client.Default;
import feign.Client.Default.DefaultClientConfig;
import feign.Feign;
import feign.Request;
import feign.Request.Options;
import feign.RequestTemplate;
import feign.codec.Decoder;

public class MyFeignClientConfiguration extends DefaultClientConfig {

    @Override
    public Client apply(Client client) {
        return new Client() {
            private final Client delegate = client;

            @Override
            public Response execute(Request request, Options options) throws IOException {
                // 设置超时时间为10秒
                options = options.toBuilder().connectTimeout(10000)
                        .readTimeout(10000).build();
                return delegate.execute(request, options);
            }
        };
    }
}
  1. Add the @FeignClient annotation to the Feign interface and specify the configuration property to be the Feign client configuration class created in the previous step.
@FeignClient(name = "my-service", configuration = MyFeignClientConfiguration.class)
public interface MyFeignClient {

    @GetMapping("/api/someEndpoint")
    String callSomeEndpoint();
}

By following the above steps, you can set the interface timeout in OpenFeign.

bannerAds