Mina、Netty、Twisted一起学(十):线程模型

要想开发一个高性能的TCP服务器,熟悉所使用框架的线程模型非常重要。MINA、Netty、Twisted本身都是高性能的网络框架,如果再搭配上高效率的代码,才能实现一个高大上的服务器。但是如果不了解它们的线程模型,就很难写出高性能的代码。框架本身效率再高,程序写的太差,那么服务器整体的性能也不会太高。就像一个电脑,CPU再好,内存小硬盘慢散热差,整体的性能也不会太高。

玩过Android开发的同学会知道,在Android应用中有一个非常重要线程:UI线程(即主线程)。UI线程是负责一个Android的界面显示以及和用户交互。Activity的一些方法,例如onCreate、onStop、onDestroy都是运行在UI线程中的。但是在编写Activity代码的时候有一点需要非常注意,就是绝对不能把阻塞的或者耗时的任务写在这些方法中,如果写在这些方法中,则会阻塞UI线程,导致用户操作的界面反应迟钝,体验很差。所以在Android开发中,耗时或者阻塞的任务会另外开线程去做。

同样在MINA、Netty、Twisted中,也有一个非常重要的线程:IO线程。

传统的BIO实现的TCP服务器,特别对于TCP长连接,通常都要为每个连接开启一个线程,线程也是操作系统的一种资源,所以很难实现高性能高并发。而异步IO实现的TCP服务器,由于IO操作都是异步的,可以用一个线程或者少量线程来处理大量连接的IO操作,所以只需要少量的IO线程就可以实现高并发的服务器。

在网络编程过程中,通常有一些业务逻辑是比较耗时、阻塞的,例如数据库操作,如果网络不好,加上数据库性能差,SQL不够优化,数据量大,一条SQL可能会执行很久。由于IO线程本身数量就不多,通常只有一个或几个,而如果这种耗时阻塞的代码在IO线程中运行的话,IO线程的其他事情,例如网络read和write,就无法进行了,会影响IO性能以及整个服务器的性能。

所以,无论是使用MINA、Netty、Twisted,如果有耗时的任务,就绝对不能在IO线程中运行,而是要另外开启线程来处理。

MINA:

在MINA中,有三种非常重要的线程:Acceptor thread、Connector thread、I/O processor thread。

下面是官方文档的介绍:

In MINA, there are three kinds of I/O worker threads in the NIO socket implementation.Acceptor thread accepts incoming connections, and forwards the connection to the I/O processor thread for read and write operations.Each SocketAcceptor creates one acceptor thread. You can’t configure the number of the acceptor threads.Connector thread attempts connections to a remote peer, and forwards the succeeded connection to the I/O processor thread for read and write operations.Each SocketConnector creates one connector thread. You can’t configure the number of the connector threads, either.I/O processor thread performs the actual read and write operation until the connection is closed.Each SocketAcceptor or SocketConnector creates its own I/O processor thread(s). You can configure the number of the I/O processor threads. The default maximum number of the I/O processor threads is the number of CPU cores + 1.

Acceptor thread:

这个线程用于TCP服务器接收新的连接,并将连接分配到I/O processor thread,由I/O processor thread来处理IO操作。每个NioSocketAcceptor创建一个Acceptor thread,线程数量不可配置。

Connector thread:

用于处理TCP客户端连接到服务器,并将连接分配到I/O processor thread,由I/O processor thread来处理IO操作。每个NioSocketConnector创建一个Connector thread,线程数量不可配置。

I/O processor thread:

用于处理TCP连接的I/O操作,如read、write。I/O processor thread的线程数量可通过NioSocketAcceptor或NioSocketConnector构造方法来配置,默认是CPU核心数+1。

由于本文主要介绍TCP服务器的线程模型,所以就没有Connector thread什么事了。下面说下Acceptor thread和I/O processor thread处理TCP连接的流程:

MINA的TCP服务器包含一个Acceptor thread和多个I/O processor thread,当有新的客户端连接到服务器,首先会由Acceptor thread获取到这个连接,同时将这个连接分配给多个I/O processor thread中的一个线程,当客户端发送数据给服务器,对应的I/O processor thread负责读取这个数据,并执行IoFilterChain中的IoFilter以及IoHandle。

由于I/O processor thread本身数量有限,通常就那么几个,但是又要处理成千上万个连接的IO操作,包括read、write、协议的编码解码、各种Filter以及IoHandle中的业务逻辑,特别是业务逻辑,比如IoHandle的messageReceived,如果有耗时、阻塞的任务,例如查询数据库,那么就会阻塞I/O processor thread,导致无法及时处理其他IO事件,服务器性能下降。

针对这个问题,MINA中提供了一个ExecutorFilter,用于将需要执行很长时间的会阻塞I/O processor thread的业务逻辑放到另外的线程中,这样就不会阻塞I/O processor thread,不会影响IO操作。ExecutorFilter中包含一个线程池,默认是OrderedThreadPoolExecutor,这个线程池保证同一个连接的多个事件按顺序依次执行,另外还可以使用UnorderedThreadPoolExecutor,它不会保证同一连接的事件的执行顺序,并且可能会并发执行。二者之间可以根据需要来选择。

public class TcpServer {public static void main(String[] args) throws IOException {IoAcceptor acceptor = new NioSocketAcceptor(4); // 配置I/O processor thread线程数量acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory()));acceptor.getFilterChain().addLast("executor", new ExecutorFilter()); // 将TcpServerHandle中的业务逻辑拿到ExecutorFilter的线程池中执行acceptor.setHandler(new TcpServerHandle());acceptor.bind(new InetSocketAddress(8080));}}class TcpServerHandle extends IoHandlerAdapter {@Overridepublic void messageReceived(IoSession session, Object message)throws Exception {// 假设这里有个变态的SQL要执行3秒Thread.sleep(3000);}}Netty:当花儿枯萎的时候,就是它生命终结的时候,

Mina、Netty、Twisted一起学(十):线程模型

相关文章:

你感兴趣的文章:

标签云: