1 全局配置 Configuring Timeouts via HTTP Client
1.1 Response Timeout
HttpClient client = HttpClient.create() .responseTimeout(Duration.ofSeconds(1));WebClient webClient = WebClient.builder() .clientConnector(new ReactorClientHttpConnector(client)) .build();
1.2 Connection Timeout
HttpClient client = HttpClient.create() .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
默认是30s, 如果在给定的工夫不能胜利建设连贯或者被抛弃掉,将抛出ConnectTimeoutException,此外还能够设置一些tcp参数
HttpClient client = HttpClient.create() .option(ChannelOption.SO_KEEPALIVE, true) .option(EpollChannelOption.TCP_KEEPIDLE, 300) .option(EpollChannelOption.TCP_KEEPINTVL, 60) .option(EpollChannelOption.TCP_KEEPCNT, 8);
1.3 Read and Write Timeout
HttpClient client = HttpClient.create()
.doOnConnected(conn -> conn
.addHandler(new ReadTimeoutHandler(10, TimeUnit.SECONDS)).addHandler(new WriteTimeoutHandler(10)));
1.4 SSL/TLS Timeout
HttpClient.create() .secure(spec -> spec.sslContext(SslContextBuilder.forClient()) .defaultConfiguration(SslProvider.DefaultConfigurationType.TCP) .handshakeTimeout(Duration.ofSeconds(30)) .closeNotifyFlushTimeout(Duration.ofSeconds(10)) .closeNotifyReadTimeout(Duration.ofSeconds(10)));
handshake timeout默认是10s
close_notify flush默认3s
read默认0s
1.5 Proxy Timeout
HttpClient.create() .proxy(spec -> spec.type(ProxyProvider.Proxy.HTTP) .host("proxy") .port(8080) .connectTimeoutMillis(30000));
connectTimeoutMillis默认10s
2 Request-Level配置
2.1 Response Timeout
webClient.get() .uri("https://baeldung.com/path") .httpRequest(httpRequest -> { HttpClientRequest reactorRequest = httpRequest.getNativeRequest(); reactorRequest.responseTimeout(Duration.ofSeconds(2)); });
2.2 Reactive Timeout
webClient.get() .uri("https://baeldung.com/path") .retrieve() .bodyToFlux(JsonNode.class) .timeout(Duration.ofSeconds(5));