Netty源码分析(一) ServerBootStrap

先来说说为什么要写netty源码分析的文章,一个方面是自己看了一些源码,却找不到了解原理的方式,一个方面是万一bat哪个大牛看到我写的文章,给我一个5k的工作呢。不开玩笑了,在学习netty之前,需要先学习java nio的知识,如果有不了解java nio的,可以先学习一下java nio方面的知识。下面我先介绍一个netty:
Netty是一个高性能、异步事件驱动的NIO框架,提供了对TCP、UDP和文件传输的支持,作为一个异步NIO框架,Netty的所有IO操作都是异步非阻塞的,通过Future-Listener机制,用户可以方便的主动获取或者通过通知机制获得IO操作结果。

作为当前最流行的NIO框架,Netty在互联网领域、大数据分布式计算领域、游戏行业、通信行业等获得了广泛的应用,一些业界著名的开源组件也基于Netty构建,比如RPC框架、zookeeper等。
netty性能高的原因是netty的reactor线程模型


Reactor线程模型

先来一个netty的基本实现:
server 代码实现

 public class EchoServer {
 private final int port;
 public EchoServer(int port) {
     this.port = port;
 }

public void run() throws Exception {
    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup();  // (1)
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap(); // (2)
        b.group(bossGroup, workerGroup)
         .channel(NioServerSocketChannel.class) // (3)
         .option(ChannelOption.SO_BACKLOG, 100)
         .handler(new LoggingHandler(LogLevel.INFO))
         .childHandler(new ChannelInitializer() { // (4)
             @Override
             public void initChannel(SocketChannel ch) throws Exception {
                 ch.pipeline().addLast(
                         //new LoggingHandler(LogLevel.INFO),
                         new EchoServerHandler());
             }
         });

        // Start the server.
        ChannelFuture f = b.bind(port).sync(); // (5)

        // Wait until the server socket is closed.
        f.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 {
    int port;
    if (args.length > 0) {
        port = Integer.parseInt(args[0]);
    } else {
        port = 8080;
    }
    new EchoServer(port).run();
}
}

EchoServerHandler 实现
public class EchoServerHandler extends ChannelInboundHandlerAdapter {

private static final Logger logger = Logger.getLogger(
        EchoServerHandler.class.getName());

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ctx.write(msg);
}

@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    ctx.flush();
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    // Close the connection when an exception is raised.
    logger.log(Level.WARNING, "Unexpected exception from downstream.", cause);
    ctx.close();
}
}

client 代码实现

public class EchoClient {

private final String host;
private final int port;
private final int firstMessageSize;

public EchoClient(String host, int port, int firstMessageSize) {
    this.host = host;
    this.port = port;
    this.firstMessageSize = firstMessageSize;
}

public void run() throws Exception {
    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group)
         .channel(NioSocketChannel.class)
         .option(ChannelOption.TCP_NODELAY, true)
         .handler(new ChannelInitializer() {
             @Override
             public void initChannel(SocketChannel ch) throws Exception {
                 ch.pipeline().addLast(
                         //new LoggingHandler(LogLevel.INFO),
                         new EchoClientHandler(firstMessageSize));
             }
         });

        // Start the client.
        ChannelFuture f = b.connect(host, port).sync();

        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down the event loop to terminate all threads.
        group.shutdownGracefully();
    }
}

public static void main(String[] args) throws Exception {
    final String host = args[0];
    final int port = Integer.parseInt(args[1]);
    final int firstMessageSize;
    if (args.length == 3) {
        firstMessageSize = Integer.parseInt(args[2]);
    } else {
        firstMessageSize = 256;
    }

    new EchoClient(host, port, firstMessageSize).run();
}
}

EchoClientHandler 实现
public class EchoClientHandler extends ChannelInboundHandlerAdapter {

private static final Logger logger = Logger.getLogger(
        EchoClientHandler.class.getName());

private final ByteBuf firstMessage;

/**
 * Creates a client-side handler.
 */
public EchoClientHandler(int firstMessageSize) {
    if (firstMessageSize <= 0) {
        throw new IllegalArgumentException("firstMessageSize: " + firstMessageSize);
    }
    firstMessage = Unpooled.buffer(firstMessageSize);
    for (int i = 0; i < firstMessage.capacity(); i ++) {
        firstMessage.writeByte((byte) i);
    }
}

@Override
public void channelActive(ChannelHandlerContext ctx) {
    ctx.writeAndFlush(firstMessage);
}

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ctx.write(msg);
}

@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
   ctx.flush();
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    // Close the connection when an exception is raised.
    logger.log(Level.WARNING, "Unexpected exception from downstream.", cause);
    ctx.close();
}
}

1、NioEventLoopGroup是用来处理I/O操作的线程池,Netty对 EventLoopGroup 接口针对不同的传输协议提供了不同的实现。在本例子中,需要实例化两个NioEventLoopGroup,通常第一个称为“boss”,用来accept客户端连接,另一个称为“worker”,处理客户端数据的读写操作。 2、ServerBootstrap是启动服务的辅助类,有关socket的参数可以通过ServerBootstrap进行设置。 3、这里指定NioServerSocketChannel类初始化channel用来接受客户端请求。 4、通常会为新SocketChannel通过添加一些handler,来设置ChannelPipeline。ChannelInitializer 是一个特殊的handler,其中initChannel方法可以为SocketChannel 的pipeline添加指定handler。 5、通过绑定端口8080,就可以对外提供服务了。

下面我们要说的就是netty的启动辅助类ServerBootStrap。
ServerBootStrap主要就是包含两个NioEventLoopGroup,每个NioEventLoopGroup由一个或多个NioEventLoop组成,我们来看看ServerBootStrap的结构:

ServerBootStrap

其实很简单,就是继承自AbstractBootstrap,这个类有一些通用的属性,比如NioEventLoopGroup等。
我们主要来看doBind方法:

private ChannelFuture doBind(final SocketAddress localAddress) {
    // 初始化并注册一个 Channel 对象,因为注册是异步的过程,所以返回一个 ChannelFuture 对象。
    final ChannelFuture regFuture = initAndRegister();
    final Channel channel = regFuture.channel();
    if (regFuture.cause() != null) { // 若发生异常,直接进行返回。
        return regFuture;
    }

    // 绑定 Channel 的端口,并注册 Channel 到 SelectionKey 中。
    if (regFuture.isDone()) { // 未
        // At this point we know that the registration was complete and successful.
        ChannelPromise promise = channel.newPromise();
        doBind0(regFuture, channel, localAddress, promise); // 绑定
        return promise;
    } else {
        // Registration future is almost always fulfilled already, but just in case it's not.
        final PendingRegistrationPromise promise = new PendingRegistrationPromise(channel);
        regFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                System.out.println(Thread.currentThread() + ": PendingRegistrationPromise");
                Throwable cause = future.cause();
                if (cause != null) {
                    // Registration on the EventLoop failed so fail the ChannelPromise directly to not cause an
                    // IllegalStateException once we try to access the EventLoop of the Channel.
                    promise.setFailure(cause);
                } else {
                    // Registration was successful, so set the correct executor to use.
                    // See https://github.com/netty/netty/issues/2586
                    promise.registered();

                    doBind0(regFuture, channel, localAddress, promise); // 绑定
                }
            }
        });
        return promise;
    }
}

通过doBind方法绑定好端口后,调用initAndRegister方法,1、方法initAndRegister返回一个ChannelFuture实例regFuture,通过regFuture可以判断initAndRegister执行结果。 2、如果regFuture.isDone()为true,说明initAndRegister已经执行完,则直接执行doBind0进行socket绑定。 3、否则regFuture添加一个ChannelFutureListener监听,当initAndRegister执行完成时,调用operationComplete方法并执行doBind0进行socket绑定。

Channel channel = null;
    try {
        // 创建 Channel 对象
        channel = channelFactory.newChannel();
        // 初始化 Channel 配置
        init(channel);
    } catch (Throwable t) {
        if (channel != null) { // 已创建 Channel 对象
            // channel can be null if newChannel crashed (eg SocketException("too many open files"))
            channel.unsafe().closeForcibly(); // 强制关闭 Channel
            // as the Channel is not registered yet we need to force the usage of the GlobalEventExecutor
            return new DefaultChannelPromise(channel, GlobalEventExecutor.INSTANCE).setFailure(t);
        }
        // as the Channel is not registered yet we need to force the usage of the GlobalEventExecutor
        return new DefaultChannelPromise(new FailedChannel(), 
       GlobalEventExecutor.INSTANCE).setFailure(t);
    }

    // 注册 Channel 到 EventLoopGroup 中
    ChannelFuture regFuture = config().group().register(channel);
    if (regFuture.cause() != null) {
        if (channel.isRegistered()) {
            channel.close();
        } else {
            channel.unsafe().closeForcibly(); // 强制关闭 Channel
        }
    }

    // If we are here and the promise is not failed, it's one of the following cases:
    // 1) If we attempted registration from the event loop, the registration has been completed at this point.
    //    i.e. It's safe to attempt bind() or connect() now because the channel has been registered.
    // 2) If we attempted registration from the other thread, the registration request has been successfully
    //    added to the event loop's task queue for later execution.
    //    i.e. It's safe to attempt bind() or connect() now:
    //         because bind() or connect() will be executed *after* the scheduled registration task is executed
    //         because register(), bind(), and connect() are all bound to the same thread.

    return regFuture;

initAndRegister创建了NioServerSocketChannel实例,并为NioServerSocketChannel的pipeline添加handler,再将NioServerSocketChannel注册到Selector上。
在NioServerSockerChannel创建完成后,调用pipeline的Head结点的read方法。服务端启动的流程就分析完了。

你可能感兴趣的:(Netty源码分析(一) ServerBootStrap)