reactor-netty之client与server如何共用EventLoopGroup

直接到DefaultLoopResources类中,可以看到两个方法

@Override public EventLoopGroup onClient(boolean useNative) { if (useNative && LoopResources.hasNativeSupport()) { return cacheNativeClientLoops(); } return cacheNioClientLoops(); }

@Override public EventLoopGroup onServer(boolean useNative) { if (useNative && LoopResources.hasNativeSupport()) { return cacheNativeServerLoops(); } return cacheNioServerLoops(); }

然后进入cacheNativeClientLoops()中
EventLoopGroup cacheNativeClientLoops() { EventLoopGroup eventLoopGroup = cacheNativeClientLoops.get(); if (null == eventLoopGroup) { EventLoopGroup newEventLoopGroup = LoopResources.colocate(cacheNativeServerLoops()); if (!cacheNativeClientLoops.compareAndSet(null, newEventLoopGroup)) { // Do not shutdown newEventLoopGroup as this will shutdown the server loops } eventLoopGroup = cacheNativeClientLoops(); } return eventLoopGroup; }

可以看到调了cacheNativeServerLoops()方法,实现了与server共用EventLoopGroup.
那么在编程中, 如何指定EventLoopGroup线程数呢?那么请转到reactor.netty.resources.LoopResources中
int DEFAULT_IO_WORKER_COUNT = Integer.parseInt(System.getProperty( ReactorNetty.IO_WORKER_COUNT, "" + Math.max(Runtime.getRuntime().availableProcessors(), 4)));

【reactor-netty之client与server如何共用EventLoopGroup】其中ReactorNetty.IO_WORKER_COUNT值为"reactor.netty.ioWorkerCount", 那可以简单通过设置这个值来实现。

    推荐阅读