关闭 databus web 请求连接池
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
package com.zt.plat.module.databus.framework.integration.gateway.config;
|
package com.zt.plat.module.databus.framework.integration.gateway.config;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.web.reactive.function.client.WebClientCustomizer;
|
import org.springframework.boot.web.reactive.function.client.WebClientCustomizer;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
@@ -17,33 +19,43 @@ public class GatewayWebClientConfiguration {
|
|||||||
private final int maxInMemorySize;
|
private final int maxInMemorySize;
|
||||||
private final long maxIdleTimeMillis;
|
private final long maxIdleTimeMillis;
|
||||||
private final long evictInBackgroundMillis;
|
private final long evictInBackgroundMillis;
|
||||||
|
private final boolean connectionPoolEnabled;
|
||||||
private final ReactorClientHttpConnector httpConnector;
|
private final ReactorClientHttpConnector httpConnector;
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(GatewayWebClientConfiguration.class);
|
||||||
|
|
||||||
public GatewayWebClientConfiguration(
|
public GatewayWebClientConfiguration(
|
||||||
@Value("${databus.gateway.web-client.max-in-memory-size:20971520}") int maxInMemorySize,
|
@Value("${databus.gateway.web-client.max-in-memory-size:20971520}") int maxInMemorySize,
|
||||||
@Value("${databus.gateway.web-client.max-idle-time:45000}") long maxIdleTimeMillis,
|
@Value("${databus.gateway.web-client.max-idle-time:45000}") long maxIdleTimeMillis,
|
||||||
@Value("${databus.gateway.web-client.evict-in-background-interval:20000}") long evictInBackgroundMillis) {
|
@Value("${databus.gateway.web-client.evict-in-background-interval:20000}") long evictInBackgroundMillis,
|
||||||
|
@Value("${databus.gateway.web-client.connection-pool-enabled:true}") boolean connectionPoolEnabled) {
|
||||||
this.maxInMemorySize = maxInMemorySize;
|
this.maxInMemorySize = maxInMemorySize;
|
||||||
this.maxIdleTimeMillis = maxIdleTimeMillis > 0 ? maxIdleTimeMillis : 45000L;
|
this.maxIdleTimeMillis = maxIdleTimeMillis;
|
||||||
this.evictInBackgroundMillis = Math.max(evictInBackgroundMillis, 0L);
|
this.evictInBackgroundMillis = evictInBackgroundMillis;
|
||||||
|
this.connectionPoolEnabled = connectionPoolEnabled;
|
||||||
this.httpConnector = buildConnector();
|
this.httpConnector = buildConnector();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public WebClientCustomizer gatewayWebClientCustomizer() {
|
public WebClientCustomizer gatewayWebClientCustomizer() {
|
||||||
|
// 统一设置 WebClient 连接器与内存限制,避免各处重复配置
|
||||||
return builder -> builder
|
return builder -> builder
|
||||||
.clientConnector(httpConnector)
|
.clientConnector(httpConnector)
|
||||||
.codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(maxInMemorySize));
|
.codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(maxInMemorySize));
|
||||||
}
|
}
|
||||||
|
|
||||||
private ReactorClientHttpConnector buildConnector() {
|
private ReactorClientHttpConnector buildConnector() {
|
||||||
ConnectionProvider.Builder providerBuilder = ConnectionProvider.builder("databus-gateway")
|
if (connectionPoolEnabled) {
|
||||||
.maxIdleTime(Duration.ofMillis(maxIdleTimeMillis));
|
// 启用连接池,基于配置设置空闲回收参数
|
||||||
if (evictInBackgroundMillis > 0) {
|
ConnectionProvider provider = ConnectionProvider.builder("databus-gateway")
|
||||||
providerBuilder.evictInBackground(Duration.ofMillis(evictInBackgroundMillis));
|
.maxIdleTime(Duration.ofMillis(maxIdleTimeMillis))
|
||||||
|
.evictInBackground(Duration.ofMillis(evictInBackgroundMillis))
|
||||||
|
.build();
|
||||||
|
log.info("Databus gateway WebClient 已启用连接池 (maxIdleTime={}ms, evictInterval={}ms)",
|
||||||
|
maxIdleTimeMillis, evictInBackgroundMillis);
|
||||||
|
return new ReactorClientHttpConnector(HttpClient.create(provider).compress(true));
|
||||||
}
|
}
|
||||||
ConnectionProvider provider = providerBuilder.build();
|
// 关闭连接池,每次请求都会重新建立 TCP 连接
|
||||||
HttpClient httpClient = HttpClient.create(provider).compress(true);
|
log.info("Databus gateway WebClient 已禁用连接池,所有请求将使用全新连接");
|
||||||
return new ReactorClientHttpConnector(httpClient);
|
return new ReactorClientHttpConnector(HttpClient.create().compress(true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -131,4 +131,9 @@ zt:
|
|||||||
ignore-tables:
|
ignore-tables:
|
||||||
- databus_api_client_credential
|
- databus_api_client_credential
|
||||||
|
|
||||||
|
databus:
|
||||||
|
gateway:
|
||||||
|
web-client:
|
||||||
|
connection-pool-enabled: false # 默认开启连接池,排查长连接问题时可临时关闭
|
||||||
|
|
||||||
debug: false
|
debug: false
|
||||||
|
|||||||
Reference in New Issue
Block a user