netty5 入门翻译

翻译网页网址:http://netty.io/wiki/user-guide-for-5.x.html

写道
NioEventLoopGroup is a multithreaded event loop that handles I/O operation. Netty provides various EventLoopGroup implementations for different kind of transports. We are implementing a server-side application in this example, and therefore two NioEventLoopGroup will be used. The first one, often called 'boss', accepts an incoming connection. The second one, often called 'worker', handles the traffic of the accepted connection once the boss accepts the connection and registers the accepted connection to the worker. How many Threads are used and how they are mapped to the created Channels depends on the EventLoopGroup implementation and may be even configurable via a constructor.

 翻译:

写道
NioEventLoopGroup是一个多线程事件环路。它用来处理IO操作。Netty为不同类型的传输提供了不同类型的EventLoopGroup的实现类。在这个例子中我们正在做一个简单的服务器端实现,将使用两个NioEventLoopGroup实现类。第一个通常称为boss,它的作用是接收传入的链接。第二个被称为‘worker’,它的作用是一旦boss接受链接并且注册链接到worker则处理这个接收链接的传输。依赖于EventLoopGroup的实现决定多少线程将被使用并且他们如何匹配到新建的channel,当然并且也可以通过构造函数来配置。

 NioEventLoopGroup的实现类可以查看API,http://netty.io/5.0/api/index.html.路径为io.netty.channel.NioEventLoopGroup

写道
ServerBootstrap is a helper class that sets up a server. You can set up the server using a Channel directly. However, please note that this is a tedious process, and you do not need to do that in most cases.

   翻译:

写道
ServerBootstrap 是一个工具类。用来启动server。我们也可以使用io.netty.channel.Channel直接启动server,这是一个乏味的过程,大多数情况下我们不必这么做。
package io.netty.example.discard;

import io.netty.bootstrap.ServerBootstrap;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * Discards any incoming data.
 */
public class DiscardServer {

    private int port;

    public DiscardServer(int port) {
        this.port = port;
    }

    public void run() throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap(); // (2)
            b.group(bossGroup, workerGroup)
             .channel(NioServerSocketChannel.class) // (3)
             .childHandler(new ChannelInitializer<SocketChannel>() { // (4)
                 @Override
                 public void initChannel(SocketChannel ch) throws Exception {
                     ch.pipeline().addLast(new DiscardServerHandler());
                 }
             })
             .option(ChannelOption.SO_BACKLOG, 128)          // (5)
             .childOption(ChannelOption.SO_KEEPALIVE, true); // (6)

            // Bind and start to accept incoming connections.
            ChannelFuture f = b.bind(port).sync(); // (7)

            // Wait until the server socket is closed.
            // In this example, this does not happen, but you can do that to gracefully
            // shut down your server.
            f.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        int port;
        if (args.length > 0) {
            port = Integer.parseInt(args[0]);
        } else {
            port = 8080;
        }
        new DiscardServer(port).run();
    }
}

  3) 此处我们指明使用NioServerSocketChannel来初始化一个新的Channel来接收传入的连接。

  4)此处指定的处理程序通常被一个新的接收渠道使用。ChannelInitializer类是一个指定的处理程序,他被用来帮助用户配置一个新的渠道。大多数状态下当我们通过添加一些处理器(例如:DiscardServerHandler)来实现我们的网络应用想要配置一个新的渠道的ChannelPipeline时需要这样做。当这个应用程序变的复杂时,例如我们最终将增加更多的处理器到这个管道里并且提取匿名类到定层类时很有用处。

  5)我们可以设置这个参数,参数是用来指定Channel实现的。这里我们是写一个TCP/IP服务器,所以我们允许设置socket选项,例如:tcpNoDelay或者keepAlive。请查看ChannelOption的apidocs文档和指定ChannelConfig实现来浏览一下支持的ChannelOptions。具体网址为:http://netty.io/5.0/api/io/netty/channel/ChannelConfig.html和http://netty.io/5.0/api/io/netty/channel/ChannelOption.html

  6)option() 是针对接收传入链接的NioServerSocketChannel,childOption()是针对被父ServerChannel接收的Channels,此处是NioServerSocketChannel

猜你喜欢

转载自01jiangwei01.iteye.com/blog/2185457