我使用 spring-boot-starter-graphql 与 websocket 进行简单的客户端-服务器通信。 下面是客户端的代码片段
"ws://localhost:8080" +"/graphql";
WebSocketClient client = new ReactorNettyWebSocketClient();
WebSocketGraphQlClient graphQlClient = WebSocketGraphQlClient.builder(endPoint, client).build();
Flux<HomeLocationResponse> locationCreateResponseMono = graphQlClient.documentName("location-document")
.variable("msisdn", getLocationRequest.getMsisdn())
.variable("transactionid", getLocationRequest.getTransactionId())
.retrieveSubscription("getLocation")
.toEntity(HomeLocationResponse.class)
在此之上,我创建了调用此代码的 REST 端点。 这对于我启动此服务后的前 500 个请求效果很好,但在 500 个请求之后它会中断,并且我看到以下堆栈跟踪中的多个错误
2022-04-26 18:39:34.868 ERROR 102456 --- [ parallel-2] o.s.g.client.WebSocketGraphQlTransport : Session handling error: Pool#acquire(Duration) has been pending for more than the configured timeout of 45000ms
2022-04-26 18:39:34.869 ERROR 102456 --- [ parallel-2] reactor.core.publisher.Operators : Operator called default onErrorDropped
reactor.netty.internal.shaded.reactor.pool.PoolAcquireTimeoutException: Pool#acquire(Duration) has been pending for more than the configured timeout of 45000ms
at reactor.netty.internal.shaded.reactor.pool.AbstractPool$Borrower.run(AbstractPool.java:415) ~[reactor-netty-core-1.0.17.jar:1.0.17]
at reactor.core.scheduler.SchedulerTask.call(SchedulerTask.java:68) ~[reactor-core-3.4.16.jar:3.4.16]
at reactor.core.scheduler.SchedulerTask.call(SchedulerTask.java:28) ~[reactor-core-3.4.16.jar:3.4.16]
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) ~[na:na]
at java.base/java.lang.Thread.run(Thread.java:831) ~[na:na]
我是 websocket 新手,不确定为什么 websocket 连接没有关闭。
我所涉及的微服务场景如下。
1。 GraphQLClient 微服务 2. GraphQl Server 微服务
GraphQLClient 微服务再次将 REST 端点公开为包装器。
因此将执行以下流程 Browser -> GraphQLClient <-> GraphQlServer
连接后https://github.com/spring-projects/spring-graphql/issues/369
并添加
graphQlClient.stop().subscribe();
可以解决问题。
使用pendingAcquireTimeout参数配置WebClient,如下所示:
@Bean
public WebClient webClient() {
ConnectionProvider connectionProvider = ConnectionProvider.builder("myConnectionPool")
.pendingAcquireTimeout(Duration.ofMinutes(100)) // Pool#acquire(Duration) has been pending for more than the configured timeout of 45000ms
.build();
ReactorClientHttpConnector clientHttpConnector = new ReactorClientHttpConnector(HttpClient.create(connectionProvider));
return WebClient.builder()
.clientConnector(clientHttpConnector)
.build();
}