【问题标题】:Spring WebClient throws javax.net.ssl.SSLException: SSLEngine closed already when used heavilySpring WebClient 抛出 javax.net.ssl.SSLException: SSLEngine 在大量使用时已关闭
【发布时间】:2019-04-07 22:08:50
【问题描述】:

这是我的代码:

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)

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

javax.net.ssl.SSLException: SSLEngine closed already
    at io.netty.handler.ssl.SslHandler.wrap(...)(Unknown Source)

编辑

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

【问题讨论】:

标签: netty spring-webflux sslengine


【解决方案1】:

调用WebClient.create()多次重复创建和初始化HTTP资源。

如果没有有关此特定问题的更多详细信息或完整的堆栈跟踪,很难在此处查明确切的问题。但我怀疑为每个调用创建客户端 HTTP 连接器很浪费,并且可能会导致在客户端设置 SSL 时出现问题。

你可以试试:

WebClient webClient = WebClient.create();
// then in your for loop
webClient.post() //...

如果您使用的是 Spring Boot,则应该注入一个 WebClient.Builder 实例并使用它来创建一个 WebClient 实例。

【讨论】:

    【解决方案2】:

    我也遇到过同样的问题,就像 OP 提到的那样,它发生在负载下,但也很容易在服务器负载下被“nginx -s reload”触发。我在 nginx 论坛上发布了这个,但到目前为止没有回复 https://forum.nginx.org/read.php?2,281786。在我的情况下,我对多个请求使用单例客户端实例,所以我认为 Brian's 评论不适用。

    【讨论】:

      【解决方案3】:

      我发现这是因为这个问题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();
      

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

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

      【讨论】:

      • 我们正在使用 async-http-client(通过 Play 框架),但仍然出现异常。但是我们使用 nginx 作为反向代理来进行 SSL 终止。我认为问题仍然存在于足够的负载。
      • 现在修复了吗?在使用最新版本的 spring webflux (5.1.7) 重载后,我遇到了与 huuuge stacktrace 相同的异常。
      • 我不认为这是固定的。我一直在 spring webflux 5.3.4 上看到这个。
      猜你喜欢
      • 2020-08-11
      • 1970-01-01
      • 1970-01-01
      • 2020-03-14
      • 2019-12-09
      • 2019-09-19
      • 2019-05-16
      • 1970-01-01
      • 2014-05-04
      相关资源
      最近更新 更多