In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "what is the use of the RPC framework", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "what is the use of the RPC framework" bar!
Local procedure call
RPC is to call remote functions as if they were local functions. Before we look at RPC, let's take a look at how local calls are tuned. Suppose we want to call the function Multiply to calculate the result of lvalue * rvalue:
Int Multiply (int l, int r) {int y = l * r; return y;} int lvalue = 10 l_times_r rvalue = 20 Multiply (lvalue, rvalue)
So at line 8, we actually did the following:
Stack the values of lvalue and rvalue
Go into the Multiply function, take the values 10 and 20 from the stack, and assign them to l and r
Execute the second line of code, calculate l * r, and store the result in y
Stack the value of y and return from Multiply
Line 8, take the return value 200 from the stack and assign it to l_times_r
The above five steps are the process of performing a local call. (20190116 Note: the above steps are only intended to illustrate the principle. In fact, the compiler will often do optimization, for a small number of parameters and return values will be directly stored in the register, without the need for stack bounce stack process, or even need to call call, but directly do the inline operation. In terms of principle alone, there is no problem with these five steps. )
New problems caused by remote procedure call
When calling remotely, the function body that we need to execute is on the remote machine, that is, Multiply is executed in another process. This raises several new questions:
Call ID mapping. How do we tell the remote machine that we want to call Multiply instead of Add or FooBar? In local calls, the function body is specified directly through the function pointer. When we call Multiply, the compiler automatically calls its corresponding function pointer for us. But in remote calls, function pointers do not work, because the address spaces of the two processes are completely different. So, in RPC, all functions must have their own ID. This ID is the only one that is determined in all processes. The client must attach this ID when making a remote procedure call. Then we need to maintain a corresponding table of {function Call ID} on the client and server, respectively. The tables of the two need not be exactly the same, but the Call ID for the same function must be the same. When the client needs to make a remote call, it looks up the table, finds the corresponding Call ID, and then passes it to the server. The server also looks up the table to determine the function that the client needs to call, and then executes the code of the corresponding function.
Serialization and deserialization. How does the client pass the parameter value to the remote function? In the local call, we just need to press the parameters into the stack and let the function read it on the stack. However, in remote procedure calls, the client and the server are different processes, and parameters cannot be passed through memory. Sometimes even the client and the server do not use the same language (for example, the server uses Cellular clients, and the client uses Java or Python). At this time, it is necessary for the client to convert the parameters into a byte stream, pass it to the server, and then convert the byte stream into a format that it can read. This process is called serialization and deserialization. Similarly, the value returned from the server also requires the process of serialization and deserialization.
Network transmission. Remote calls are often used on the network, and the client and server are connected through the network. All data needs to be transmitted over the network, so there needs to be a network transport layer. The network transport layer needs to pass the Call ID and serialized parameter byte stream to the server, and then send the serialized call result back to the client. As long as it can accomplish both, it can be used as the transport layer. Therefore, the protocol it uses is actually unlimited, as long as it can complete the transmission. Although most RPC frameworks use the TCP protocol, UDP also works, while gRPC simply uses HTTP2. Java's Netty also belongs to this layer.
With these three mechanisms, RPC can be implemented as follows:
/ / Client side / / int l_times_r = Call (ServerAddr, Multiply, lvalue, rvalue) 1. Map this call to Call ID. This assumes that the simplest string is used as the Call ID method 2. Serialize Call ID,lvalue and rvalue. You can package their values directly in binary form. Send the packet obtained in 2 to ServerAddr, which requires the use of the network transport layer 4. Wait for the server to return the result 5. If the server call succeeds, the result is deserialized and assigned to the l_times_r// Server side 1. 0. To maintain a Call ID-to-function pointer mapping call_id_map locally, you can use std::map2. Wait for request 3. After getting a request, the packet is deserialized to get Call ID4. By searching in call_id_map, we can get the corresponding function pointer 5. 5. After deserializing lvalue and rvalue, call the Multiply function locally, and get the result 6. 0. Serialize the result and return it to Client over the network
So to implement a RPC framework, you only need to follow the above process to basically complete the implementation.
Where:
The Call ID mapping can use either a function numeric string directly or an integer ID. A mapping table is usually a hash table.
Serialization and deserialization can be written on your own, or you can use things like Protobuf or FlatBuffers.
The network transport library can write its own socket, or use asio,ZeroMQ,Netty or the like.
Of course, there are some details that can be filled in, such as how to handle network errors, how to prevent attacks, how to do flow control, and so on. But with the above architecture, these can continue to be added.
Thank you for your reading, the above is the content of "what is the use of the RPC framework", after the study of this article, I believe you have a deeper understanding of the use of the RPC framework, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.