|
| 1 | +<!-- TOC --> |
| 2 | + |
| 3 | + * [Bootstrap引导](#bootstrap引导) |
| 4 | + * [Bootstrap类](#bootstrap类) |
| 5 | + * [引导客户端和无连接协议](#引导客户端和无连接协议) |
| 6 | + * [引导服务端](#引导服务端) |
| 7 | + * [Table of Contents](#table-of-contents) |
| 8 | + |
| 9 | +<!--/ TOC --> |
| 10 | + |
| 11 | +# Bootstrap引导 |
| 12 | +在了解ChanelPipeline,EventLoop等组件之后,我们需要将这些组件组织起来,使其成为一个可运行的应用程序。 |
| 13 | +这里就需要引导这些组件了。 |
| 14 | + |
| 15 | + |
| 16 | +## Bootstrap类 |
| 17 | +引导类的层次结构包括一个抽象的父类和两个具体的引导子类: |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +ServerBootstrap总是需要一个ServerSocketChannel来处理客户端的连接通信,而 |
| 22 | +Bootstrap则只需要一个普通的Channel用于与服务端的通信。 |
| 23 | + |
| 24 | +下面是AbstractBootstrap的主要方法: |
| 25 | + |
| 26 | +| 方法 | 描述 | |
| 27 | +| :--- | :--- | |
| 28 | +| group | 设置用语处理所有事件的EventLoopGroup | |
| 29 | +| channel | 指定服务端或客户端的Channel | |
| 30 | +| channelFactory | 如果引导没有指定Channel,那么可以指定ChannelFactory来创建Channel | |
| 31 | +| localAddress | 指定Channel需要绑定的本地地址,如果不指定,则将由系统随机分配一个地址 | |
| 32 | +| remoteAddress | 设置Channel需要连接的远程地址 | |
| 33 | +| attr | 指定新创建的Channel的属性值 | |
| 34 | +| handler | 设置添加到ChannelPipeline中的ChannelHandler | |
| 35 | +| connect | 连接到远程主机,返回ChannelFuture,用于连接完成的回调 | |
| 36 | +| bind | 绑定指定地址,返回ChannelFuture,用于绑定完成的回调 | |
| 37 | + |
| 38 | + |
| 39 | +### 引导客户端和无连接协议 |
| 40 | +Bootstrap负责Netty应用程序的客户端引导,作为客户端,我们需要使用到connect API来连接到远程 |
| 41 | +服务端,其过程如下: |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +客户端引导的编程模型如下: |
| 46 | + |
| 47 | +````text |
| 48 | +
|
| 49 | +//创建EventLoopGroup |
| 50 | +EventLoopGroup group = new NioEventLoopGroup(); |
| 51 | +//创建客户端引导 |
| 52 | +Bootstrap bootstrap = new Bootstrap(); |
| 53 | +//配置各种属性,如Channel,ChannelHandler等 |
| 54 | +bootstrap.group(group) |
| 55 | + .channel(NioSocketChannel.class) |
| 56 | + .handler(new SimpleChannelInboundHandler<ByteBuf>() { |
| 57 | + @Override |
| 58 | + protected void channeRead0( |
| 59 | + ChannelHandlerContext channelHandlerContext, |
| 60 | + ByteBuf byteBuf) throws Exception { |
| 61 | + System.out.println("Received data"); |
| 62 | + byteBuf.clear(); |
| 63 | + } |
| 64 | + }); |
| 65 | +//连接到远程主机 |
| 66 | +ChannelFuture future = bootstrap.connect( |
| 67 | + new InetSocketAddress("www.manning.com", 80)); |
| 68 | +//设置连接成功后的回调 |
| 69 | +future.addListener(new ChannelFutureListener() { |
| 70 | + @Override |
| 71 | + public void operationComplete(ChannelFuture channelFuture) |
| 72 | + throws Exception { |
| 73 | + if (channelFuture.isSuccess()) { |
| 74 | + System.out.println("Connection established"); |
| 75 | + } else { |
| 76 | + System.err.println("Connection attempt failed"); |
| 77 | + channelFuture.cause().printStackTrace(); |
| 78 | + } |
| 79 | + } |
| 80 | + }); |
| 81 | +
|
| 82 | +```` |
| 83 | + |
| 84 | + |
| 85 | +### 引导服务端 |
| 86 | +ServerBootstrap负责Netty应用程序的服务端引导,作为服务端,我们需要使用bind API来 |
| 87 | +与本地地址绑定,从而接收客户端连接,其过程如下: |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +服务端引导的编程模型如下: |
| 92 | + |
| 93 | +````text |
| 94 | +//创建EventLoopGroup |
| 95 | +NioEventLoopGroup group = new NioEventLoopGroup(); |
| 96 | +//创建服务端引导 |
| 97 | +ServerBootstrap bootstrap = new ServerBootstrap(); |
| 98 | +//配置各种属性,如Channel,ChannelHandler等 |
| 99 | +bootstrap.group(group) |
| 100 | + .channel(NioServerSocketChannel.class) |
| 101 | + .childHandler(new SimpleChannelInboundHandler<ByteBuf>() { |
| 102 | + @Override |
| 103 | + protected void channelRead0(ChannelHandlerContext ctx, |
| 104 | + ByteBuf byteBuf) throws Exception { |
| 105 | + System.out.println("Reveived data"); |
| 106 | + byteBuf.clear(); |
| 107 | + } |
| 108 | + } |
| 109 | + ); |
| 110 | +//绑定本地地址 |
| 111 | +ChannelFuture future = bootstrap.bind(new InetSocketAddress(8080)); |
| 112 | +//设置绑定成功后的回调 |
| 113 | +future.addListener(new ChannelFutureListener() { |
| 114 | + @Override |
| 115 | + public void operationComplete(ChannelFuture channelFuture) |
| 116 | + throws Exception { |
| 117 | + if (channelFuture.isSuccess()) { |
| 118 | + System.out.println("Server bound"); |
| 119 | + } else { |
| 120 | + System.err.println("Bound attempt failed"); |
| 121 | + channelFuture.cause().printStackTrace(); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +); |
| 126 | +```` |
0 commit comments