我正在使用 springboot webclient 从远程服务器调用 rest api。第一个请求工作正常。如果我在一段时间后发出后续请求,服务器会抛出 500 服务器错误。我得到的错误是“onError(java.io.IOException:一个现有的连接被远程主机强行关闭)”。
我想通过禁用连接池来测试行为,因为我相信它使用以前的连接。你能帮我在创建 webclient 时如何禁用连接池吗?
TcpClient tcpClient = TcpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30000)
.option(ChannelOption.SO_KEEPALIVE, false)
.doOnConnected(connection ->
connection.addHandlerLast(new ReadTimeoutHandler(30))
.addHandlerLast(new WriteTimeoutHandler(30))
);
ReactorClientHttpConnector httpConnector = new ReactorClientHttpConnector(HttpClient.from(tcpClient));
final WebClient webClient = WebClient
.builder()
.clientConnector(httpConnector)
.baseUrl("http://customer.service.api.internal.cloud.qa.intranet.pagseguro.uol")
.exchangeStrategies(strategies)
.build()
Run Code Online (Sandbox Code Playgroud) Springboot Webclient 在尝试调用远程服务器中的 rest api 时抛出“一个现有连接被远程主机强行关闭”错误。
当服务器加载时,第一个服务器请求加载正常。当我在一段时间后(比如 5 分钟)发送第二个请求时,出现错误。
网页客户端创建代码:
WebClient webClient() {
TcpClient tcpClient = TcpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30000)
.option(ChannelOption.SO_KEEPALIVE, false)
.doOnConnected { connection ->
connection.addHandlerLast(new LoggingHandler(LogLevel.TRACE))
connection.addHandlerLast(new ReadTimeoutHandler(30))
.addHandlerLast(new WriteTimeoutHandler(30))
.addHandlerLast(new IdleStateHandler(30,30,30))
}
ReactorClientHttpConnector httpConnector = new ReactorClientHttpConnector(HttpClient.from(tcpClient))
return WebClient.builder()
.clientConnector(httpConnector)
.build()
}
Run Code Online (Sandbox Code Playgroud)
请在下面找到日志。看起来连接没有正确关闭。
你能告诉我如何解决吗?
2019-04-10 15:26:31.534 INFO 235344 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-04-10 15:26:31.534 INFO 235344 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-04-10 15:26:31.544 INFO 235344 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Springboot-Reactive webclient 进行 HTTP 调用。我的连接因远程服务器错误而关闭。
请找到以下使用Webclient进行休息呼叫的代码。
Mono<String> post(String url, JSONObject body) {
Mono<String> result = webClient().post().uri(url)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON_UTF8)
.body(BodyInserters.fromObject(body))
.exchange().log()
.flatMap { clientResponse ->
return clientResponse.bodyToMono(String.class)
}
return result
}
Run Code Online (Sandbox Code Playgroud)
Web客户端创建的代码:
WebClient webClient() {
TcpClient tcpClient = TcpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30000)
.doOnConnected { connection ->
connection.addHandlerLast(new LoggingHandler(LogLevel.TRACE))
connection.addHandlerLast(new ReadTimeoutHandler(30))
.addHandlerLast(new WriteTimeoutHandler(30))
}
tcpClient.wiretap(true)
ReactorClientHttpConnector httpConnector = new ReactorClientHttpConnector(HttpClient.from(tcpClient))
return WebClient.builder()
.clientConnector(httpConnector)
.build()
}
Run Code Online (Sandbox Code Playgroud)
第一次通话后我收到以下日志:
2019-04-10 15:26:31.534 INFO 235344 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-04-10 15:26:31.534 INFO 235344 …Run Code Online (Sandbox Code Playgroud) netty spring-boot reactor-netty spring-webflux spring-webclient