/**
* Netty服務端啟動代碼。
*
* @author hmilyylimh
*
* @version 0.0.1
*
* @date 2018/3/25
*
*/
public class NettyServer {
public static final int TCP_PORT = 20000;
private final int port;
public NettyServer(int port) {
this.port = port;
}
public void start() throws Exception {
EventLoopGroup bossGroup = null;
EventLoopGroup workerGroup = null;
try {
// Server 端引導類
ServerBootstrap serverBootstrap = new ServerBootstrap();
// Boss 線程管理組
bossGroup = new NioEventLoopGroup(1);
// Worker 線程管理組
workerGroup = new NioEventLoopGroup();
// 將 Boss、Worker 設置到 ServerBootstrap 服務端引導類中
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
// 指定通道類型為NioServerSocketChannel,一種異步模式,OIO阻塞模式為OioServerSocketChannel
.localAddress("localhost", port)//設置InetSocketAddress讓服務器監聽某個端口已等待客戶端連接。
.childHandler(new ChannelInitializer() {//設置childHandler執行所有的連接請求
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new PacketHeadDecoder());
ch.pipeline().addLast(new PacketBodyDecoder());
ch.pipeline().addLast(new PacketHeadEncoder());
ch.pipeline().addLast(new PacketBodyEncoder());
ch.pipeline().addLast(new PacketHandler());
}
});
// 最后綁定服務器等待直到綁定完成,調用sync()方法會阻塞直到服務器完成綁定,然后服務器等待通道關閉,因為使用sync(),所以關閉操作也會被阻塞。
ChannelFuture channelFuture = serverBootstrap.bind().sync();
System.out.println("Server started,port:" + channelFuture.channel().localAddress());
channelFuture.channel().closeFuture().sync();
} finally {
// Shut down all event loops to terminate all threads.
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
new NettyServer(TCP_PORT).start();
}
}
三、常用的類結構四、源碼分析Netty服務端啟動4.1、創建bossGroup對象
1、源碼:
// NettyServer.java, Boss 線程管理組, 上面NettyServer.java中的示例代碼
bossGroup = new NioEventLoopGroup(1);
// NioEventLoopGroup.java
/**
* Create a new instance using the specified number of threads, {@link ThreadFactory} and the
* {@link SelectorProvider} which is returned by {@link SelectorProvider#provider()}.
*/
public NioEventLoopGroup(int nThreads) {
this(nThreads, (Executor) null);
}
// NioEventLoopGroup.java
public NioEventLoopGroup(int nThreads, Executor executor) {
this(nThreads, executor, SelectorProvider.provider());
}
// NioEventLoopGroup.java
public NioEventLoopGroup(
int nThreads, Executor executor, final SelectorProvider selectorProvider) {
this(nThreads, executor, selectorProvider, DefaultSelectStrategyFactory.INSTANCE);
}
// NioEventLoopGroup.java
public NioEventLoopGroup(int nThreads, Executor executor, final SelectorProvider selectorProvider,
final SelectStrategyFactory selectStrategyFactory) {
super(nThreads, executor, selectorProvider, selectStrategyFactory, RejectedExecutionHandlers.reject());
}
// MultithreadEventLoopGroup.java
/**
* @see MultithreadEventExecutorGroup#MultithreadEventExecutorGroup(int, Executor, Object...)
*/
protected MultithreadEventLoopGroup(int nThreads, Executor executor, Object... args) {
// DEFAULT_EVENT_LOOP_THREADS 默認為CPU核數的2倍
super(nThreads == 0 ? DEFAULT_EVENT_LOOP_THREADS : nThreads, executor, args);
}
// MultithreadEventExecutorGroup.java
/**
* Create a new instance.
*
* @param nThreads the number of threads that will be used by this instance.
* @param executor the Executor to use, or {@code null} if the default should be used.
* @param args arguments which will passed to each {@link #newChild(Executor, Object...)} call
*/
protected MultithreadEventExecutorGroup(int nThreads, Executor executor, Object... args) {
this(nThreads, executor, DefaultEventExecutorChooserFactory.INSTANCE, args);
}
// MultithreadEventExecutorGroup.java
/**
* Create a new instance.
*
* @param nThreads the number of threads that will be used by this instance.
* @param executor the Executor to use, or {@code null} if the default should be used.
* @param chooserFactory the {@link EventExecutorChooserFactory} to use.
* @param args arguments which will passed to each {@link #newChild(Executor, Object...)} call
*/
protected MultithreadEventExecutorGroup(int nThreads, Executor executor,
EventExecutorChooserFactory chooserFactory, Object... args) {
if (nThreads <= 0) { // 小于或等于零都會直接拋異常,由此可見,要想使用netty,還得必須至少得有1個線程跑起來才能使用
throw new IllegalArgumentException(String.format("nThreads: %d (expected: > 0)", nThreads));
}
if (executor == null) { // 如果調用方不想自己定制線程池的話,那么則用netty自己默認的線程池
executor = new ThreadPerTaskExecutor(newDefaultThreadFactory());
}
children = new EventExecutor[nThreads]; // 構建孩子結點數組,也就是構建NioEventLoopGroup持有的線程數組
for (int i = 0; i < nThreads; i ++) { // 循環線程數,依次創建實例化線程封裝的對象NioEventLoop
boolean success = false;
try {
children[i] = newChild(executor, args); // 最終調用到了NioEventLoopGroup類中的newChild方法
success = true;
} catch (Exception e) {
// TODO: Think about if this is a good exception type
throw new IllegalStateException("failed to create a child event loop", e);
} finally {
if (!success) {
for (int j = 0; j < i; j ++) {
children[j].shutdownGracefully();
}
for (int j = 0; j < i; j ++) {
EventExecutor e = children[j];
try {
while (!e.isTerminated()) {
e.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
}
} catch (InterruptedException interrupted) {
// Let the caller handle the interruption.
Thread.currentThread().interrupt();
break;
}
}
}
}
}
// 實例化選擇線程器,也就是說我們要想執行任務,對于nThreads個線程,我們得靠一個規則來如何選取哪個具體線程來執行任務;
// 那么chooser就是來干這個事情的,它主要是幫我們選出需要執行任務的線程封裝對象NioEventLoop
chooser = chooserFactory.newChooser(children);
final FutureListener
4.2、實例化線程管理組的孩子結點children[i]
1、源碼:
// MultithreadEventExecutorGroup.java, 最終調用到了NioEventLoopGroup類中的newChild方法
children[i] = newChild(executor, args);
// NioEventLoopGroup.java
@Override
protected EventLoop newChild(Executor executor, Object... args) throws Exception {
return new NioEventLoop(this, executor, (SelectorProvider) args[0],
((SelectStrategyFactory) args[1]).newSelectStrategy(), (RejectedExecutionHandler) args[2]);
}
// NioEventLoop.java
NioEventLoop(NioEventLoopGroup parent, Executor executor, SelectorProvider selectorProvider,
SelectStrategy strategy, RejectedExecutionHandler rejectedExecutionHandler) {
// 調用父類的構造方法
// DEFAULT_MAX_PENDING_TASKS 任務隊列初始化容量值,默認值為:Integer.MAX_VALUE
// 若不想使用默認值的話,那么就得自己配置 io.netty.eventLoop.maxPendingTasks 屬性值為自己想要的值
super(parent, executor, false, DEFAULT_MAX_PENDING_TASKS, rejectedExecutionHandler);
if (selectorProvider == null) {
throw new NullPointerException("selectorProvider");
}
if (strategy == null) {
throw new NullPointerException("selectStrategy");
}
// 這個對象在NioEventLoopGroup的構造函數中通過SelectorProvider.provider()獲得,然后一路傳參到此類
provider = selectorProvider;
// 通過調用JDK底層類庫,為每個NioEventLoop配備一個多路復用器
final SelectorTuple selectorTuple = openSelector();
selector = selectorTuple.selector;
unwrappedSelector = selectorTuple.unwrappedSelector;
selectStrategy = strategy;
}
// SingleThreadEventLoop.java
protected SingleThreadEventLoop(EventLoopGroup parent, Executor executor,
boolean addTaskWakesUp, int maxPendingTasks,
RejectedExecutionHandler rejectedExecutionHandler) {
// 調用父類的構造方法
super(parent, executor, addTaskWakesUp, maxPendingTasks, rejectedExecutionHandler);
// 構造任務隊列,最終會調用NioEventLoop的newTaskQueue(int maxPendingTasks)方法
tailTasks = newTaskQueue(maxPendingTasks);
}
// SingleThreadEventExecutor.java
/**
* Create a new instance
*
* @param parent the {@link EventExecutorGroup} which is the parent of this instance and belongs to it
* @param executor the {@link Executor} which will be used for executing
* @param addTaskWakesUp {@code true} if and only if invocation of {@link #addTask(Runnable)} will wake up the
* executor thread
* @param maxPendingTasks the maximum number of pending tasks before new tasks will be rejected.
* @param rejectedHandler the {@link RejectedExecutionHandler} to use.
*/
protected SingleThreadEventExecutor(EventExecutorGroup parent, Executor executor,
boolean addTaskWakesUp, int maxPendingTasks,
RejectedExecutionHandler rejectedHandler) {
// 調用父類的構造方法
super(parent);
this.addTaskWakesUp = addTaskWakesUp; // 添加任務時是否需要喚醒多路復用器的阻塞狀態
this.maxPendingTasks = Math.max(16, maxPendingTasks);
this.executor = ObjectUtil.checkNotNull(executor, "executor");
taskQueue = newTaskQueue(this.maxPendingTasks);
rejectedExecutionHandler = ObjectUtil.checkNotNull(rejectedHandler, "rejectedHandler");
}
// AbstractScheduledEventExecutor.java
protected AbstractScheduledEventExecutor(EventExecutorGroup parent) {
// 調用父類的構造方法
super(parent);
}
// AbstractEventExecutor.java
protected AbstractEventExecutor(EventExecutorGroup parent) {
this.parent = parent;
}
2、該流程主要實例化線程管理組的孩子結點children[i],孩子結點的類型為NioEventLoop類型;
3、仔細一看,netty的開發者對命名也很講究,線程管理組的類名為NioEventLoopGroup,線程管理組的子線程類名為NioEventLoop,
有沒有發現有什么不一樣的地方?其實就是差了個Group幾個字母,線程管理組自然以Group結尾,不是組的就自然沒有Group字母;
4、每個NioEventLoop都持有組的線程池executor對象,方便添加task到任務隊列中;
5、每個NioEventLoop都有一個selector多路復用器,而那些Channel就是注冊到這個玩意上面的;
6、每個NioEventLoop都有一個任務隊列,而且這個隊列的初始化容器大小為1024;
4.3、如何構建任務隊列
1、源碼:
// SingleThreadEventLoop.java, 構造任務隊列,最終會調用NioEventLoop的newTaskQueue(int maxPendingTasks)方法
tailTasks = newTaskQueue(maxPendingTasks);
// NioEventLoop.java
@Override
protected Queue newTaskQueue(int maxPendingTasks) {
// This event loop never calls takeTask()
// 由于默認是沒有配置io.netty.eventLoop.maxPendingTasks屬性值的,所以maxPendingTasks默認值為Integer.MAX_VALUE;
// 那么最后配備的任務隊列的大小也就自然使用無參構造隊列方法
return maxPendingTasks == Integer.MAX_VALUE ? PlatformDependent.newMpscQueue()
: PlatformDependent.newMpscQueue(maxPendingTasks);
}
// PlatformDependent.java
/**
* Create a new {@link Queue} which is safe to use for multiple producers (different threads) and a single
* consumer (one thread!).
* @return A MPSC queue which may be unbounded.
*/
public static Queue newMpscQueue() {
return Mpsc.newMpscQueue();
}
// Mpsc.java
static Queue newMpscQueue() {
// 默認值 MPSC_CHUNK_SIZE = 1024;
return USE_MPSC_CHUNKED_ARRAY_QUEUE ? new MpscUnboundedArrayQueue(MPSC_CHUNK_SIZE)
: new MpscUnboundedAtomicArrayQueue(MPSC_CHUNK_SIZE);
}
2、這里主要看看NioEventLoop是如何構建任務隊列的,而且還構建了一個給定初始化容量值大小的隊列;
4.4、如何獲得多路復用器
1、源碼:
// NioEventLoop.java, 通過調用JDK底層類庫,為每個NioEventLoop配備一個多路復用器
final SelectorTuple selectorTuple = openSelector();
selector = selectorTuple.selector;
unwrappedSelector = selectorTuple.unwrappedSelector;
selectStrategy = strategy;
// NioEventLoop.java
private SelectorTuple openSelector() {
final Selector unwrappedSelector;
try {
// 通過 provider 調用底層獲取一個多路復用器對象
unwrappedSelector = provider.openSelector();
} catch (IOException e) {
throw new ChannelException("failed to open a new selector", e);
}
// DISABLE_KEYSET_OPTIMIZATION: 是否優化選擇器key集合,默認為不優化
if (DISABLE_KEYSET_OPTIMIZATION) {
return new SelectorTuple(unwrappedSelector);
}
// 執行到此,說明需要優化選擇器集合,首先創建一個選擇器集合
final SelectedSelectionKeySet selectedKeySet = new SelectedSelectionKeySet();
// 然后通過反射找到SelectorImpl對象
Object maybeSelectorImplClass = AccessController.doPrivileged(new PrivilegedAction() {
@Override
public Object run() {
try {
// 通過反射獲取SelectorImpl實現類對象
return Class.forName(
"sun.nio.ch.SelectorImpl",
false,
PlatformDependent.getSystemClassLoader());
} catch (Throwable cause) {
return cause;
}
}
});
if (!(maybeSelectorImplClass instanceof Class) ||
// ensure the current selector implementation is what we can instrument.
!((Class>) maybeSelectorImplClass).isAssignableFrom(unwrappedSelector.getClass())) {
if (maybeSelectorImplClass instanceof Throwable) {
Throwable t = (Throwable) maybeSelectorImplClass;
logger.trace("failed to instrument a special java.util.Set into: {}", unwrappedSelector, t);
}
return new SelectorTuple(unwrappedSelector);
}
final Class> selectorImplClass = (Class>) maybeSelectorImplClass;
// 以下run方法的主要目的就是將我們自己創建的selectedKeySet選擇器集合通過反射替換底層自帶的選擇器集合
Object maybeException = AccessController.doPrivileged(new PrivilegedAction() {
@Override
public Object run() {
try {
Field selectedKeysField = selectorImplClass.getDeclaredField("selectedKeys");
Field publicSelectedKeysField = selectorImplClass.getDeclaredField("publicSelectedKeys");
Throwable cause = ReflectionUtil.trySetAccessible(selectedKeysField, true);
if (cause != null) {
return cause;
}
cause = ReflectionUtil.trySetAccessible(publicSelectedKeysField, true);
if (cause != null) {
return cause;
}
selectedKeysField.set(unwrappedSelector, selectedKeySet);
publicSelectedKeysField.set(unwrappedSelector, selectedKeySet);
return null;
} catch (NoSuchFieldException e) {
return e;
} catch (IllegalAccessException e) {
return e;
}
}
});
if (maybeException instanceof Exception) {
selectedKeys = null;
Exception e = (Exception) maybeException;
logger.trace("failed to instrument a special java.util.Set into: {}", unwrappedSelector, e);
return new SelectorTuple(unwrappedSelector);
}
// 反射執行完后,則將創建的selectedKeySet賦值為當成員變量
selectedKeys = selectedKeySet;
logger.trace("instrumented a special java.util.Set into: {}", unwrappedSelector);
return new SelectorTuple(unwrappedSelector,
new SelectedSelectionKeySetSelector(unwrappedSelector, selectedKeySet));
}
2、其實說獲得多路復用器,倒不如說多路復用器從何而來,是通過provider調用provider.openSelector()方法而獲得的;
3、而這個provider所產生的地方其內部是一個靜態變量,細心的童鞋會發現SelectorProvider.provider()這個里面還真有一個靜態provider;
4、而這里給用戶做了一個選擇是否需要優化選擇器,如果需要優化則用自己創建的選擇器通過反射塞到底層的多路復用器對象中;
4.5、線程選擇器
1、源碼:
// MultithreadEventExecutorGroup.java
// 實例化選擇線程器,也就是說我們要想執行任務,對于nThreads個線程,我們得靠一個規則來如何選取哪個具體線程來執行任務;
// 那么chooser就是來干這個事情的,它主要是幫我們選出需要執行任務的線程封裝對象NioEventLoop
chooser = chooserFactory.newChooser(children);
// DefaultEventExecutorChooserFactory.java
@SuppressWarnings("unchecked")
@Override
public EventExecutorChooser newChooser(EventExecutor[] executors) {
if (isPowerOfTwo(executors.length)) {
return new PowerOfTwoEventExecutorChooser(executors);
} else {
return new GenericEventExecutorChooser(executors);
}
}
// PowerOfTwoEventExecutorChooser.java
private static final class PowerOfTwoEventExecutorChooser implements EventExecutorChooser {
private final AtomicInteger idx = new AtomicInteger();
private final EventExecutor[] executors;
PowerOfTwoEventExecutorChooser(EventExecutor[] executors) {
this.executors = executors;
}
@Override
public EventExecutor next() {
return executors[idx.getAndIncrement() & executors.length - 1];
}
}
// GenericEventExecutorChooser.java
private static final class GenericEventExecutorChooser implements EventExecutorChooser {
private final AtomicInteger idx = new AtomicInteger();
private final EventExecutor[] executors;
GenericEventExecutorChooser(EventExecutor[] executors) {
this.executors = executors;
}
@Override
public EventExecutor next() {
return executors[Math.abs(idx.getAndIncrement() % executors.length)];
}
}
2、記得在前面說過,在實例化線程組Group的時候,會實例化一個線程選擇器,而這個選擇器的實現方式也正是由通過線程數量來決定的;
3、PowerOfTwoEventExecutorChooser與GenericEventExecutorChooser的主要區別就是,當線程個數為2的n次方的話,那么則用PowerOfTwoEventExecutorChooser實例化的選擇器;
4、因為EventExecutorChooser的next()方法,一個是與操作,一個是求余操作,而與操作的效率稍微高些,所以在選擇線程這個細小的差別,netty的開發人員也真實一絲不茍的處理;