Java NIO Framework

NIO

The full name is Non-Blocking IO or New IO, that is, non-blocking IO or new version of IO.

The characteristics of NIO are as follows:

1. Buffer-oriented (Buffer): Each Buffer is essentially a container object.
Each basic Java type corresponds to a buffer type:
ByteBuffer——byte
CharBuffer——char
ShortBuffer——short
IntBuffer——int
LongBuffer— —long
FloatBuffer——float
DoubleBuffer——double
2. Selectors (Selectors): Selectors are used to monitor events of multiple pipelines. When using traditional blocking, you can easily know when you can read and write, while using non-blocking channels, The selector lets us know when the channel is ready
3. Non-blocking mode
4. Pipeline (Channel): Pipelines are actually like streams in traditional IO, all data to any destination (or from anywhere) must Pass a Channel object.

The difference between NIO and BIO

1. BIO is stream-oriented, NIO is block-oriented (buffer)

IO stream-oriented operations process data one byte at a time. An input stream produces one byte of data, and an output stream consumes one byte of data. , leading to inefficient reading and writing of data;
NIO block-oriented operations produce or consume a data block in one step. Processing data in chunks is much faster than processing data in (streaming) bytes, while data is read into a buffer where it is processed later, moving back and forth in the buffer as needed. This increases flexibility in processing. In layman's terms, NIO adopts the "pre-reading" method. When you read a certain part of the data, it will guess the data that you may read next and pre-buffer it.

2. BIO is blocking, NIO is non-blocking.

When a thread calls read() or write() in traditional IO, the thread is blocked until some data is read, or the data is completely written. The thread can no longer do anything during this period.
For NIO, a thread is used to send a read data request. Before a response is received, the thread is idle. At this time, the thread can perform other tasks instead of waiting for the response to complete as in IO.

Expansion: JDK1.7 introduces AIO
Simply put,
BIO: synchronous blocking IO
NIO: synchronous non-blocking IO
AIO: asynchronous blocking IO

Applicable scenarios
BIO: Applicable to architectures with a relatively small and fixed number of connections. This method requires relatively high server resources, and concurrency is limited to applications. For example: uploading and downloading files
NIO: suitable for architectures with a large number of connections and relatively short connections (light operation), such as chat servers, where concurrency is limited to applications, and programming is relatively complicated AIO: used for large number of connections and relatively long connections (heavy operations
) Operation) architecture, such as the photo album server, fully invokes the OS to participate in concurrent operations, and the programming is more complicated.

Currently common NIO frameworks

1. Netty (mainstream version is 4.1)

Netty is a NIO client-server framework, which provides asynchronous, event-driven network application framework and tools for rapid development of high-performance, high-reliability network server and client programs. Network applications such as protocol servers and clients can be developed quickly and easily.
Github address: https://github.com/netty/netty
Official website: https://netty.io/
Development documentation: https://netty.io/wiki/user-guide-for-4.x.html
API documentation : https://netty.io/4.1/api/index.html

Netty greatly simplifies and simplifies network programming such as TCP and UDP socket servers. Has rich protocols such as FTP, SMTP, HTTP and various binary and text-based legacy protocols. Thus, Netty implements methods for ease of development, performance, stability and flexibility.

Netty service framework:
Netty is a typical Reactor model structure. There are three commonly used Reactor thread models,
namely:
Reactor single-thread model
Reactor multi-thread model
master-slave Reactor multi-thread model

Netty architecture diagram
image.png

Chinese Version:
image.png

Scenario
1. Distributed service framework Dubbo's RPC framework uses the Dubbo protocol for inter-node communication. The Dubbo protocol uses Netty as the basic communication component by default to implement internal communication between process nodes.
2. Netty is also used for high-performance and asynchronous communication between message producers and message consumers of the message middleware RocketMQ.
3. Hadoop's high-performance communication and serialization component Avro's RPC framework uses Netty for cross-node communication by default, and its Netty Service is implemented based on the secondary encapsulation of the Netty framework.
4. In the game industry, Netty is also used to realize communication between various servers

Netty high-performance principles and framework architecture analysis can refer to
https://www.cnblogs.com/imstudy/p/9908791.html

2.Mina

Apache Mina Server is a network communication application framework that provides an abstract, event-driven, asynchronous API based on TCP/IP and UDP/IP protocols through Java nio technology. That is to say, it is mainly for communication frameworks based on TCP/IP and UDP/IP protocol stacks (of course, it can also provide serialization services for JAVA objects, virtual machine pipeline communication services, etc.), helping us to quickly develop high-performance, high-performance Extensible network communication application, and provides an event-driven, asynchronous operation programming model.
Github address: https://github.com/apache/mina
official website: https://mina.apache.org/
development documentation: https://mina.apache.org/mina-project/documentation.html
API documentation: https ://mina.apache.org/mina-project/apidocs/index.html

If you need to know more about Mina, you can refer to:
https://www.cnblogs.com/duanxz/p/5143227.html

3.Grizzly

Grizzly is an application framework designed to solve the various problems that arise when writing thousands of users accessing a server. Use JAVA NIO as the basis and hide the complexity of its programming. Easy-to-use high-performance API. Brings non-blocking socketd to the protocol processing layer. Take advantage of high-performance buffering and buffer management using a high-performance thread pool.
Github address: https://github.com/javaee/grizzly
Official website: https://javaee.github.io/grizzly/
Development documentation: https://javaee.github.io/grizzly/documentation.html

Design concept:
The original intention of the Grizzly NIO framework is to help developers make better use of the Java NIO API, build powerful and scalable server applications, and provide components that extend the framework: Web framework (HTTP/S), WebSocket, Comet, etc. .

Grizzly architecture diagram
image.png

Grizzly architecture understanding can refer to:
https://blog.csdn.net/a_dreaming_fish/article/details/50468607

おすすめ

転載: blog.csdn.net/qq_37131111/article/details/129831091