在使用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中設置接口超時時間。