首页 > 解决方案 > Spring 的 WebClient 的连接和套接字超时会引发哪个异常?

问题描述

我正在通过 a 设置连接、套接字、读取和写入超时,reactor.netty.tcp.TcpClient如下面的代码片段所示。在读取或写入超时时会抛出io.netty.handler.timeout.ReadTimeoutExceptionor 。WriteTimeoutException

连接和套接字超时会引发哪个异常?

// configuration of timeouts
HttpClient httpClient = HttpClient.create()
    .tcpConfiguration(client -> client
        .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2_000)
        //.option(ChannelOption.SO_TIMEOUT, 2_000) // SO_TIMEOUT not supported
        .doOnConnected(conn -> conn
            .addHandlerLast(new ReadTimeoutHandler(2_000, TimeUnit.MILLISECONDS))
            .addHandlerLast(new WriteTimeoutHandler(2_000, TimeUnit.MILLISECONDS))));
ClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);

// build the web client
WebClient webClient = WebClient.builder()
    .clientConnector(connector)
    .baseUrl("http://localhost:8081")
    .build();

// perform the GET-request (throws ReadTimeoutException, WriteTimeoutException)
List<MyEntity> responseList = this.nominatimWebClient.get()
                    .uri("/query")
                    .retrieve().bodyToMono(new ParameterizedTypeReference<List<MyEntity>>() {}).block();

编辑:这个警告出现在我的日志中,所以我不再使用 SO_TIMEOUT 了:

io.netty.bootstrap.Bootstrap: Unknown channel option 'SO_TIMEOUT' for channel '[id: 0xbb2d06f5]'

标签: javaspringspring-boot

解决方案


推荐阅读