WebClient 超时后如何优雅地处理异常
在使用 WebClient 时,超时是一个常见的问题。当请求超时时,WebClient 会抛出 TimeoutException。为了优雅地处理这些异常,可以采用以下几种方法:【起飞嘎嘎飞LSIXSO】
一、使用 onErrorResume
或 onErrorReturn
处理超时
onErrorResume 和 onErrorReturn 是 WebClient 提供的两个操作符,用于在发生错误时提供替代的响应。
(一)onErrorResume
onErrorResume 允许你在发生错误时返回一个替代的 Mono 或 Flux。
java复制
import org.springframework.web.reactive.function.client.WebClient;
import java.time.Duration;
public class WebClientTimeoutExample {
public static void main(String[] args) {
WebClient client = WebClient.builder()
.baseUrl("https://api.example.com")
.build();
client.get()
.uri("/data")
.retrieve()
.bodyToMono(String.class)
.timeout(Duration.ofSeconds(5)) // 设置请求级超时为 5 秒
.onErrorResume(e -> Mono.just("Default response")) // 超时后返回默认响应
.subscribe(
response -> System.out.println("Response: " + response),
error -> System.err.println("Error: " + error.getMessage())
);
}
}
(二)onErrorReturn
onErrorReturn 允许你在发生错误时返回一个特定的值。
java复制
client.get()
.uri("/data")
.retrieve()
.bodyToMono(String.class)
.timeout(Duration.ofSeconds(5))
.onErrorReturn("Default response")
.subscribe(
response -> System.out.println("Response: " + response),
error -> System.err.println("Error: " + error.getMessage())
);
二、使用 retryWhen
实现重试机制
retryWhen 允许你定义一个重试策略,当请求失败时自动重试。这可以提高系统的可靠性和容错能力。
java复制
import org.springframework.web.reactive.function.client.WebClient;
import java.time.Duration;
public class WebClientTimeoutExample {
public static void main(String[] args) {
WebClient client = WebClient.builder()
.baseUrl("https://api.example.com")
.build();
client.get()
.uri("/data")
.retrieve()
.bodyToMono(String.class)
.timeout(Duration.ofSeconds(5)) // 设置请求级超时为 5 秒
.retryWhen(Retry.backoff(3, Duration.ofSeconds(2))) // 重试 3 次,每次间隔 2 秒
.subscribe(
response -> System.out.println("Response: " + response),
error -> System.err.println("Error: " + error.getMessage())
);
}
}
(一)自定义异常处理
你可以在 retryWhen 中定义更复杂的重试策略,例如在重试前执行某些操作。
java复制
client.get()
.uri("/data")
.retrieve()
.bodyToMono(String.class)
.timeout(Duration.ofSeconds(5))
.retryWhen(Retry.backoff(3, Duration.ofSeconds(2))
.doBeforeRetry(retrySignal -> System.out.println("Retrying...")))
.subscribe(
response -> System.out.println("Response: " + response),
error -> System.err.println("Error: " + error.getMessage())
);
三、使用 onStatus
处理特定状态码
onStatus 允许你根据 HTTP 状态码处理错误。这可以用于处理 4xx 和 5xx 错误。
java复制
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.http.HttpStatus;
public class WebClientTimeoutExample {
public static void main(String[] args) {
WebClient client = WebClient.builder()
.baseUrl("https://api.example.com")
.build();
client.get()
.uri("/data")
.retrieve()
.onStatus(HttpStatus::is4xxClientError, response -> Mono.error(new RuntimeException("Client error")))
.onStatus(HttpStatus::is5xxServerError, response -> Mono.error(new RuntimeException("Server error")))
.bodyToMono(String.class)
.timeout(Duration.ofSeconds(5)) // 设置请求级超时为 5 秒
.subscribe(
response -> System.out.println("Response: " + response),
error -> System.err.println("Error: " + error.getMessage())
);
}
}
四、全局异常处理
你可以通过定义一个全局的异常处理逻辑来处理所有 WebClient 请求的异常。
java复制
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.http.HttpStatus;
public class WebClientConfig {
public WebClient createWebClient() {
return WebClient.builder()
.filter(ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
if (clientResponse.statusCode().is4xxClientError()) {
return clientResponse.bodyToMono(String.class)
.flatMap(errorBody -> Mono.error(new RuntimeException("Client error: " + errorBody)));
} else if (clientResponse.statusCode().is5xxServerError()) {
return clientResponse.bodyToMono(String.class)
.flatMap(errorBody -> Mono.error(new RuntimeException("Server error: " + errorBody)));
}
return Mono.just(clientResponse);
}))
.build();
}
}
五、总结
在使用 WebClient 时,超时是一个常见的问题。通过使用 onErrorResume、onErrorReturn、retryWhen 和 onStatus 等操作符,可以优雅地处理这些异常。这些方法不仅可以提高系统的可靠性,还可以提供更好的用户体验。