This is the first day of my participation in Gwen Challenge

What is a Netty?

Netty is a Java open source network application framework provided by JBOSS. It is asynchronous and event-driven and is used to develop high performance and high reliability network IO programs.

Consider the key words in the introduction:

  1. What is asynchrony?

  2. What is event-based?

  3. How to achieve high performance and reliability?

Why Netty?

  1. In a distributed system, each node needs to be called remotely, so high-performance RPC framework is essential. Netty, as an asynchronous high-performance communication framework, is used as a basic communication component.
  2. NIO class library and API complex use trouble, it is difficult to write high-quality NIO program, development workload and difficulty are very large, such as client facing disconnection and reconnection, network flash, etc., there are NIO EpollBug, resulting in Selector empty polling, resulting in CPU100%. Netty is quick and easy to develop a web application.

What are the usage scenarios?

  1. Elasticsearch,Dubbo framework implementation,
  2. Game industry, big data industry.

IO model

To understand NetTY, you need to understand the IO model of the network. What kind of channel is used to send and receive data, which largely determines the communication performance of the program. And then step by step, BIO,NIO. Know the following points in advance: 1 Synchronous and asynchronous 2 Blocking and non-blocking.

What is the BIO model?

  1. Synchronous blocking, server implementation mode for a connection to a thread, will cause unnecessary waste of threads;
  2. CMD Telnet 127.0.0.1 6666 to connect to the code server, each client connection will create a thread, the client connection after the block read; When the number of concurrent requests is large, a large number of threads need to be created to handle the problem, occupying system resources. Verify the following code
public static void main(String[] args) throws IOException { ExecutorService executorService = Executors.newCachedThreadPool(); ServerSocket serverSocket = new ServerSocket(6666); System.out.println(" server startup "); While (true){// Wait for client to connect Socket accept = serversocket.accept (); System.out.println(" client connection "); executorService.submit(new Runnable() { @Override public void run() { handler(accept); }}); }} public static void handler(Socket Socket){system.out.println (" +"+ thread.currentThread ().getName()); byte[] bytes = new byte[1024]; try { InputStream inputStream = socket.getInputStream(); While (true){system.out.println (" after the client connects, block here, waiting for the client to send "); int read = inputStream.read(bytes); if (read ! Println (new String(bytes,0,read).toString()); } } } catch (IOException e) { e.printStackTrace(); }finally { try { socket.close(); } catch (IOException e) { e.printStackTrace(); }}}Copy the code

3 drawing and understanding

graph TD
Server --> Thread1 --> Read1/Write1  --> client1
Server --> Thread2 --> Read2/Write2 --> client2
Server --> Thread3 --> Read3/Write3 --> client3

4 Application scenario: The architecture with small and fixed connection number has high requirements on server resources. The program is simple and easy to understand through thread pool optimization.

The next article is about NIO.