zoukankan      html  css  js  c++  java
  • Netty服务端创建流程及组件职责

    public class NettyServer {
        public static void main(String[] args) throws InterruptedException {
            NioEventLoopGroup boosGruop = new NioEventLoopGroup();
            NioEventLoopGroup workerGroup = new NioEventLoopGroup();
    
            ServerBootstrap serverBootstrap = new ServerBootstrap();
    
            serverBootstrap.group(boosGruop,workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<NioSocketChannel>() {
                        @Override
                        protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
                                nioSocketChannel.pipeline().addLast(new FirstServerHandler());
                        }
                    });
    
            ChannelFuture f = serverBootstrap.bind(8000).sync();
            f.channel().closeFuture().sync();
    
        }
    }

    ServerBootStrap 

      服务端的辅助启动类,使用Builder模式隐藏了多个构造器的参数。

    EventLoopGroup

      Reactor线程池。EventLoopGroup是EventLoop数组,EventLoop负责轮询所有注册到Selector上的channel。

    绑定Channel

      用于指定服务端使用何种channel,可选的有NIO(NioServerSocketChannel)和BIO。netty屏蔽了这两种IO在使用上的区别,方便用户在NIO和BIO之间快速切换。由于在channel中传入的是一个类,所以这里是通过反射创建对象。

    ChannelPipeline与ChannelHandler

      用来处理网络时间的责任链,负责管理和执行ChannelHandler,依据不同的网络时间调用ChannelHandler的不同方法去响应网络事件。除了用户自定义的ChannelHandler还有Netty提供的。

    绑定端口

      这一步完成把Channel注册到Selector上

    Selector轮询

      由EventLoop负责调度和执行Selector操作

    调用执行ChannelHandler

      当注册的时间发生后,会调用ChannelPipeline并继而调用响应的ChannelHandler

  • 相关阅读:
    【转】手动写一个Behavior Designer任务节点
    【转】BehaviorDesigner学习
    Behavior trees for AI: How they work
    【转】behave行为树学习使用第一天
    【转】写给笨人的法线贴图原理
    【转】如何把Json格式字符写进text文件中
    【转】系列化与反系列化
    【转】C#中将JSon数据转换成实体类,将实体类转换成Json
    【转】c#处理3种json数据的实例
    【转】Mecanim Animator使用详解
  • 原文地址:https://www.cnblogs.com/AshOfTime/p/10874401.html
Copyright © 2011-2022 走看看