Do a simple program wang, share dry goods, talk about life.

Wechat public account: Advanced backend architecture

Follow me to find more dry goods, microservices, Spring source, JVM, SpringCloud Alibaba, K8S, etc.

If you found this article helpful, please give me a like and thank you for your support!

What is Netty for?

As a Java student, if you haven’t studied Netty, then your use and understanding of the Java language is only superficial. You can click SSH, write a few MVC’s, access databases and caches, and that’s just what a beginner Java programmer does. Netty is definitely a must pass if you want to get to the next level of Java server knowledge.

With Netty, you can implement your own HTTP server, FTP server, UDP server, RPC server, WebSocket server, Redis Proxy server, MySQL Proxy server, etc.

If you want to know how Nginx is written, if you want to know how Tomcat and Jetty are implemented, if you want to implement a simple Redis server, then you should understand Netty, their high performance principles are similar.

The principle of the traditional HTTP server

Let’s review how a traditional HTTP server works

  1. Create a ServerSocket that listens for and binds a port

  2. A series of clients request this port

  3. The server uses Accept to get a Socket connection object from the client

  4. Start a new thread to process the connection

    1. Read the Socket to get the byte stream
    2. Decode the protocol and get the Http request object
    3. Process the Http request and get the result, encapsulated as an HttpResponse object
    4. Encoding protocol that serializes the result as a byte stream
    5. Write the Socket and send the byte stream to the client
  5. Continue with step 3

HTTP servers are called HTTP servers because the encoding and decoding protocol is HTTP, if the protocol is Redis, it becomes a Redis server, if the protocol is WebSocket, it becomes a WebSocket server, etc.

Multiplexer and NIO

With Netty you can customize the codec protocol and implement your own protocol specific server.

We are talking about a traditional multi-threaded server, and this is how Apache handles requests. In a high concurrency environment, the number of threads may be too many to create, and the operating system will be under heavy task scheduling pressure, and the system load will be high. So what to do?

NIO is not unique to Java. NIO stands for a term called IO multiplexing. It was a system call provided by the operating system. In the early days, the operating system call was called SELECT, but its performance was poor, and later evolved into epoll on Linux and Kqueue on Macs.

We usually just call it epoll, because no one uses an Apple computer as a server for external services. Netty is a framework packaged based on Java NIO technology.

Why encapsulate? Because native Java NIO is not so easy to use and is notoriously buggy, Netty encapsulates it and provides an easy-to-use usage pattern and interface that makes it much easier for users to use.

What is NIO?

NIO stands for NoneBlocking IO and BIO stands for NoneBlocking IO. So what does this block mean?

  1. Accept is blocked and will not return until a new connection is accepted, allowing the main thread to continue

  2. Read blocks, and the child thread can continue processing only after the request message is returned

  3. Write is blocked and cannot be returned until the client accepts the message and the child thread continues to read the next request

So a traditional multithreaded server is BlockingIO, where all threads are blocked from beginning to end. These threads just sit there, taking up the operating system’s scheduling resources and doing nothing. It’s a waste.

So how does NIO do that? It uses the event mechanism. It allows a single thread to dry up the logic for Accept, read and write operations, and request processing. If there is nothing to do, it does not loop, it will sleep until the next event comes, such a thread is called a NIO thread.

While true {events = takeEvents(FDS) // The thread will hibernate for event in events {if event.isacceptable {doAccept() // the new link comes} elif event.isreadable {request = doRead() // IsComplete () {doProcess()}} elif event.iswriteable {doWrite() // write message}}Copy the code

The NIO process is basically the process described in the pseudocode above, which is a little different from the actual code, but it’s enough for beginners to understand.

How awesome is Netty?

Netty is based on NIO, and Netty provides a higher level of abstraction on top of NIO.

In Netty, Accept connections are handled by a separate thread pool, and read and write operations are handled by a separate thread pool.

Accept connections and read and write operations can also be processed using the same thread pool. The request processing logic can be processed using a separate thread pool or in parallel with the read-write thread. Each thread in the thread pool is a NIO thread. Users can assemble according to the actual situation and construct a concurrency model that meets the system requirements.

Netty provides built-in common codecs, including line codecs [one-line request], prefix length codecs [the first N bytes define the byte length of the request], replayable codecs [records the state of half-packet messages], HTTP codecs, WebSocket message codecs, and more.

Netty provides a series of lifecycle callback interfaces that allow users to receive callback events when a complete request arrives, when a connection is closed, and when a connection is established for logical processing.

Netty’s ability to manage multiple ports at the same time and its ability to use the NIO client model are necessary for RPC services.

Netty can handle TCP sockets as well as UDP sockets.

In the process of message reading and writing, a large number of ByteBuffer needs to be used. Netty has optimized and abstracted ByteBuffer in terms of performance and convenience.

In short, Netty is a must-have for Java programmers to get ahead. If you know this and want to know why, take a look at Netty. If Java is boring to you, Netty is the key to reopening your interest in Java.

Behind will continue to make a summary of Netty related articles and comb planning, so that Netty is no longer difficult!

Summary of historical articles

conclusion

The above is the summary and sharing of the previous content, thank you for your attention, thumbs up and favorites!

Wechat public account: Advanced backend architecture

More articles are coming, like to remember to give me a 👍, thank you for your support!

Public article synchronous update! Focus on me, don’t get lost!