How to reset the number of selector threads and worker threads in Netty HTTP Server
方法:使用自定义ReactorResourceFactory bean 替代原有 ReactorResourceFactory bean。
由于HttpResource全局唯一,因此分三步进行重新设置。
- 初始化默认 HttpResource
- 更新 LoopResources 配置
- 更新 ConnectionProvider 配置
设置完毕后创建ReactorResourceFactory实例返回即可。
@Configuration
public class NettyWebServerConfiguration {
@Value("${netty.server.worker.ratio}")
private BigDecimal processorWorkerRatio = BigDecimal.valueOf(1l);
@Bean
public ReactorResourceFactory reactorServerResourceFactory() {
// 初始化 http resources
HttpResources.get();
// 设置自定义 loop resources
int processor = Runtime.getRuntime().availableProcessors();
int count = BigDecimal.valueOf(processor).multiply(processorWorkerRatio).intValue();
int workerCount = Math.max(count, 4);
HttpResources.set(LoopResources.create("reactor-http-lr", workerCount, workerCount, true));
// 设置自定义 connection provider
HttpResources.set(ConnectionProvider.elastic("reactor-http-cp"));
// 获取 resource factory
ReactorResourceFactory resourceFactory = new ReactorResourceFactory();
return resourceFactory;
}
}