学习Netty BootStrap的核心知识,成为网络编程高手!

0 定义

深入 ChannelPipeline、ChannelHandler 和 EventLoop 后,如何将这些部分组织起来,成为可运行的应用程序?

引导(Bootstrapping)!引导一个应用程序是指对它进行配置,并使它运行起来的过程—尽管该过程的具体细节可能并不如它的定义那样简单,尤其是对于一个网络应用程序来说。

和它对应用程序体系架构的分层抽象一致,Netty处理引导的方式使你的【应用程序的逻辑或实现】和【网络层】相

隔离,而无论它是客户端还是服务器。所有的框架组件都将会在后台结合在一起并启用。引导是我们一直以来都在组装的完整拼图(Netty 的核心概念以及组件,也包括如何完整正确地组织并且运行一个 Netty 应用程序)中缺失的那一块。当你把它放到正确的位置上时,你的Netty应用程序就完整了。

1 Bootstrap 类

引导类的层次结构包括一个抽象父类和两个具体的引导子类:

相比于将具体的引导类分别看作用于服务器、客户端的引导,记住它们的本意是用来支撑不同的应用程序的功能的更有裨益,即:

  • 服务器致力于使用一个父 Channel 接受来自客户端的连接,并创建子 Channel 用于它们之间的通信

  • 而客户端将最可能只需要一个单独的、没有父 Channel 的 Channel 用于所有的网络交互

    正如同我们将看到的,这也适用于无连接的传输协议,如 UDP,因为它们并不是每个连接都需要一个单独的 Channel

客户端和服务器两种应用程序类型之间通用的引导步骤由 AbstractBootstrap 处理,而特定于客户端或服务器的引导步骤则分别由 Bootstrap 或 ServerBootstrap 处理。

接下来将详细地探讨这两个类,首先从不那么复杂的 Bootstrap 类开始。

1.1 为何引导类是 Cloneable

有时可能需要创建多个类似或完全相同配置的Channel。为支持这种模式而又无需为每个 Channel 都创建并配置一个新的引导类实例, AbstractBootstrap 被标记为 Cloneable。在一个已配置完成的引导类实例上调用clone()方法将返回另一个可立即使用的引导类实例。

这种方式只会创建引导类实例的EventLoopGroup的浅拷贝,所以,【被浅拷贝的 EventLoopGroup】将在所有克隆的Channel实例之间共享。这能接受,因为通常这些克隆的Channel的生命周期都很短暂,一个典型场景:创建一个Channel以进行一次HTTP请求。

AbstractBootstrap 类的完整声明:

// 子类型 B 是其父类型的一个类型参数,因此可以返回到运行时实例的引用以支持方法的链式调用(流式语法)
public abstract class AbstractBootstrap<B extends AbstractBootstrap<B, C>, C extends Channel> implements Cloneable {
    
    

其子类的声明:

public class Bootstrap extends AbstractBootstrap<Bootstrap, Channel> {
    
    
}

public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap, ServerChannel> {
    
    
}

2 引导客户端和无连接协议

Bootstrap类被用于客户端或使用了无连接协议的应用程序。表8-1很多继承自AbstractBootstrap:

表 8-1:Bootstrap类的API

2.1 引导客户端

Bootstrap 类负责为客户端和使用无连接协议的应用程序创建 Channel,如图 8-2:

图 8-2:引导过程

代码清单 8-1 引导了一个使用 NIO TCP 传输的客户端。

代码清单 8-1:引导一个客户端

EventLoopGroup group = new NioEventLoopGroup();
// 创建一个Bootstrap类的实例,以创建和连接新的客户端
Channel
Bootstrap bootstrap = new Bootstrap();

/**
 * 使用流式语法;这些方法(除了connect()方法)将通过每次方法调用所返回的对 Bootstrap 实例的引用
 * 链接在一起。
 */

// 设置 EventLoopGroup,提供用于处理 Channel 事件的 EventLoop
bootstrap.group(group)
  			 // 指定要使用的 Channel 实现
        .channel(NioSocketChannel.class)
  			 // 设置用于 Channel 事件和数据的ChannelInboundHandler
        .handler(new SimpleChannelInboundHandler<ByteBuf>() {
    
    
            @Override
            protected void channelRead0(
                    ChannelHandlerContext channelHandlerContext,
                    ByteBuf byteBuf) throws Exception {
    
    
                System.out.println("Received data");
            }
        });
// 连接到远程主机
ChannelFuture future = bootstrap.connect(new InetSocketAddress("www.JavaEdge.com", 80));
future.addListener(new ChannelFutureListener() {
    
    
    @Override
    public void operationComplete(ChannelFuture channelFuture) throws Exception {
    
    
        if (channelFuture.isSuccess()) {
    
    
            System.out.println("Connection established");
        } else {
    
    
            System.err.println("Connection attempt failed");
            channelFuture.cause().printStackTrace();
        }
    }
});

2.2 Channel 和 EventLoopGroup 的兼容性

代码清单 8-2 所示目录清单来自 io.netty.channel 包。从包名及类名前缀可见,对 NIO 及 OIO 传输,都有相关EventLoopGroup 和 Channel 实现。

相互兼容的 EventLoopGroup 和 Channel:

必须保持这种兼容性,不能混用具有不同前缀的组件,如 NioEventLoopGroup 和 OioSocketChannel。代码清单 8-3 展示了试图这样做的一个例子

EventLoopGroup group = new NioEventLoopGroup();
// 创建一个新的 Bootstrap类的实例,以创建新的客户端Channel
Bootstrap bootstrap = new Bootstrap();
// 指定一个适用于 NIO 的 EventLoopGroup 实现
bootstrap.group(group)
  			 // 指定一个适用于OIO 的 Channel 实现类
        .channel(OioSocketChannel.class)
  			 // 设置一个用于处理 Channel的 I/O 事件和数据的ChannelInboundHandler
        .handler(new SimpleChannelInboundHandler<ByteBuf>() {
    
    
            @Override
            protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf)
                    throws Exception {
    
    
                System.out.println("Received data");
            }
        });
// 尝试连接到远程节点
ChannelFuture future = bootstrap.connect(new InetSocketAddress("www.JavaEdge.com", 80));
future.syncUninterruptibly();

这段代码将会导致 IllegalStateException,因为它混用了不兼容的传输

Exception in thread "main" java.lang.IllegalStateException: incompatible event loop type: io.netty.channel.nio.NioEventLoop
	at io.netty.channel.AbstractChannel$AbstractUnsafe.register(AbstractChannel.java:462)

IllegalStateException

引导的过程中,在调用 bind()或 connect()前,必须调用以下方法来设置所需的组件:

  • group()
  • channel()或者 channelFactory()
  • handler()

若不这样做,将导致 IllegalStateException。对 handler()方法的调用尤其重要,因为它需要配置好 ChannelPipeline。

3 引导服务器

从 ServerBootstrap API 概要视图开始对服务器引导过程的概述。然后,探讨引导服务器过程中所涉及的几个步骤及几个相关的主题,包含从一个 ServerChannel 的子 Channel 中引导一个客户端这样的特殊情况。

3.1 ServerBootstrap 类

表 8-2 列出了 ServerBootstrap 类的方法:

表8-2 ServerBootstrap类的方法

3.2 引导服务器

表 8-2 中列出一些表 8-1 不存在的方法:childHandler()、childAttr()和 childOption()。这些调用支持特别用于服务器应用程序的操作。

ServerChannel 的实现负责创建子 Channel,这些子 Channel 代表了已被接受的连接。因此,负责引导 ServerChannel 的 ServerBootstrap 提供了这些方法,以简化将设置应用到已被接受的子 Channel 的 ChannelConfig 的任务。

图 8-3 展示 ServerBootstrap 在 bind()方法被调用时创建了一个 ServerChannel,并且该 ServerChannel 管理了多个子 Channel。

图 8-3:ServerBootstrap 和 ServerChannel

代码8-4 实现图 8-3 中所展示的服务器的引导过程:

package io.netty.example.cp8;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.net.InetSocketAddress;

/**
 * 代码清单 8-4 引导服务器
 *
 * @author JavaEdge
 * @date 2023/5/20
 */
public class Demo84 {
    
    

    public static void main(String[] args) {
    
    
        NioEventLoopGroup group = new NioEventLoopGroup();
        // 创建 ServerBootstrap
        ServerBootstrap bootstrap = new ServerBootstrap();
        // 设置 EventLoopGroup,其提供了用于处理Channel 事件的EventLoop
        bootstrap.group(group)
                // 指定要使用的 Channel 实现
                .channel(NioServerSocketChannel.class)
                // 设置用于处理已被接受的子Channel的I/O及数据的 ChannelInboundHandler
                .childHandler(new SimpleChannelInboundHandler<ByteBuf>() {
    
    
                    @Override
                    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf byteBuf) throws Exception {
    
    
                        System.out.println("Received data");
                    }
                });
        // 通过配置好的ServerBootstrap的实例绑定该Channel
        ChannelFuture future = bootstrap.bind(new InetSocketAddress(8080));
        future.addListener(new ChannelFutureListener() {
    
    
            @Override
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
    
    
                if (channelFuture.isSuccess()) {
    
    
                    System.out.println("Server bound");
                } else {
    
    
                    System.err.println("Bound attempt failed");
                    channelFuture.cause().printStackTrace();
                }
            }
        });
    }
}

4 从 Channel 引导客户端

服务器正在处理一个客户端请求,该请求需要它充当第三方系统的客户端。当一个应用程序(如一个代理服务器)必须要和现有的系统(如 Web 服务或数据库)集成时,就可能发生这种情况。此时,将需要从已被接受的子 Channel 中引导一个客户端 Channel。

可按 8.2.1 节中所描述的方式创建新的 Bootstrap 实例,但是这并不是最高效的解决方案,因为它要求你为每个新创建的客户端 Channel 定义另一个 EventLoop,会产生额外的线程,以及在已被接受的子 Channel 和客户端 Channel 之间交换数据时不可避免的上下文切换。

更好的解决方案

将已被接受的子 Channel 的 EventLoop 传递给 Bootstrap 的 group()方法来共享该 EventLoop。因为分配给 EventLoop 的所有 Channel 都使用同一线程,所以这避免了:

  • 额外的线程创建
  • 前面所提到的相关的上下文切换

该共享的解决方案图:

图 8-4:在两个 Channel 之间共享 EventLoop

实现 EventLoop 共享涉及通过调用 group()方法来设置 EventLoop,如代码8-5:

package io.netty.example.cp8;

import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;

/**
 * 代码清单 8-5 引导服务器
 *
 * @author JavaEdge
 * @date 2023/5/20
 */
public class Demo85 {
    
    

    public static void main(String[] args) {
    
    
        // 创建 ServerBootstrap 以创建ServerSocketChannel,并绑定它
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        // 设置 EventLoopGroup,其将提供用以处理 Channel 事件的 EventLoop
        serverBootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup())
                // 指定要使用的 Channel 实现
                .channel(NioServerSocketChannel.class)
                // 设置用于处理已被接受的 子 Channel 的 I/O 和数据的ChannelInboundHandler
                .childHandler(
                        new SimpleChannelInboundHandler<ByteBuf>() {
    
    
                            ChannelFuture connFuture;

                            @Override
                            public void channelActive(ChannelHandlerContext ctx) throws Exception {
    
    
                                // 创建一个 Bootstrap 类的实例以连接到远程主机
                                Bootstrap bootstrap = new Bootstrap();
                                // 指定 Channel 的实现
                                bootstrap.channel(NioSocketChannel.class)
                                        // 为入站 I/O 设置 ChannelInboundHandler
                                        .handler(
                                                new SimpleChannelInboundHandler<ByteBuf>() {
    
    
                                                    @Override
                                                    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    
    
                                                        System.out.println("Received data");
                                                    }
                                                });
                                // 使用与分配给已被接受的子Channel相同的EventLoop
                                bootstrap.group(ctx.channel().eventLoop());
                                // 连接到远程节点
                                connFuture = bootstrap.connect(new InetSocketAddress("www.manning.com", 80));
                            }

                            @Override
                            protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
    
    
                                if (connFuture.isDone()) {
    
    
                                    // 当连接完成时,执行一些数据操作(如代理)
                                    // do something with the data
                                    System.out.println();
                                }
                            }
                        });
        // 通过配置好的ServerBootstrap绑定该 ServerSocketChannel
        ChannelFuture future = serverBootstrap.bind(new InetSocketAddress(8080));
        future.addListener(new ChannelFutureListener() {
    
    
            @Override
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
    
    
                if (channelFuture.isSuccess()) {
    
    
                    System.out.println("Server bound");
                } else {
    
    
                    System.err.println("Bind attempt failed");
                    channelFuture.cause().printStackTrace();
                }
            }
        });
    }
}

这一节中所讨论的主题以及所提出的解决方案都反映了编写 Netty 应用程序的一个一般准则:尽可能复用 EventLoop,以减少线程创建所带来的开销。

5 在引导过程中添加多个 ChannelHandler

猜你喜欢

转载自blog.csdn.net/qq_33589510/article/details/130783163