连接器参数调优
连接器参数直接影响网络 I/O 性能,合理配置可显著提升吞吐量和响应速度。
核心参数概览
关键参数分类
| 类别 | 参数 | 说明 |
|---|---|---|
| 连接管理 | connection-timeout | 连接超时时间 |
| 连接管理 | keep-alive-timeout | Keep-Alive 超时 |
| 连接管理 | max-connections | 最大连接数 |
| 请求处理 | accept-count | 等待队列长度 |
| 请求处理 | max-http-header-size | 请求头大小限制 |
| 请求处理 | max-http-post-size | 请求体大小限制 |
连接超时配置
Tomcat 配置
YAML
server:
tomcat:
connection-timeout: 20000 # 连接超时 20s
keep-alive-timeout: 60000 # Keep-Alive 60s
max-keep-alive-requests: 100 # 单连接最大请求数
Undertow 配置
YAML
server:
undertow:
no-request-timeout: 60000 # 无请求超时
idle-timeout: 30000 # 空闲超时
代码配置
Java
@Configuration
public class ConnectorTimeoutConfig {
@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> timeoutCustomizer() {
return factory -> factory.addConnectorCustomizers(connector -> {
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
protocol.setConnectionTimeout(20000); // 连接超时
protocol.setKeepAliveTimeout(60000); // Keep-Alive 超时
protocol.setMaxKeepAliveRequests(100); // 最大请求数
});
}
}
连接数限制
Tomcat 连接数配置
YAML
server:
tomcat:
max-connections: 10000 # 最大连接数
accept-count: 200 # 等待队列长度
参数关系
Java
max-connections: 最大并发连接数(包括等待和处理)
accept-count: 等待队列长度(超过 max-connections 后排队)
threads.max: 工作线程数(同时处理请求的线程)
Undertow 连接数配置
YAML
@Bean
public WebServerFactoryCustomizer<UndertowServletWebServerFactory> connectionCustomizer() {
return factory -> {
factory.addBuilderCustomizers(builder -> {
builder.setServerOption(UndertowOptions.MAX_ENTITY_SIZE, 10485760L);
builder.setServerOption(UndertowOptions.MAX_PARAMETERS, 1000);
});
};
}
Keep-Alive 优化
Keep-Alive 作用
- 复用 TCP 连接,减少握手开销
- 提升短连接场景性能
- 避免频繁创建/销毁连接
推荐配置
Java
server:
tomcat:
keep-alive-timeout: 60000 # 60s 超时
max-keep-alive-requests: 1000 # 单连接最大请求数
connection-timeout: 20000
关闭 Keep-Alive(特殊场景)
YAML
@Configuration
public class DisableKeepAliveConfig {
@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> disableKeepAlive() {
return factory -> factory.addConnectorCustomizers(connector -> {
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
protocol.setMaxKeepAliveRequests(1); // 每个请求后关闭连接
});
}
}
请求大小限制
请求体限制
Java
server:
max-http-request-header-size: 16KB
tomcat:
max-http-form-post-size: 10MB
spring:
servlet:
multipart:
max-file-size: 100MB
max-request-size: 100MB
请求头限制
YAML
@Configuration
public class RequestSizeConfig {
@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> headerSizeCustomizer() {
return factory -> factory.addConnectorCustomizers(connector -> {
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
protocol.setMaxHttpHeaderSize(16384); // 16KB
});
}
}
压缩配置
启用 GZIP 压缩
Java
server:
compression:
enabled: true
mime-types: application/json,application/xml,text/html,text/xml,text/plain
min-response-size: 1024 # 大于1KB才压缩
压缩级别(Undertow)
text
@Bean
public WebServerFactoryCustomizer<UndertowServletWebServerFactory> compressionCustomizer() {
return factory -> {
factory.addBuilderCustomizers(builder -> {
builder.setServerOption(UndertowOptions.ENABLE_COMPRESSION, true);
builder.setServerOption(UndertowOptions.COMPRESSION_LEVEL, 6); // 1-9
});
};
}
常用配置对照表
| 场景 | connection-timeout | keep-alive-timeout | max-connections |
|---|---|---|---|
| 高并发短连接 | 5000 | 30000 | 10000+ |
| 长连接场景 | 20000 | 120000 | 5000 |
| API 网关 | 10000 | 60000 | 20000 |
| 内部服务 | 30000 | 180000 | 2000 |
注意:Keep-Alive 超时不宜过长,避免占用过多连接资源。
要点总结
- connection-timeout 控制连接建立超时
- Keep-Alive 复用连接,减少握手开销
- max-connections 限制并发连接数
- 根据业务场景调整超时参数
- 压缩传输减少网络开销
📝 发现内容有误?点击此处直接编辑