2

那是我的代码:

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但它仍然抛出相同的异常

4

3 回答 3

2

我发现这是由于这个问题而发生的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您可以通过调用其第二个参数来更改池大小

另一种解决方案是用另一个像这样的https://github.com/AsyncHttpClient/async-http-client替换这个 Async http 客户端

于 2018-11-06T23:30:24.847 回答
1

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

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

你可以试试:

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

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

于 2018-11-04T10:28:01.573 回答
0

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

于 2018-11-06T09:25:06.530 回答