是否可以将WebClient配置为使用Reactor-http-nio线程池以外的自定义线程池(使用Netty时)?如果有可能,我们是否可以以某种方式将自定义线程池限制为仅在特定处理器内核上运行?
答案 0 :(得分:3)
是的。可以。
在您自己的线程池和EventLoopGroup中创建一些(或创建NioEventLoopGroup bean)。例如:
<input name="Seg" id="form_Seg" type="checkbox" checked="checked">
Seg
<input name="Ter" id="form_Ter" type="checkbox">
Ter
注册您自己的ReactorResourceFactory。并基于自定义线程执行器提供您自己的EventLoopGrooup
{
Intger THREADS = 10;
BasicThreadFactory THREADFACTORY = new BasicThreadFactory.Builder()
.namingPattern("HttpThread-%d")
.daemon(true)
.priority(Thread.MAX_PRIORITY)
.build();
EXECUTOR = new ThreadPoolExecutor(
THREADS,
THREADS,
0L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(),
THREADFACTORY,
new ThreadPoolExecutor.AbortPolicy());
NioEventLoopGroup RESOURCE= new NioEventLoopGroup(THREADS,EXECUTOR);
}
然后注册ReactorClientHttpConnector。在下面的示例中,使用了自定义SSL上下文
@Bean
public ReactorResourceFactory reactorResourceFactory(NioEventLoopGroup RESOURCE) {
ReactorResourceFactory f= new ReactorResourceFactory();
f.setLoopResources(new LoopResources() {
@Override
public EventLoopGroup onServer(boolean b) {
return RESOURCE;
}
});
f.setUseGlobalResources(false);
return f;
}
最终构建WebClient
@Bean
public ReactorClientHttpConnector reactorClientHttpConnector(ReactorResourceFactory r) throws SSLException {
SslContext sslContext = SslContextBuilder
.forClient()
.trustManager(InsecureTrustManagerFactory.INSTANCE)
.build();
return new ReactorClientHttpConnector(r, m -> m.secure(t -> t.sslContext(sslContext)));
}
如果要将其用于WebServer。对ReactiveWebServerFactory进行相同的配置。
@Bean
public WebClient webClient(ReactorClientHttpConnector r) {
return WebClient.builder().clientConnector(r).build();
}
进口:
@Bean
public ReactiveWebServerFactory reactiveWebServerFactory(NioEventLoopGroup RESOURCE) {
NettyReactiveWebServerFactory factory = new NettyReactiveWebServerFactory();
factory.addServerCustomizers(hs->hs.tcpConfiguration(s->s.runOn(RESOURCE)));
return factory;
}