Netty use open-source framework
Dubbo: Ali open source, high-performance communications framework rpc
RocketMQ: Ali produced high-performance message queue
Spark: processing a computational engine designed for large-scale data
Elasticsearch,Cassandra,Flink,Netty-SocketIO,Spring5,Play,Grpc...
What is Netty
- Asynchronous event-driven framework for users to quickly develop high-performance server and client
- JDK and encapsulates the underlying BIO NIO model, providing high availability API
- Comes codec unpacking stick package to solve the problem, users only care about business logic
- reactor threading model designed to support high concurrent connection Mass
- It comes with a variety of protocol stack can handle any kind of common agreement do not have hands-on
Netty basic components
In the absence of netty case, the client and server communication
public class ServerBoot { private static final int PORT = 8000; public static void main(String[] args) {
// 启动一个server Server server = new Server(PORT); server.start(); } }
Server-side code as follows:
Server class {public Private ServerSocket serverSocket; public Server (int Port) { the try {
// Bind port according to the port number this.serverSocket = new new ServerSocket (Port); System.out.println ( "server started successfully, port:" port +); } the catch (IOException Exception) { System.out.println ( "server failed to start"); } } // create the application's main thread to avoid blocking ServerBoot the server, so the port monitor into a separate thread in public void Start () { new new the Thread (the Runnable new new () { @Override public void RUN () { the doStart (); } }). Start (); } // receiving client is connected
void the doStart Private () { the while (to true) { the try {
// the client is connected to the receiver, Accept a blocking method, when a client before creating over socket the Socket = serverSocket.accept Client ();
// read avoid socket blocking serverSocket.accept, so the need to create a thread ClientHandler new new ClientHandler (Client) .start (); } the catch (IOException E) { System.out.println ( "exception server"); } } } }
ClientHandler code is as follows:
ClientHandler {class public public static int MAX_DATA_LEN Final = 1024;
// save client Socket Private Final the Socket Socket; public ClientHandler (the Socket Socket) { this.socket = Socket; } public void Start () { System.out.println ( " new client access "); new new the Thread (the Runnable new new () { @Override public void RUN () { the doStart (); } .}) Start (); } Private void the doStart () { the try { the inputStream inputStream = Socket. the getInputStream ();
// client and server communicate while (true) { byte[] data = new byte[MAX_DATA_LEN]; int len; while ((len = inputStream.read(data)) != -1) { String message = new String(data, 0, len); System.out.println("客户端传来消息: " + message); socket.getOutputStream().write(data); } } } catch (IOException e) { e.printStackTrace(); } } }
Client Client code is as follows:
public class Client { private static final String HOST = "127.0.0.1"; private static final int PORT = 9010; private static final int SLEEP_TIME = 5000; public static void main(String[] args) throws IOException { final Socket socket = new Socket(HOST, PORT); new Thread(new Runnable() { @Override public void run() { System.out.println("客户端启动成功!"); while (true) { try { String message = "hello world"; System.out.println("客户端发送数据: " + message); socket.getOutputStream().write(message.getBytes()); } catch (Exception e) { System.out.println("写数据出错!"); } sleep(); } } }).start(); } private static void sleep() { try { Thread.sleep(SLEEP_TIME); } catch (InterruptedException e) { e.printStackTrace(); } } }
The client-side logic to communicate with the service:
Server perspective:
- First-service monitoring port
- By while (true) continue to accept the link, if the client creates a new connection to get connected to the server, and then by the processor to handle client connections
- The server receives data
- Server-side business logic processing to do
- The server sends data to the client
Relationship with the above process corresponds netty following components together: