Netty 前置 BIO NIO AIO

博文目录


IO模型

IO模型就是说用什么样的通道进行数据的发送和接收,Java共支持3种网络编程IO模式:BIO,NIO,AIO

BIO(Blocking IO)

同步阻塞模型,一个客户端连接对应一个处理线程

缺点

  1. IO代码里read操作是阻塞操作,如果连接不做数据读写操作会导致线程阻塞,浪费资源
  2. 如果线程很多,会导致服务器线程太多,压力太大,比如C10K问题

应用场景

BIO 方式适用于连接数目比较小且固定的架构, 这种方式对服务器资源要求比较高, 但程序简单易理解。

server

package com.mrathena.io;

import lombok.extern.slf4j.Slf4j;

import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

@Slf4j
public class BioTest {
	public static void main(String[] args) {
		bio2();
	}

	private static void bio() {
		try {
			ServerSocket serverSocket = new ServerSocket(8888);
			while (true) {
				Socket socket = serverSocket.accept();
				log.info("有客户端连接了");
				byte[] buffer = new byte[1024];
				int length = socket.getInputStream().read(buffer);
				log.info("读取完毕");
				if (length != -1) {
					log.info("数据: {}", new String(buffer, 0, length));
				}
				socket.getOutputStream().write("response".getBytes(StandardCharsets.UTF_8));
				socket.getOutputStream().flush();
			}
		} catch (Throwable cause) {
			log.error("", cause);
		}
	}
	private static void bio2() {
		try {
			ServerSocket serverSocket = new ServerSocket(8888);
			while (true) {
				Socket socket = serverSocket.accept();
				new Thread(new Runnable() {
					@Override
					public void run() {
						try {
							log.info("有客户端连接了");
							byte[] buffer = new byte[1024];
							while (true) {
								int length = socket.getInputStream().read(buffer);
								log.info("读取完毕");
								if (length != -1) {
									log.info("数据: {}", new String(buffer, 0, length));
								}
								socket.getOutputStream().write("response".getBytes(StandardCharsets.UTF_8));
								socket.getOutputStream().flush();
							}
						} catch (Throwable cause) {
							log.error("", cause);
						}
					}
				}).start();
			}
		} catch (Throwable cause) {
			log.error("", cause);
		}
	}
}

NIO(Non Blocking IO)

同步非阻塞,服务器实现模式为一个线程可以处理多个请求(连接),客户端发送的连接请求都会注册到多路复用器selector上,多路复用器轮询到连接有IO请求就进行处理,JDK1.4开始引入。

应用场景

NIO方式适用于连接数目多且连接比较短(轻操作) 的架构, 比如聊天服务器, 弹幕系统, 服务器间通讯,编程比较复杂

NIO 有三大核心组件: Channel(通道), Buffer(缓冲区),Selector(多路复用器)

多路复用指的是一个server线程处理所有的客户端连接和数据收发请求

  1. channel 类似于流,每个 channel 对应一个 buffer缓冲区,buffer 底层就是个数组
  2. channel 会注册到 selector 上,由 selector 根据 channel 读写事件的发生将其交由某个空闲的线程处理
  3. NIO 的 Buffer 和 channel 都是既可以读也可以写
    在这里插入图片描述
    NIO底层在JDK1.4版本是用linux的内核函数select()或poll()来实现,和下面NIOServer端代码的第一个demo类似,selector每次都会轮询所有的sockchannel看下哪个channel有读写事件,有的话就处理,没有就继续遍历,JDK1.5开始引入了epoll基于事件响应机制来优化NIO。

bioWithSelector代码里如下几个方法非常重要,我们从Hotspot与Linux内核函数级别来理解下

Selector.open(); // 创建多路复用器, 根据不同系统的JDK创建, Linux系统下使用的是 EPollSelectorImpl, 底层是Linux操作系统的epoll模型机制
socketChannel.register(selector, SelectionKey.OP_READ); // 将channel注册到多路复用器上, 将channel和监听事件关联起来
selector.select(); // 阻塞等待需要处理的事件发生, 调用操作系统函数绑定关联关系并阻塞等待事件发生

总结:NIO整个调用流程就是Java调用了操作系统的内核函数来创建Socket,获取到Socket的文件描述符,再创建一个Selector对象,对应操作系统的Epoll描述符,将获取到的Socket连接的文件描述符的事件绑定到Selector对应的Epoll文件描述符上,进行事件的异步通知,这样就实现了使用一条线程,并且不需要太多的无效的遍历,将事件处理交给了操作系统内核(操作系统中断程序实现),大大提高了效率。
在这里插入图片描述

EPoll

Linux操作系统的一个底层机制, 通过epoll_create函数创建一个epoll文件描述符, 然后调用epoll_ctl函数将一些Socket对象和事件绑定到该文件描述符上, 然后调用epoll_wait函数, 操作系统将通过中断机制监听所有Socket对象的对应事件的发生, 然后主动将事件和Socket对象推到rdlist(准备好的列表)中. 这时候wait将被唤醒, 直接从rdlist中取得事件和Socket对象做处理.

最大的好处就是一个epoll绑定的Socket可能有很多, 但是真的发生事件的可能没多少, 每当有事件发生时, 操作系统主动告诉我们处理发生事件的Socket对象即可, 无需遍历所有Socket对象, 通过事件驱动, 极大提高了系统性能

# 创建一个epoll实例,并返回一个非负数作为文件描述符,用于对epoll接口的所有后续调用。
# 参数size代表可能会容纳size个描述符,但size不是一个最大值,只是提示操作系统它的数量级,现在这个参数基本上已经弃用了。
int epoll_create(int size);

# 使用文件描述符epfd引用的epoll实例,对目标文件描述符fd执行op操作。
# 参数epfd表示epoll对应的文件描述符,参数fd表示socket对应的文件描述符。
# 参数op有以下几个值:
# EPOLL_CTL_ADD:注册新的fd到epfd中,并关联事件event;
# EPOLL_CTL_MOD:修改已经注册的fd的监听事件;
# EPOLL_CTL_DEL:从epfd中移除fd,并且忽略掉绑定的event,这时event可以为null;
# 参数event是一个结构体
# struct epoll_event {
# 	    __uint32_t   events;      /* Epoll events */
# 	    epoll_data_t data;        /* User data variable */
# 	};
# 	
# 	typedef union epoll_data {
# 	    void        *ptr;
# 	    int          fd;
# 	    __uint32_t   u32;
# 	    __uint64_t   u64;
# 	} epoll_data_t;
# events有很多可选值,这里只举例最常见的几个:
# EPOLLIN :表示对应的文件描述符是可读的;
# EPOLLOUT:表示对应的文件描述符是可写的;
# EPOLLERR:表示对应的文件描述符发生了错误;
# 成功则返回0,失败返回-1
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);

# 等待文件描述符epfd上的事件。
# epfd是Epoll对应的文件描述符,events表示调用者所有可用事件的集合,maxevents表示最多等到多少个事件就返回,timeout是超时时间。
# I/O多路复用底层主要用的Linux内核函数(select,poll,epoll)来实现,windows不支持epoll实现,windows底层是基于winsock2的select函数实现的(不开源)
int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout);
selectpollepoll(jdk 1.5及以上)
操作方式遍历遍历回调
底层实现数组链表哈希表
IO效率每次调用都进行线性遍历,时间复杂度为O(n)每次调用都进行线性遍历,时间复杂度为O(n)事件通知方式,每当有IO事件就绪,系统注册的回调函数就会被调用,时间复杂度O(1)
最大连接有上限无上限无上限

Redis线程模型

Redis就是典型的基于epoll的NIO线程模型(nginx也是),epoll实例收集所有事件(连接与读写事件),由一个服务端线程连续处理所有事件命令。

Redis底层关于epoll的源码实现在redis的src源码目录的ae_epoll.c文件里

server

package com.mrathena.io;

import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.*;

@Slf4j
public class NioTest {
	private static final List<SocketChannel> socketChannelList = new LinkedList<>();
	public static void main(String[] args) {
		nioWithSelector();
	}
	private static void nio() {
		try {
			// 创建NIO的ServerSocketChannel, 与BIO的ServerSocket类似
			ServerSocketChannel serverSocketChannel = ServerSocketChannel.open().bind(new InetSocketAddress(8888));
			// 设置ServerSocketChannel为非阻塞
			serverSocketChannel.configureBlocking(false);
			log.info("服务已启动");
			while (true) {
				// 非阻塞模式accept方法不会阻塞, 没有连接时将返回null
				// NIO的非阻塞是由操作系统内部实现的, linux时调用了系统的accept函数
				SocketChannel socketChannel = serverSocketChannel.accept();
				if (null != socketChannel) {
					log.info("有客户端连接了");
					// 设置SocketChannel为非阻塞
					socketChannel.configureBlocking(false);
					// 保存起来
					socketChannelList.add(socketChannel);
					log.info("当前连接数: {}", socketChannelList.size());
				}
				// 遍历socketChannelList
				// 如果连接数太多的话,会有大量的无效遍历,假如有10000个连接,其中只有10个连接有写数据,但是由于其他9990个连接并没有断开,我们还是要每次轮询遍历一万次,其中有千分之九九九的遍历都是无效的,这显然不是一个让人很满意的方案
				ListIterator<SocketChannel> iterator = socketChannelList.listIterator();
				while (iterator.hasNext()) {
					SocketChannel channel = iterator.next();
					ByteBuffer buffer = ByteBuffer.allocate(1024);
					int length = channel.read(buffer);
					if (length == -1) {
						log.info("客户端断开连接");
						iterator.remove();
						log.info("当前连接数: {}", socketChannelList.size());
					} else if (length > 0) {
						log.info("读取数据: {}", new String(buffer.array(), 0, length));
					}
				}
			}
		} catch (Throwable cause) {
			log.error("", cause);
		}
	}
	# 还可以优化
	# 1.read/write用线程池处理
	# 2.两个selector, 一个处理accept, 一个处理read/write
	# 3.多个selector, 一个处理accept, 多个处理read/write
	# 4.多个selector, 多个处理accept, 多个处理read/write
	# 了解Reactor模型
	private static void nioWithSelector() {
		try {
			// 创建NIO的ServerSocketChannel, 与BIO的ServerSocket类似
			ServerSocketChannel serverSocket = ServerSocketChannel.open().bind(new InetSocketAddress(8888));
			// 设置ServerSocketChannel为非阻塞
			serverSocket.configureBlocking(false);
			// Selector 多路复用器
			// 把ServerSocketChannel注册到Selector上, 并且Selector监听accept事件
			Selector selector = Selector.open();
			serverSocket.register(selector, SelectionKey.OP_ACCEPT);
			log.info("服务已启动");
			while (true) {
				// 阻塞等待需要处理的事件的发生
				selector.select();
				// 事件发生了, 获取这些事件的 SelectionKey 实例
				Set<SelectionKey> selectionKeys = selector.selectedKeys();
				Iterator<SelectionKey> iterator = selectionKeys.iterator();
				// 遍历处理事件
				while (iterator.hasNext()) {
					SelectionKey selectionKey = iterator.next();
					// 判断事件类型
					if (selectionKey.isAcceptable()) {
						// accept事件, 连接获取和事件注册
						// 获取发生事件的对应的Channel, 该事件注册的是ServerSocketChannel
						ServerSocketChannel serverSocketChannel = (ServerSocketChannel) selectionKey.channel();
						SocketChannel socketChannel = serverSocketChannel.accept();
						socketChannel.configureBlocking(false);
						// 注册该连接的读事件
						socketChannel.register(selector, SelectionKey.OP_READ);
						log.info("有客户端连接了");
					} else if (selectionKey.isReadable()) {
						// 读事件, 获取和打印
						// 获取发生事件的对应的Channel, 读事件注册的是SocketChannel
						SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
						ByteBuffer buffer = ByteBuffer.allocate(1024);
						int length = socketChannel.read(buffer);
						if (length > 0) {
							log.info("读取数据: {}", new String(buffer.array(), 0, length));
						} else if (length == -1) {
							log.info("客户端断开连接");
							socketChannel.close();
						}
					}
				}
				// 处理过的事件需要删除掉, 防止下次重复处理
				iterator.remove();
			}
		} catch (Throwable cause) {
			log.error("", cause);
		}
	}
}

AIO(NIO 2.0)

异步非阻塞, 由操作系统完成后回调通知服务端程序启动线程去处理, 一般适用于连接数较多且连接时间较长的应用

应用场景

AIO方式适用于连接数目多且连接比较长(重操作)的架构,JDK7 开始支持

server

package com.mrathena.io;

import com.mrathena.toolkit.ThreadKit;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;

@Slf4j
public class AioTest {
	public static void main(String[] args) {
		try {
			AsynchronousServerSocketChannel asynchronousServerSocketChannel = AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(8888));
			asynchronousServerSocketChannel.accept(null, new CompletionHandler<AsynchronousSocketChannel, Object>() {
				@Override
				public void completed(AsynchronousSocketChannel asynchronousSocketChannel, Object attachment) {
					try {
						log.info("2");
						// 接收客户端连接, 否则后面的客户端连接不上
						asynchronousServerSocketChannel.accept(attachment, this);
						ByteBuffer buffer = ByteBuffer.allocate(1024);
						asynchronousSocketChannel.read(buffer, buffer, new CompletionHandler<Integer, ByteBuffer>() {
							@Override
							public void completed(Integer result, ByteBuffer attachment) {
								log.info("3");
								// 读写模式切换, 非常容易出bug
								buffer.flip();
								log.info(new String(buffer.array(), 0 , result));
								asynchronousSocketChannel.write(ByteBuffer.wrap("response".getBytes()));
							}

							@Override
							public void failed(Throwable exc, ByteBuffer attachment) {
								log.error("", exc);
							}
						});
					} catch (Throwable cause) {
						log.error("", cause);
					}
				}

				@Override
				public void failed(Throwable exc, Object attachment) {
					log.error("", exc);
				}
			});
			log.info("1");
			ThreadKit.sleepDay(1);
		} catch (Throwable cause) {
			log.error("", cause);
		}
	}
}

client

public static void main(String... args) throws Exception {
    AsynchronousSocketChannel socketChannel = AsynchronousSocketChannel.open();
    socketChannel.connect(new InetSocketAddress("localhost", 8888)).get();
    socketChannel.write(ByteBuffer.wrap("HelloServer".getBytes()));
    ByteBuffer buffer = ByteBuffer.allocate(512);
    Integer len = socketChannel.read(buffer).get();
    if (len != -1) {
        System.out.println("客户端收到信息:" + new String(buffer.array(), 0, len));
    }
}

NIO实现的简易聊天室

server

package com.mrathena.io.chat;

import com.mrathena.toolkit.ThreadKit;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class NioChatServer {

	private static final Map<String, SocketChannel> socketChannelMap = new HashMap<>();

	public static void main(String[] args) {
		try {
			ExecutorService pool = Executors.newFixedThreadPool(1);

			Selector acceptSelector = Selector.open();
			Selector readSelector = Selector.open();

			ServerSocketChannel serverSocketChannel = ServerSocketChannel.open().bind(new InetSocketAddress(8888));
			serverSocketChannel.configureBlocking(false);

			serverSocketChannel.register(acceptSelector, SelectionKey.OP_ACCEPT);

			log.info("server started");

			pool.submit(() -> {
				try {
					while (true) {
						if (readSelector.select() == 0) {
							// 防止线程跑太快, 刚唤醒就又select了, 导致register来不及执行
							ThreadKit.sleepSecond(1);
							continue;
						}
						Set<SelectionKey> readWriteSelectedKeys = readSelector.selectedKeys();
						Iterator<SelectionKey> readWriteSelectedKeyIterator = readWriteSelectedKeys.iterator();
						while (readWriteSelectedKeyIterator.hasNext()) {
							SelectionKey key = readWriteSelectedKeyIterator.next();
							if (key.isReadable()) {
								// 获取当前的连接
								SocketChannel readSocketChannel = (SocketChannel) key.channel();
								String address = readSocketChannel.getRemoteAddress().toString();
								ByteBuffer buffer = ByteBuffer.allocate(1024);
								int length = readSocketChannel.read(buffer);
								if (length == -1) {
									log.info("{} 离开了", readSocketChannel.getRemoteAddress());
									// 从组中移除当前连接
									socketChannelMap.remove(address);
									// 通知其他连接
									String message = "[系统通知] " + address + " 下线了";
									notifyMessageToOther(message);
									// 关闭连接
									readSocketChannel.close();
								} else if (length > 0) {
									String content = new String(buffer.array(), 0, length);
									log.info("{} {}", address, content);
									// 通知其他连接
									String message = address + " " + content;
									notifyMessageToAllWithoutSelf(message, readSocketChannel);
								}
								readWriteSelectedKeyIterator.remove();
							}
						}
					}
				} catch (Throwable cause) {
					log.error("", cause);
				}
			});

			while (true) {
				if (acceptSelector.select() == 0) {
					continue;
				}
				Set<SelectionKey> selectionKeys = acceptSelector.selectedKeys();
				Iterator<SelectionKey> iterator = selectionKeys.iterator();
				while (iterator.hasNext()) {
					SelectionKey key = iterator.next();
					if (key.isAcceptable()) {
						// 获取当前的连接
						SocketChannel socketChannel = ((ServerSocketChannel) key.channel()).accept();
						socketChannel.configureBlocking(false);
						String address = socketChannel.getRemoteAddress().toString();
						log.info("{} 上线了", address);
						// 欢迎当前连接
						notifyMessageToSelf("[系统通知] Hello " + address + ", Welcome to Join", socketChannel);
						// 通知其他连接
						notifyMessageToOther("[系统通知] " + address + " 上线了");
						// 当前连接加入到组中
						socketChannelMap.put(address, socketChannel);
						log.info("{}", socketChannelMap.keySet());
						// 唤醒读多路复用器, readSelector 的 select 和 register 方法貌似有同一把锁, select的时候, register会被阻塞
						readSelector.wakeup();
						socketChannel.register(readSelector, SelectionKey.OP_READ);
					}
					iterator.remove();
				}
			}
		} catch (Throwable cause) {
			log.error("", cause);
		}
	}

	private static void notifyMessageToSelf(String message, SocketChannel socketChannel) throws Throwable {
		socketChannel.write(ByteBuffer.wrap(message.getBytes()));
	}

	private static void notifyMessageToOther(String message) throws Throwable {
		Collection<SocketChannel> socketChannelCollection = socketChannelMap.values();
		for (SocketChannel socketChannel : socketChannelCollection) {
			socketChannel.write(ByteBuffer.wrap(message.getBytes()));
		}
	}

	private static void notifyMessageToAllWithoutSelf(String message, SocketChannel excludeSocketChannel) throws Throwable {
		Collection<SocketChannel> socketChannelCollection = socketChannelMap.values();
		for (SocketChannel socketChannel : socketChannelCollection) {
			if (!socketChannel.getRemoteAddress().equals(excludeSocketChannel.getRemoteAddress())) {
				socketChannel.write(ByteBuffer.wrap(message.getBytes()));
			}
		}
	}

}

client

package com.mrathena.io.chat;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class NioChatClient {
	public static void main(String[] args) {
		try {
			ExecutorService pool = Executors.newFixedThreadPool(1);

			SocketChannel socketChannel = SocketChannel.open();
			socketChannel.connect(new InetSocketAddress("localhost", 8888));

			pool.submit(new Runnable() {
				@Override
				public void run() {
					try {
						while (true) {
							ByteBuffer buffer = ByteBuffer.allocate(1024);
							int length = socketChannel.read(buffer);
							if (length > 0) {
								log.info(new String(buffer.array(), 0, length));
							}
						}
					} catch (Throwable cause) {
						log.error("", cause);
					}
				}
			});

			Scanner scanner = new Scanner(System.in);
			while (true) {
				String message = scanner.nextLine();
				if (StringUtils.isNotBlank(message)) {
					socketChannel.write(ByteBuffer.wrap(message.getBytes()));
				}
			}
		} catch (Throwable cause) {
			log.error("", cause);
		}
	}
}

BIO、 NIO、 AIO 对比

BIONIOAIO
IO模型同步阻塞(一个线程)同步非阻塞(多路复用)(一个线程)异步非阻塞(多个线程)
编程难度简单复杂复杂
可靠性
吞吐量

为什么Netty使用NIO而不是AIO?

在Linux系统上,AIO的底层实现仍使用Epoll,没有很好实现AIO,因此在性能上没有明显的优势,而且被JDK封装了一层不容易深度优化,Linux上AIO还不够成熟。Netty是异步非阻塞框架,Netty在NIO上做了很多异步的封装。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值