原理剖析-Netty之服务端启动工作原理分析(上)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                原理剖析-Netty之服务端启动工作原理分析(上)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                一、大致介紹
1、Netty這個詞,對于熟悉并發的童鞋一點都不陌生,它是一個異步事件驅動型的網絡通信框架; 2、使用Netty不需要我們關注過多NIO的API操作,簡簡單單的使用即可,非常方便,開發門檻較低; 3、而且Netty也經歷了各大著名框架的“摧殘”,足以證明其性能高,穩定性高; 4、那么本章節就來和大家分享分析一下Netty的服務端啟動流程,分析Netty的源碼版本為:netty-netty-4.1.22.Final;二、簡單認識Netty
2.1 何為Netty?
1、是一個基于NIO的客戶端、服務器端的網絡通信框架;2、是一個以提供異步的、事件驅動型的網絡應用工具;3、可以供我們快速開發高性能的、高可靠性的網絡服務器與客戶端;2.2 為什么使用Netty?
1、開箱即用,簡單操作,開發門檻低,API簡單,只需關注業務實現即可,不用關心如何編寫NIO;2、自帶多種協議棧且預置多種編解碼功能,且定制化能力強;3、綜合性能高,已歷經各大著名框架(RPC框架、消息中間件)等廣泛驗證,健壯性非常強大;4、相對于JDK的NIO來說,netty在底層做了很多優化,將reactor線程的并發處理提到了極致;5、社區相對較活躍,遇到問題可以隨時提問溝通并修復;2.3 大致闡述啟動流程
1、創建兩個線程管理組,一個是bossGroup,一個是workerGroup,每個Group下都有一個線程組children[i]來執行任務;2、bossGroup專門用來攬客的,就是接收客戶端的請求鏈接,而workerGroup專門用來干事的,bossGroup攬客完了就交給workerGroup去干活了;3、通過bind輕松的一句代碼綁定注冊,其實里面一點都不簡單,一堆堆的操作;4、創建NioServerSocketChannel,并且將此注冊到bossGroup的子線程中的多路復用器上;5、最后一步就是將NioServerSocketChannel綁定到指定ip、port即可,由此完成服務端的整個啟動過程;2.4 Netty服務端啟動Demo
/*** 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<Channel>() {//設置childHandler執行所有的連接請求三、常用的類結構
四、源碼分析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轉載于:https://www.cnblogs.com/hd-zg/p/8724576.html
總結
以上是生活随笔為你收集整理的原理剖析-Netty之服务端启动工作原理分析(上)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 关于update set from wh
- 下一篇: Django学习笔记(一):第一个dja
