关于java:webflux超时时间配置

91次阅读

共计 1672 个字符,预计需要花费 5 分钟才能阅读完成。

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));

正文完
 0