当前位置:网站首页>Simple understanding of RPC
Simple understanding of RPC
2022-07-25 04:05:00 【zhanghuaichao】
This article mainly wants to make a record , Mainly reprinted from :
RPC The implementation of the -Aquester-ChinaUnix Blog
1. Preface
RPC Its full name is Remote Procedure Call, That is, far procedure call . without RPC, Then the process communication between machines usually takes the form of message ( Network communication tcp perhaps udp), This reduces development efficiency , It will also increase the coupling between the network layer and the upper layer ,RPC Can help us solve these problems .

In fact, it encapsulates the implementation details of network communication , Not coupled with specific communication , No need to write the code of network communication , Naturally, development efficiency will be improved . There are two main ways I use in my work rpc Namely Zeroc Of ice, as well as google Of grpc.
2. Basic concepts
Before the formal explanation , Let's introduce the relationship between RPC Basic concepts about :
2.1. IDL
IDL The full name is Interface Definition Language, Interface definition language ( Sometimes called interface description language ). because RPC Usually cross process 、 Cross machine 、 Cross system and cross language ,IDL To solve this problem , It has nothing to do with language , Translate it into different programming languages with the help of compiler .
Google Open source ProtoBuf Medium “.proto” A document is one IDL file :
message Person {
required int32 id = 1;
required string name = 2;
optional string email = 3;
}
stay .proto In file message Be similar to C In language struct Of , convert to C++ After language , It corresponds to C++ A class in . of ProtoBuf For more information , May refer to :.
Please note that ,IDL Data types in ( Such as ProtoBuf Medium int32) Is independent of any language , But it usually has a mapping relationship with the data types in the target language , Otherwise, you will not be able to IDL Compile the file into the target language file .
2.2. agent (Proxy)
agent (Proxy) yes RPC The client implementation of , Client code always communicates with... Through a proxy RPC Server communication .Proxy The code is completely composed of IDL Compiler generation .
2.3. stub (Stub)
stub (Stub) yes RPC Server implementation of . On the server , Need to achieve IDL The interface defined in the file ; On the client side, you can use . The relationship between proxy and stub is shown in the figure below :

Here I'll explain a little bit ,Proxy, Agency is generally what we call positive agency , It proxy server . And that stub Stub, Generally speaking, it means Left manuscript , The manuscript must be on the server , Then we need to realize on the server side Stub The method in , because Stub The framework is also written according to you IDL Language generated , And users should realize Implement This class , We usually use this when we use it Implement Pointer form of , Because we may not necessarily achieve Stub All interfaces of , So maybe we can't instantiate objects . Then no matter which framework , We all have to put our Implement The pointer of is registered to this Stub Or the bottom , Because if the bottom layer receives data , You must trigger polymorphism through pointers to call the methods you implement .
3. Three elements
To achieve a complete RPC, Three things need to be done , Here we call these three things three elements :

3.1. Network communication
Be responsible for sending the client's request to the server , And sending the response of the server back to the client . This is a familiar piece , It is mainly the implementation of high-performance network program .
3.2. Message codec
IDL Interface defined in 、 Functions and data, etc , It needs to be encoded into a byte stream before sending , Decode after receiving . For example, the function name 、 The parameter type and parameter value are encoded into byte stream , Then send it to the opposite end , Then decode the end , Revert to function call .ProtoBuf Is a very good codec tool .
Please note that IDL All data types supported are required to be codec ,IDL The compiler needs to know how to encode it into a byte stream , And decoding and restoring from the byte stream .
3.3. IDL compiler
For most people , This job is strange , Because there is not much contact with daily development . And because of that , Most people will find it difficult to be profound . In fact, as long as we overcome psychological obstacles , Learning it is much easier than expected . To make a long story short , It's not hard to get started , Especially after reading this article . But if you need to implement something like Hive or GCC East east , That's quite difficult , Among them, optimizing the syntax tree is a very big challenge .
In order to be able to use RPC, Need to put IDL Compile the file into the specified language code .ProtoBuf In fact, this function has been realized . If based on ProtoBuf Achieve one RPC, Then this IDL The compilation step can be skipped , Will only need to realize network communication , And realize google::protobuf::RpcController and google::protobuf::RpcChannel Two interfaces .
This article is to introduce RPC The implementation of the , The goal is to enable readers to achieve a set of RPC, For the three elements of network communication and message encoding and decoding , It's easy for ordinary people to understand and get started , But for the IDL Compiling this piece is relatively strange . So , The rest of this article will focus on IDL Implementation of compilation , All implementations will be based on Flex and Bison Two open source tools , Of course, you can also use JavaCC、SableCC and Antlr(ANother Tool for Language Recognition) etc. .
边栏推荐
- Huawei od maximum consumption JS
- Network security - information hiding - use steganography to prevent sensitive data from being stolen
- The difference between apply, call and bind
- Chapter 18 request matcher serverwebexchangematcher of oauth2loginauthenticationwebfilter
- MySQL select query part 2
- C language: string processing function
- Day008 select structure (switch statement)
- P100 MSSQL database penetration test of secondary vocational network security skills competition
- JS absolute minimum value of the sum of Huawei od two numbers
- Deeply understand the connection state and reliable mechanism of TCP protocol
猜你喜欢

Nested if selection structure and switch selection structure

Spirng security (VIII) multiple filter chains coexist

Acwing 870. approximate number

Deep learning method of building a model from zero

Wechat applet access wechat payment process

Traditional architecture of enterprise digital transformation to medium platform micro service architecture

Es- retrieve the selected field from the search

Modbus poll/slave simulator tutorial

C language: string processing function

Hw2021 attack and defense drill experience - Insights
随机推荐
51 single chip microcomputer measures the period of square wave signal
What are the models of asemi from the manufacturer of rectifier bridge and how about the electroplating process of the manufacturer of rectifier bridge?
Visual studio 2019 installation and uninstallation problems
13. Talk about the expiration strategy of redis
Advantages and disadvantages of zero trust security
Network security - information hiding - use steganography to prevent sensitive data from being stolen
DNS domain name resolution service
Chapter 3 business function development (modify the remarks of market activities)
Yuntu says digital asset chain: your God of digital asset property protection
Pytorch deep learning practice lesson 8 importing data
To clarify the tax arrears: there is no tax arrears, and will continue to operate in compliance, rooted in China
[understanding of opportunity-47]: Guiguzi - Chapter 11 - decision makers, moderation, and rational distribution of interests
01_ Education 1
Operations in shell
EMQ Yingyun technology was successfully selected into the 2022 "cutting edge 100" list of Chinese entrepreneurs
Construction of data center (I): background of the emergence of data center
Divide candy Huawei od JS
Traditional architecture of enterprise digital transformation to medium platform micro service architecture
Homologous strategy, surface longitude
Project launch process of software testing technology