一、大致介绍

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执行所有的连接请求 @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 

转载于:https://www.cnblogs.com/hd-zg/p/8724576.html

原理剖析-Netty之服务端启动工作原理分析(上)相关推荐

  1. Netty实战 IM即时通讯系统(四)服务端启动流程

    ## Netty实战 IM即时通讯系统(四)服务端启动流程 零. 目录 IM系统简介 Netty 简介 Netty 环境配置 服务端启动流程 实战: 客户端和服务端双向通信 数据传输载体ByteBuf ...

  2. 《netty入门与实战》笔记-02:服务端启动流程

    为什么80%的码农都做不了架构师?>>>    1.服务端启动流程 这一小节,我们来学习一下如何使用 Netty 来启动一个服务端应用程序,以下是服务端启动的一个非常精简的 Demo ...

  3. 服务端_说说Netty服务端启动流程

    点击上方☝SpringForAll社区 轻松关注!及时获取有趣有料的技术文章 本文来源:http://yeming.me/2016/03/12/netty1/ netty服务端代码分析 服务端启动配置 ...

  4. 图说Netty服务端启动过程

    来源:逐码 我们知道Netty是一个基于JDK的nio实现的网络编程框架,那Netty的服务端是怎么启动的呢,包括他是何时register 的,何时 bind 端口的,以及何时开始读取网络中的数据的? ...

  5. Netty 源码解析系列-服务端启动流程解析

    netty源码解析系列 Netty 源码解析系列-服务端启动流程解析 Netty 源码解析系列-客户端连接接入及读I/O解析 五分钟就能看懂pipeline模型 -Netty 源码解析 1.服务端启动 ...

  6. 【闪电侠学netty】第4章 服务端启动流程

    [Netty]读书笔记 - 跟闪电侠学 1. 内容概要 1 服务端启动最小化代码 启动服务器步骤 Step1:线程模型,服务器引导类ServerBootstrap Step2:IO 模型 Step3: ...

  7. Zookeeper系列(十)zookeeper的服务端启动详述

    作者:leesf    掌控之中,才会成功:掌控之外,注定失败.出处:http://www.cnblogs.com/leesf456/p/6105276.html尊重原创,大家功能学习进步: 一.前言 ...

  8. netty tcp服务端主动断开客户端_【Netty】服务端和客户端

    欢迎关注公众号:[爱编程] 如果有需要后台回复2019赠送1T的学习资料哦!! 本文是基于Netty4.1.36进行分析 服务端 Netty服务端的启动代码基本都是如下: private void s ...

  9. 图解Java服务端Socket建立原理

    1.前言 1.1 目标 本文通过一个典型的java server socket代码,逐层剖析其tcp协议的服务端建立的原理,其中会涉及到linux内核的实现,本文会以简单通俗的图形将其中原理展示给大家 ...

最新文章

  1. 从玩具到游戏,另类的项目激励机制
  2. linux debian 自动安装,debian系统精简安装
  3. Appboy 基于 MongoDB 的数据密集型实践
  4. windows挂载linux共享,永久挂载 Windows 共享
  5. mac Desktop 在Terminal 无法访问,错误提示:ls: .: Operation not permitted
  6. 已解决:TeamViewer使用的设备数量上限
  7. 高并发编程系列:深入探讨ConcurrentHashMap
  8. 微信小程序实现使用百度云 人脸录入人脸识别功能
  9. 【第十届“泰迪杯”数据挖掘挑战赛】B题:电力系统负荷预测分析 ARIMA、AutoARIMA、LSTM、Prophet、多元Prophet 实现
  10. 安装“万能解码器”还原真实“解码”
  11. 新西兰计算机科学硕士哪所大学最好,2020年新西兰哪些大学计算机科学专业比较好及其优势介绍...
  12. LabVIEW:忽略自动错误处理
  13. HCIA网络基础17-HDLC和PPP
  14. 最长递增子序列 O(NlogN)算法
  15. 老师傅传授,数控车床对刀步骤
  16. 混合式步进电机本体建模
  17. NUC 折腾笔记 - 储存能力测试
  18. np.atleast_2d用法
  19. ArcGIS栅格数据的空间分析讲解(上)
  20. Android打印机--小票打印格式及模板设置

热门文章

  1. ASP开发10条经验总结(网上找的)
  2. 关于个别网段上网时断时续的问题解决
  3. html固定广告位置,如何将广告始终定位到网页右下角
  4. 3.2.3节:特权级
  5. spring4声明式事务--01注解方式
  6. 构建自己的C/C++插件开发框架
  7. tcp断开连接的几种状态
  8. 简单但经典的算法题:有效字母的异位词
  9. STM32的:外部中断线EXTIN和外部中断向量EXTIN_IRQn和中断服务程序入口EXTIN_IRQHandler详解
  10. 文件上传检测的基本思路