Preface 1. Establish a connection

  • HTTP
  • Socket

2. Request processing

  • BIO
  • NIO
  • AIO

3. Transport protocol

  • The HTTP protocol
  • DUBBO agreement
  • WebService agreement

4. Serialization 5. Summary

preface

We know what RPC is and what it can do, but we have little knowledge about how it does it. Today, we will rearrange the little knowledge!

Remote Procedure Call (RPC) is a method used in different processes on different physical machines to Call Remote service methods just like calling local methods and realize the interaction between services.

So how does this work?

Take the telephone communication between people as an example. There is A scenario in which characters A and B ask B A question that B must know. The scenario is as follows:

A first calls B. After the phone is connected, A raises A question. B thinks about the question and tells A through the phone after receiving the answer.

This process can be analogous to RPC remote calls, where A and B are each A service and A calls A method of B

Then the first thing is to establish A connection. After the establishment of the connection, A and B can communicate. Both sides need to say the same when communicating

(otherwise, no one can understand if one speaks Chinese and the other English), and the phone plays the role of transmission negative

Pass on what A and B say to each other.

Here is the procedure for implementing an RPC remote call:

  1. First establish a connection (make a call)

  2. Second request processing (B Brain thinking)

  3. Third Transport Protocol (language spoken by A and B)

  4. Fourth serialization (network serialization A, B)

1. Establish a connection

The first step of RPC implementation is the connection problem. Only the two parties establish the connection can proceed to the next step

This section uses the client server as an example.

We know that the client and the server can establish a connection through the TCP protocol in the network, and through

TCP Connection There are two most common ways to connect a network:

  • 1. HTTP implementation

  • 2、TCP/IP中的Socke

1.1. The HTTP

HTTP communication is completed through THE HTTP protocol. An HTTP request will establish a TCP connection, and then a three-way handshake will occur. After the connection is established, the request will be processed, and the request will be terminated by a four-time wave to disconnect the connection.

1.2. The Socket

Socket is also called a Socket, is a TCP/IP protocol implementation, the construction of a Socket such as:

Socket =IP address + port number

A pair of sockets is generated when a connection is established

Socket: ={socket1, socket2}={(IP1:port1),(IP2,port2)},

One on the client and one on the server. Socket communication is divided into four steps:

1, server listening:

ServerSocket first binds to a port using the bind() method, then calls the Listen () method to listen to the port and wait for the client to connect

2. Client request:

ClientSocket calls the connect() method to send a connection request to the port bound to the ServerSocket, requesting that a connection be established.

3. Connection confirmation:

When ServerSocket listens to a ClientSocket connection request, it calls the Accept () method in response to the ClientSocket request to establish a connection with the client.

4. Data transmission:

When the two sockets are connected, ClientSocket calls send() to send the data, ServerSocket calls Receive () to receive the data, and ServerSocket processes the request. When the request is completed, ServerSocket calls send(). ClientSocket calls the Receive method to receive the response data.

2. Request processing

After the server receives the request from the client, how does it process the request? For example, when a request comes to the server, the server allocates a thread to handle the request or multiple requests are processed by a thread. This is how the server handles requests

There are three processing methods on the server:

2.1. BIO(Synchronous Blocking)

The synchronous non-blocking approach means that the server generates one thread for each request sent by the client. But if the client requests too much the server thread reaches a bottleneck. In addition, if the client request does not request data, then the server thread will block, resulting in a waste of resources.

2.2. NIO(Synchronous Non-blocking)

Synchronous non-blocking solves the blocking problem in BIO, where a thread on the server binds multiple requests from the client, and a thread processes the client request by polling for multiple bound client requests. This approach is known as I/O multiplexing. This ensures that multiple client requests can be processed even if the server is single-threaded.

2.3. AIO(Asynchronous Non-blocking)

Asynchronous non-blocking is a better strategy. This approach is a good solution to the synchronization problem in NIO. The asynchronous approach can be understood as that the client initiates an I/O request and returns it directly. This approach is a good solution to the client waiting and blocking problems.

At this point, the connection between the client and the server has been established and the I/O requests have been made to each other. Then the data needs to be manipulated. There are two aspects of data manipulation: one is the transport protocol, and the other is serialization.

See Netty’s article here.

3. Transport protocol

First of all, the transmission protocol is a protocol for client and server to transmit data. Through this protocol, the transmission between each other is bound to ensure that the data of each other can be identified. Common transport protocols are

3.1. The HTTP protocol

HTTP protocol provides the client to send a request in the manner of GET, POST, DELETE, PUT, etc., the sent data is contained in a body, server processing completed return data to a response body, the response body contains data and status code and other data. The correctness of transmission between client and server is guaranteed by such a protocol.

3.2. DUBBO agreement

The Dubbo protocol is a protocol for transferring between services provided by Dubbo. The protocol defines the format of the requested data, through which the service can read and parse the data.

3.3. Web service agreement

Webservice is based on HTTP SOAP protocol to transmit data, WebService protocol is composed of HTTP + XML, where XML will use WSDL, WSDL is a format in the description language XML. In the use of the need to match the FORMAT of XML to achieve data packaging.

Through the analysis of the above protocols, it can be known that the client and the server can communicate and transmit data in accordance with the prescribed protocols to achieve barrier-free communication.

4. The serialization

Now that we’re done talking about transport protocols, let’s look at serialization. What is serialization? Serialization can be understood as, the client before sending data to encode the data according to certain rules, encoded data transmission in the network, after finally transferred to the server, the server receives the data after the need according to certain rules to decode the client sends the correct data, this is the serialization and deserialization.

The whole process can be understood as a key to open a lock process, only matching the correct lock core can open the lock, serialization and deserialization is the same principle.

So why code?

This needs to consider the problem of network transmission. We hope that the data transmission can be maximized. If a 1G file is reduced to 400M after coding, the data transmission efficiency will be greatly improved. There are two types of serialization: XML and JSON for text classes and PB/Thrift for binary classes.

5, summary

After the above four processes, a complete RPC remote call can be realized. For the specific implementation of each process, you can check the corresponding books to learn.

References:

1. “Hook Education Micro-service Course”