RPC, remote procedure call, defines the behavior of a program on one machine calling a subroutine on another machine. Features:
Move the remote implementation to the local, and there is no difference between remote calls and local calls in terms of effect
Using cs mode, the client initiates a request, and the server executes after receiving the request parameters
Shielding the underlying complexity of cross-process and cross-network calls allows us to focus more on business logic
1.2 Specific Implementation Framework
dubbo(apache alibaba java)
motan (Weibo)
tars (Tencent internal)
grpc
thrift
spring cloud openfeign
1.3 Application Scenarios
Cross-network communication can be used
2. Key technical points of RPC & one-time RPC call process
2.1 RPC Process
process
The client calls the interface to the proxy class, assembles the request and serializes it, then encodes the protocol and sends it
The server receives the request, parses the protocol and deserializes it to obtain the request parameters.
The server calls the interface implementation based on the request parameters and then assembles the response
The response is returned in the same way
How are the two network modules connected?
The registration center is a place to store data, and it is best to provide monitoring functions. The registration center is separate from the rpc framework Common registration centers: zookeeper, nacos, etcd
Other Features
Routing filters available providers
Load balancing: Choose which provider to use from the available providers
Fuse current limiting: flow control
Network Processing
Protocol processing
RPC Advantages
Makes it easier to build distributed applications, decouple services, and make them easier to expand
RPC generally uses a long connection, so there is no need to establish a connection every time you communicate, reducing network overhead
RPC requires a registration center to dynamically perceive and visualize service changes
Rich background management functions, unified management interface services, unimpeded to the caller, unified operation
The protocol is streamlined, more efficient, and more private and secure
With load balancing, fuse limiting and other functions
2.2 Serialization Technology
Any serialization framework: the core idea is designA serialization protocol,Write the object type, attribute type and attribute value into a binary byte stream in a fixed format to complete serialization, and then read out the object type, attribute type, and attribute value one by one in a fixed format, and recreate a new object with this information to complete the deserialization
Serialization method
JDK native serialization
Lightweight text data exchange format-json/XML
Good readability, easy to read and debug, and supports multiple languages. The byte file after serialization is relatively large and the efficiency is relatively low, but it is smaller than the byte stream after XML serialization. It is widely used in enterprises, especially for providing APIs to front-ends and third parties.
HessianIt is a dynamically typed, binary, and cross-language Xu Lihua framework
Hessian is much more efficient than JDK and JSON serialization.And the number of bytes generated is also smaller. It has very good compatibility and stability,Therefore, Hessian is more suitable as a serialization protocol for remote communication in the RPC framework.
protobuf
An open source sequence library launched by Google, it is a lightweight and efficient structured data storage format that supports multiple languages.
Fast speed, high compression ratio, small size, the serialized size is much smaller than JSON and Hessian. The expansion, upgrade and compatibility of the course color disk format are good, and it can be backward compatible.
How does PRC choose a serialization framework?
Selection factors
Security: The first consideration is that if there is a security vulnerability in serialization, then the online service is likely to be invaded (JDK native serialization has a vulnerability
Compatibility: Is the serialization protocol compatible after version upgrade? Is it cross-platform and cross-language?
Versatility: It can serialize and deserialize any type, and the server will not suddenly fail to call the service interface method after adding a parameter of a certain type.
Performance and efficiency: Serialization and deserialization are necessary processes for RPC calls. The performance and efficiency will directly affect the overall performance and efficiency of the RPC framework.
Space overhead: The size of the serialized binary data. The smaller the size of the serialized byte data, the smaller the amount of data transmitted over the network, and the faster the data transmission speed. In RPC calls, it is directly related to the time taken to respond to requests.
Considerations
Avoid objects that are too complex, have many attributes, and have multiple layers of nesting.
Avoid objects that are too large: large strings, very large arrays, etc.
Avoid passing types that are not supported by the serialization framework as parameters
Prevent objects from having complex inheritance relationships
2.3 Application layer communication protocol - http
2.3.1 Basic Concepts
Most RPCs are self-developed http and also support http1.1
What is IO
IO is the process of copying data between the computer and external devices. When network data arrives, it is first stored in the kernel cache of the operating system, waiting for the application to collect it.
Edge Trigger
When using edge-triggered mode, when a readable event occurs on the monitored Socket descriptor, the server will only wake up from epoll_wait once. Even if the process does not call the read function to read data from the kernel, it will still only wake up once. Therefore, our program must ensure that the data in the kernel buffer is read at one time.
Level trigger
When using the horizontal trigger mode, when a readable event occurs on the monitored Socket, the server will continue to wake up from epoll_wait until the kernel buffer data is read by the read function, in order to tell us that there is data to be read.
Event-driven IO
After initiating a read request, wait for the read-ready event notification before reading the data.
Asynchronous IO
After initiating a read request, wait for the operating system to notify you when the reading is complete, and completely hand over the function to the operating system.
2.3.2 What are the IO models of operating systems?
The first stage of read is blocked, which is what we often call blocking IO. That is, if the first stage of read is blocked waiting for read read, we call it blocking IO.