Java 如何在reactor netty中配置池连接空闲超时

Java 如何在reactor netty中配置池连接空闲超时,java,reactor-netty,Java,Reactor Netty,我正在使用带有连接池的reactor netty http客户端(0.7.X系列),希望配置池连接的空闲超时,但不知道在哪里 更准确地说,我需要配置reactor netty http客户端连接池,使其能够自动关闭在可配置超时内未看到任何活动的连接。这些连接是打开的,但在一段(可配置的)时间内未传入或传出字节 如何配置reactory netty http客户端以抢先关闭空闲连接?通过向通道管道添加netty写入和读取超时处理程序,我能够在0.7.x分支上实现这一点。但是,在0.8.x上,这种方

我正在使用带有连接池的reactor netty http客户端(0.7.X系列),希望配置池连接的空闲超时,但不知道在哪里

更准确地说,我需要配置reactor netty http客户端连接池,使其能够自动关闭在可配置超时内未看到任何活动的连接。这些连接是打开的,但在一段(可配置的)时间内未传入或传出字节


如何配置reactory netty http客户端以抢先关闭空闲连接?

通过向通道管道添加netty写入和读取超时处理程序,我能够在0.7.x分支上实现这一点。但是,在0.8.x上,这种方法不再有效

HttpClient httpClient = HttpClient
    .create((HttpClientOptions.Builder builder) -> builder
    .host(endpointUrl.getHost())
    .port(endpointUrl.getPort())
    .poolResources(PoolResources.fixed(connectionPoolName, maxConnections, timeoutPool))
    .afterChannelInit(channel -> {
        channel.pipeline()
                // The write and read timeouts are serving as generic socket idle state handlers.
                .addFirst("write_timeout", new WriteTimeoutHandler(timeoutIdle, TimeUnit.MILLISECONDS))
                .addFirst("read_timeout", new ReadTimeoutHandler(timeoutIdle, TimeUnit.MILLISECONDS));
    })
    .build());

我成功地配置了
WebClient
(通过底层
TcpClient
),以便在reactor netty 0.8.9中的连接池超时时删除空闲连接

我的解决方案部分基于关于extended的官方文档,我研究了在创建
HttpClient
实例时如何正确应用它

我是这样做的:

public类IdleCleanupHandler扩展ChannelDuplexHandler{
@凌驾
public void userEventTriggered(最终ChannelHandlerContext ctx,最终对象evt)引发异常{
if(IdleStateEvent的evt实例){
最终IdleState状态=((IDLESTATEVENT)evt.state();
if(state==idlstate.ALL\u IDLE){//或READER\u IDLE/WRITER\u IDLE
//关闭空转通道
ctx.close();
}
}否则{
超级用户事件触发(ctx、evt);
}
}
}
...
公共静态WebClient createWebClient(最终字符串baseUrl,最终int idleTimeoutSec){
最终TcpClient TcpClient=TcpClient.create(ConnectionProvider.fixed(“固定池”))
.bootstrap(bootstrap->bootstraphandler.updateConfiguration(bootstrap,“idleTimeoutConfig”,
(连接观察者,频道)->{
管道()
.addLast(“idleStateHandler”,新的idleStateHandler(0,0,idleTimeoutSec))
.addLast(“idleCleanupHandler”,新的idleCleanupHandler());
}));
返回WebClient.builder()
.clientConnector(新的ReactorClientHttpConnector(HttpClient.from(tcpClient)))
.baseUrl(baseUrl)
.build();
}

重要更新:

我的进一步测试表明,在
引导过程中添加处理程序会破坏池,而
连接不会重用套接字(通道)

添加处理程序的正确方法是:

publicstaticwebclient createWebClient(最终字符串baseUrl,最终int idleTimeoutSec){
最终TcpClient TcpClient=TcpClient.create(ConnectionProvider.fixed(“固定池”))
.doon已连接(连接->{
最终通道管道=连接通道().管道();
if(pipeline.context(“idleStateHandler”)==null){
addLast(“idleStateHandler”,新的idleStateHandler(0,0,idleTimeoutSec))
.addLast(“idleCleanupHandler”,新的idleCleanupHandler());
}
});
返回WebClient.builder()
.clientConnector(新的ReactorClientHttpConnector(HttpClient.from(tcpClient)))
.baseUrl(baseUrl)
.build();
}

注意:在
reactor netty
0.9.x中,将有一种标准方法来配置连接池中连接的空闲超时,请参阅此提交:

在reactor netty 0.9.x中使用TCP客户端执行此操作的最简单方法是使用以下方法,我从@Vladimir-L引用的方法中获得此方法。配置“maxIdleTime”谢谢你的提问

TcpClient timeoutClient = TcpClient.create(ConnectionProvider.fixed(onnectionPoolName, maxConnections, acquireTimeout,maxIdleTime));

由于spring boot starter webflux的原因,我目前在反应堆netty 0.8.2,并且面临相同的问题,连接池在连接完成后将连接保持打开60秒

使用此方法,您无法配置超时,但可以禁用超时:

WebClient.builder()
    .clientConnector(new ReactorClientHttpConnector(
        HttpClient.from(TcpClient.create()).keepAlive(false)))
    .build()
    .get()
    .uri("someurl")
    .retrieve()
    .bodyToMono(String.class)

谢谢你的回复。