在使用OpenFeign调用接口时,可以通过Feign的配置来设置接口超时时间。可以通过以下步骤设置接口超时时间:
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);
}
};
}
}
@FeignClient(name = "my-service", configuration = MyFeignClientConfiguration.class)
public interface MyFeignClient {
@GetMapping("/api/someEndpoint")
String callSomeEndpoint();
}
通过以上步骤,就可以在OpenFeign中设置接口超时时间。