尝试了解 spring webclient 如何处理对等点之间的连接。
使用以下代码时:
我与流中的元素有尽可能多的密切联系。
我预计当使用 http1.1 时,reactor-netty 会重用连接。
即使在尝试使用配置上的保持活动时,我也有相同的行为:
[d6b705e1]响应200 OK
[d6b705e1] 取消信号(关闭连接)
public WebClient webClient(WebClient.Builder builder) {
return builder
.baseUrl(config.getBaseurl())
.clientConnector(new ReactorClientHttpConnector(
HttpClient.create()
.tcpConfiguration(tcpClient -> tcpClient.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeout))
.secure(spec -> spec.sslContext(SslContextBuilder.forClient()))
))
.build();
}
Flux<String> getIds() { ... }
Flux<Response> getResponses() {
// here as many open and close connection as the return stream getIds
return getIds().flatMap(... // client.retrieve().bodyToMono(Reponse.class));
}
我期望在使用 http1.1 时,reactor-netty 会重用连接。
如果可以的话,会的。以下面为例:
@RestController
public class MyController {
private final WebClient wc;
@Autowired
public MyController(WebClient.Builder wcb) {
wc = wcb
.baseUrl("https://jsonplaceholder.typicode.com/")
.clientConnector(new ReactorClientHttpConnector(
HttpClient.create()
.tcpConfiguration(tcpClient -> tcpClient.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2000))
.secure(spec -> spec.sslContext(SslContextBuilder.forClient()))
))
.build();
}
@GetMapping("/")
public Mono<List<Body>> cookieSetter() {
return Flux.range(1,10)
.flatMap(i -> wc.get().uri("todos/" + i).retrieve().bodyToMono(Body.class))
.collectList();
}
@Data
static class Body {
private String title;
}
}
加载它,您会看到一堆
HTTP GET https://jsonplaceholder.typicode.com/todos/x
和 Response 200 OK
,但没有 Cancel signal (to close connection)
日志 - 它保持连接打开并重用它。
您没有提供完整的示例,但这里可能发生以下两种情况之一:
Connection: close
标头。这将正确地强制 Netty 关闭每个请求之间的连接,而不是重用它。