Spring WebClient 抛出 javax.net.ssl.SSLException: SSLEngine 在大量使用时已关闭

Avi*_*ego 4 netty sslengine spring-webflux

这是我的代码:

WebClient.create().post()
                .uri(URI.create(url))
                .header("Authorization",
                        "Basic " + Base64Utils.encodeToString(("username:password").getBytes(UTF_8)))
                .body(Mono.just(requestBody), Object.class)
                .retrieve()
                .bodyToMono(responseType)
Run Code Online (Sandbox Code Playgroud)

我同时从多个线程调用这个函数。当我在一次运行中只调用它大约 20~30 次时,它工作得很好。但是当我在大约 2 分钟内调用它 500~600 次(对于同一个 URL)时,它会抛出

javax.net.ssl.SSLException: SSLEngine closed already
    at io.netty.handler.ssl.SslHandler.wrap(...)(Unknown Source)
Run Code Online (Sandbox Code Playgroud)

编辑

我尝试只创建一个实例WebClient,但它仍然抛出相同的异常

Avi*_*ego 5

我发现这是由于这个问题而发生的https://github.com/reactor/reactor-netty/issues/413

要解决它,您需要WebClient像这样创建:

WebClient webClient = WebClient.builder()
               .clientConnector(new ReactorClientHttpConnector(options -> {
                   options.poolResources(PoolResources.fixed("httpPool")).compression(true);
               })).build();
Run Code Online (Sandbox Code Playgroud)

PoolResources.fixed您可以通过使用第二个参数调用来更改池大小

另一种解决方案是将此异步 http 客户端替换为另一个类似的客户端https://github.com/AsyncHttpClient/async-http-client