What is Netty? In-depth analysis of Netty related knowledge
This article brings you what is Netty? The in-depth analysis of Netty related knowledge has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
What is Netty
Start with HTTP
With Netty, you can implement your own HTTP server, FTP server, UDP server, RPC server, WebSocket server, Redis Proxy server, MySQL Proxy server, etc.
Let’s review the principles of traditional HTTP servers
1. Create a ServerSocket, listen and bind a port
2. A series of clients request this port
3. The server uses Accept to obtain a Socket connection object from the client
4. Start a new thread to process the connection
4.1. Read the Socket and obtain the byte stream
4.2. Decode the protocol and get the Http request object
4.3. Process the Http request and get a result and encapsulate it into an HttpResponse object
4.4. Encode the protocol and serialize the result into words Throttle write Socket and send the byte stream to the client
5. Continue to cycle step 3
The reason why the HTTP server is called an HTTP server is because the encoding and decoding protocol is the HTTP protocol. If If the protocol is the Redis protocol, then it becomes a Redis server, if the protocol is WebSocket, then it becomes a WebSocket server, and so on. Using Netty, you can customize the encoding and decoding protocol and implement your own protocol-specific server.
NIO
The above is a traditional HTTP server, but in a high-concurrency environment, the number of threads will be larger and the system load will be higher, so NIO is created.
He is not a concept unique to Java. A term represented by NIO is called IO multiplexing. It is a system call provided by the operating system. In the early days, the name of this operating system call was select, but its performance was low. It later gradually evolved into epoll under Linux and kqueue in Mac. We generally call it epoll, because no one uses Apple computers as servers to provide external services. Netty is a framework based on Java NIO technology. Why encapsulation is necessary? Because native Java NIO is not that convenient to use and has notorious bugs. After Netty encapsulates it, it provides an easy-to-operate usage mode and interface, which makes it much more convenient for users to use.
Before talking about NIO, let’s talk about BIO (Blocking IO). How to understand this Blocking?
When the client is listening (Listen), Accept is blocked. Only when a new connection comes, Accept will return and the main thread can continue
When reading and writing the socket, Read is blocked. Only when the request message comes, can Read return and the sub-thread can continue to process
When reading and writing the socket, Write is blocked. Only when the client receives the message can Write return and the sub-thread can continue reading. A request
In the traditional BIO mode, all threads are blocked from beginning to end. These threads just wait, occupying system resources, and do nothing.
So how does NIO achieve non-blocking? It uses an event mechanism. It can use one thread to do all the logic of Accept, read and write operations, and request processing. If there is nothing to do, it will not loop endlessly. It will sleep the thread until the next event comes and then continue working. Such a thread is called a NIO thread. Expressed in pseudo code:
while true { events = takeEvents(fds) // 获取事件,如果没有事件,线程就休眠 for event in events { if event.isAcceptable { doAccept() // 新链接来了 } elif event.isReadable { request = doRead() // 读消息 if request.isComplete() { doProcess() } } elif event.isWriteable { doWrite() // 写消息 } } }
Reactor thread model
Reactor single-thread model
One NIO thread and one accept thread:
Reactor multi-threading model
##Reactor master-slave model
Master-slave Reactor multi-threading: multiple acceptor NIO thread pools are used to accept client connectionsAccept连接和读写操作也可以使用同一个线程池来进行处理。而请求处理逻辑既可以使用单独的线程池进行处理,也可以跟放在读写线程一块处理。线程池中的每一个线程都是NIO线程。用户可以根据实际情况进行组装,构造出满足系统需求的高性能并发模型。
为什么选择Netty
如果不用netty,使用原生JDK的话,有如下问题:
1、API复杂
2、对多线程很熟悉:因为NIO涉及到Reactor模式
3、高可用的话:需要出路断连重连、半包读写、失败缓存等问题
4、JDK NIO的bug
而Netty来说,他的api简单、性能高而且社区活跃(dubbo、rocketmq等都使用了它)
什么是TCP 粘包/拆包
现象
先看如下代码,这个代码是使用netty在client端重复写100次数据给server端,ByteBuf是netty的一个字节容器,里面存放是的需要发送的数据
public class FirstClientHandler extends ChannelInboundHandlerAdapter { @Override public void channelActive(ChannelHandlerContext ctx) { for (int i = 0; i < 1000; i++) { ByteBuf buffer = getByteBuf(ctx); ctx.channel().writeAndFlush(buffer); } } private ByteBuf getByteBuf(ChannelHandlerContext ctx) { byte[] bytes = "需要更多资料加群:586446657".getBytes(Charset.forName("utf-8")); ByteBuf buffer = ctx.alloc().buffer(); buffer.writeBytes(bytes); return buffer; } }
从client端读取到的数据为:
从服务端的控制台输出可以看出,存在三种类型的输出
一种是正常的字符串输出。
一种是多个字符串“粘”在了一起,我们定义这种 ByteBuf 为粘包。
一种是一个字符串被“拆”开,形成一个破碎的包,我们定义这种 ByteBuf 为半包。
透过现象分析原因
应用层面使用了Netty,但是对于操作系统来说,只认TCP协议,尽管我们的应用层是按照 ByteBuf 为 单位来发送数据,server按照Bytebuf读取,但是到了底层操作系统仍然是按照字节流发送数据,因此,数据到了服务端,也是按照字节流的方式读入,然后到了 Netty 应用层面,重新拼装成 ByteBuf,而这里的 ByteBuf 与客户端按顺序发送的 ByteBuf 可能是不对等的。因此,我们需要在客户端根据自定义协议来组装我们应用层的数据包,然后在服务端根据我们的应用层的协议来组装数据包,这个过程通常在服务端称为拆包,而在客户端称为粘包。
拆包和粘包是相对的,一端粘了包,另外一端就需要将粘过的包拆开,发送端将三个数据包粘成两个 TCP 数据包发送到接收端,接收端就需要根据应用协议将两个数据包重新组装成三个数据包。
如何解决
在没有 Netty 的情况下,用户如果自己需要拆包,基本原理就是不断从 TCP 缓冲区中读取数据,每次读取完都需要判断是否是一个完整的数据包 如果当前读取的数据不足以拼接成一个完整的业务数据包,那就保留该数据,继续从 TCP 缓冲区中读取,直到得到一个完整的数据包。 如果当前读到的数据加上已经读取的数据足够拼接成一个数据包,那就将已经读取的数据拼接上本次读取的数据,构成一个完整的业务数据包传递到业务逻辑,多余的数据仍然保留,以便和下次读到的数据尝试拼接。
而在Netty中,已经造好了许多类型的拆包器,我们直接用就好:
选好拆包器后,在代码中client段和server端将拆包器加入到chanelPipeline之中就好了:
如上实例中:
客户端:
ch.pipeline().addLast(new FixedLengthFrameDecoder(31));
服务端:
ch.pipeline().addLast(new FixedLengthFrameDecoder(31));
Netty 的零拷贝
传统意义的拷贝
是在发送数据的时候,传统的实现方式是:
1. `File.read(bytes)`
2. `Socket.send(bytes)`
这种方式需要四次数据拷贝和四次上下文切换:
1. 数据从磁盘读取到内核的read buffer
2. 数据从内核缓冲区拷贝到用户缓冲区
3. 数据从用户缓冲区拷贝到内核的socket buffer
4. 数据从内核的socket buffer拷贝到网卡接口(硬件)的缓冲区
零拷贝的概念
明显上面的第二步和第三步是没有必要的,通过java的FileChannel.transferTo方法,可以避免上面两次多余的拷贝(当然这需要底层操作系统支持)
1. Call transferTo, the data is copied from the file to the kernel read buffer by the DMA engine
2. Then DMA copies the data from the kernel read buffer to the network card interface buffer
Two times above The operation does not require CPU participation, so zero copy is achieved.
Zero copy in Netty
is mainly reflected in three aspects:
1. bytebuffer
Netty mainly uses bytebuffer to send and receive messages, and bytebuffer is used External memory (DirectMemory) directly reads and writes Socket.
Reason: If traditional heap memory is used for Socket reading and writing, the JVM will copy the heap memory buffer to direct memory and then write it to the socket, creating an additional memory copy of the buffer. DirectMemory can be sent directly to the network card interface through DMA
2. Composite Buffers
Traditional ByteBuffer, if you need to combine the data in two ByteBuffers together, we need to first create a size= Create a new array of size1 size2, and then copy the data in the two arrays to the new array. However, using the combined ByteBuf provided by Netty, you can avoid such operations, because CompositeByteBuf does not really combine multiple Buffers, but saves their references, thus avoiding data copying and achieving zero copy.
3. The use of FileChannel.transferTo
Netty uses the transferTo method of FileChannel, which relies on the operating system to implement zero copy.
Netty internal execution process
Server:
3. Set up and bind the channel on the server4.5. Create a ChannelPipeline and handler to handle network events. Network time flows through it in the form of a stream. The handler completes most of the function customization: such as editing Decode SSl security authentication6. Bind and start the listening port7. When the ready channel is polled, the Reactor thread: NioEventLoop executes the method in the pipline, and finally schedules and executes it. channelHandler 8. Speaking of which, I would like to recommend a Java communication and learning community: 586446657, where you can not only communicate and discuss, but also share interview experience and download free materials, including Spring, MyBatis, and Netty source code analysis. , the principles of high concurrency, high performance, distributed, microservice architecture, and JVM performance optimization have become a necessary knowledge system for architects. I believe that for coders who are already working and encounter technical bottlenecks, there will be the content you need here. Client
##Summary
The above is I have compiled some knowledge related to Netty. If you have different opinions, please feel free to discuss!
The above is the detailed content of What is Netty? In-depth analysis of Netty related knowledge. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



TCP is a type of computer network communication protocol and a connection-oriented transmission protocol. In Java application development, TCP communication is widely used in various scenarios, such as data transmission between client and server, real-time transmission of audio and video, etc. Netty4 is a high-performance, highly scalable, and high-performance network programming framework that can optimize the data exchange process between the server and the client to make it more efficient and reliable. The specific implementation steps of using Netty4 for TCP communication are as follows: Introduction

Java development: How to use Netty for high-performance network programming Summary: Netty is a high-performance, asynchronous event-driven network programming framework that simplifies the development process of network applications. This article will introduce the main features of Netty and how to use Netty for high-performance network programming. At the same time, we will also provide some specific Java code examples to help readers better understand and apply Netty. 1. Introduction to Netty Netty is a network programming box based on JavaNIO

With the continuous development of Internet technology, network programming is becoming more and more important. In this field, Netty is a very well-known framework. It is a high-performance, asynchronous event-driven network application framework that is widely used to develop various high-concurrency network applications. Netty is a Java framework, and its emergence has promoted the development of Java network programming. However, with the widespread use of PHP, PHP developers are also looking for frameworks capable of high-concurrency network programming. Therefore, this article describes how to exploit P

As a high-performance io framework, netty is a very easy-to-use technical framework. Netty is a client and server-side programming framework based on NIO. Using Netty can ensure that you can quickly and easily develop a network application, such as implementing some kind of Client and server applications of the protocol. Netty is equivalent to simplifying and streamlining the programming and development process of network applications, such as: socket service development based on TCP and UDP. "Fast" and "simple" need not create maintenance or performance problems. Netty is a project that absorbs the implementation experience of multiple protocols (including various binary text protocols such as FTP, SMTP, HTTP, etc.) and is quite carefully designed. In the end, Netty succeeded

With the continuous development of the Internet and the expansion of application fields, high concurrency has become an issue that must be considered in network application development. As a language widely used in enterprise-level application development, Java’s performance in high-concurrency application scenarios Much attention. Netty is a high-performance, asynchronous event-driven network application framework that has been widely used in the field of Java back-end development in recent years. This article will introduce the basic concepts and usage of Netty, and take building a high-concurrency API server as an example to demonstrate Netty

How to use Java to develop a high-performance network application based on Netty. Netty is a network programming framework based on JavaNIO technology and is widely used in high-performance network application development. In this article, we will explore how to use Java and Netty to develop a high-performance network application based on Netty. We will introduce the basic concepts and features of Netty and provide some code examples to help you better understand and use Netty. 1. Basic concepts and features of Netty Ne

In JavaAPI development, TCP communication is a very important component, and Netty5 is a set of high-performance network communication framework based on NIO, which can handle complex network communication tasks very conveniently. This article will introduce how to use Netty5 for TCP communication, including the core components of Netty5, an introduction to common APIs, and practical application cases. At the same time, this article will also introduce how to use Netty5 to improve the performance and reliability of TCP communication. 1. Netty5 core component of Netty5

What is Netty asynchronous? It is an event-driven network application framework that is used to quickly develop high-performance, highly reliable network IO programs. It is mainly aimed at high-concurrency applications for Clients under the TCP protocol. It is essentially a NIO framework, suitable for server communication, etc. Scenario asynchronous: sending a request without waiting for a response, the program continues on. Event-driven: a connection event or disconnection event, or a read event or a write event, and subsequent processing after the event occurs. Typical applications of Netty: high-performance rpc framework is used for remote service (process) calls, such as Dubbo. Game industry, page data interaction. Big data areas such as Hadoop high-performance communication and serialization components (AVRO). A simple understanding of the IO model is to use what pass
