当前位置:网站首页>RPC protocol details
RPC protocol details
2022-07-05 18:29:00 【Knock the little crisp of the code】
One 、 What is? RPC agreement
Remote procedure call protocol , It is a way of requesting services over a network from a remote computer program , You don't need to know the protocols of the underlying network technology .
The specific calling process is :
1. Serving consumers (client client ) Call the service that needs to be consumed by calling the local service ;
2. Client stub (client stub) After receiving the call request, it is responsible for putting the method 、 Serialization of input parameter information ( assemble ) To be a message body capable of network transmission ;
3. Client stub (client stub) Find the remote service address , And send the message to the server through the network ;
4. Server stub (server stub) Decode the message upon receipt ( Deserialization operation );
5. Server stub (server stub) Call the local service according to the decoding result for relevant processing ;
6. The local service executes the specific business logic and returns the processing result to the server stub (server stub);
7. Server stub (server stub) Repackage the returned results into messages ( serialize ) And send it to the consumer through the Internet ;
8. Client stub (client stub) Message received , And decode it ( Deserialization );
9. Service consumers get the end result ;
and RPC The goal of the framework is to achieve the 2-9 The steps are well sealed , That is to call 、 code / The decoding process is encapsulated , Let the user feel like calling a local service to call a remote service .
Two 、 Realization RPC Problems to be solved
Communication problems
The remote invocation , There must be communication between the two machines . Mainly through the establishment between the client and the server TCP Connect , All the data exchanged by the remote procedure call is transferred in this connection . Connections can be on-demand , It will be broken after the call , It can also be a long connection , Multiple remote procedure calls share the same connection .
Usually this connection can be on-demand ( When it needs to be called, establish the connection first , It will be broken immediately after the call ), It can also be a long connection ( Keep the client and server connected for a long time , No matter whether there is a packet sent at this time , It can cooperate with heartbeat detection mechanism to regularly detect whether the established connection is alive and effective ), Multiple remote procedure calls share the same connection .
Service addressing
above-mentioned ,RPC It is local oriented . Then the user makes a local call , How to find the corresponding remote call , need RPC Implement at the bottom . Need to manage ip, port , Service list and other information . It's common to use zookeeper To manage .
For producers :
When the service starts , Register your services to the Registration Center ;
When the producer hangs up , The registry logs out of the stopped service ;
The producer sends heartbeat to the registration center regularly , If the heartbeat is abnormal , The registry also unregisters producer services .
For consumers :
Subscribe to the service of the registry , obtain ip, Port and other service information ;
When producers go online and offline , The registry will notify the caller ;
After the service is offline , Consumers no longer consume the service .
Network transmission
When A The application on the machine initiates a RPC Invocation time , Call method and its input parameter information need to go through the underlying network protocol, such as TCP Transferred to the B machine , Because the network protocol is based on binary , All the parameter data we transmit needs to be serialized first (Serialize) Or group (marshal) Only in binary form can it be transmitted in the network . Then the binary data after serialization or marshalling is sent to B machine .
When B The machine received A After the request from the application of the machine , It also needs to deserialize the received parameters and other information ( The reverse operation of serialization ), To restore binary information to an in memory representation , Then find the corresponding method ( Part of addressing ) Make a local call ( It's usually through the generation of agents Proxy To call , Usually there will be JDK A dynamic proxy 、CGLIB A dynamic proxy 、Javassist Bytecode generation technology, etc ), Then get the return value of the call .
The service call
B The machine makes local calls ( Through agency Proxy And reflection calls ) Then we get the return value , At this time, you need to send the return value back to A machine , It also needs to be serialized , And then send the binary data back to A machine , And when A After the machine receives these return values , Then deserialize again , Revert to in memory expression , Give it to A The application on the machine carries out relevant processing , Generally, business logic processing operations .
3、 ... and 、RPC And REST The difference between ?
RPC And REST The biggest difference is RPC Provides better abstraction ,RPC Even completely hide the details of network transmission , and REST No, . say concretely ,REST As a minimum, it is required to provide URL And request parameters , and RPC Hide the implementation details related to network transmission . On the other hand ,RPC It can be based on any network communication protocol , and REST Usually based on HTTP( perhaps HTTPS) agreement .RPC The caller doesn't care if the specific agreement is :HTTP、TCP Or any other custom protocol .
Four 、 Advantages and disadvantages
advantage :
Help the client communicate with the server through the procedure call of traditional high-level language .
It can be used in distributed environment and local environment .
Support process oriented and thread oriented models .
Hide the internal messaging mechanism from users .
Code can be rewritten and redeveloped with minimal effort .
Provide abstraction , That is to hide the message transmission nature of network communication from users .
Omit many protocol layers to improve performance .
shortcoming :
The client and server use different execution environments for their routines , And resources ( for example , file ) The use of is also more complex . therefore ,RPC Systems are not always suitable for transmitting large amounts of data .
RPC Very prone to failure , Because it involves communication systems , Another computer and another process .
RPC There is no uniform standard ; It can be realized in many ways .
RPC Just based on Interaction , therefore , In terms of hardware architecture , It does not provide any flexibility .
Reference article :
RPC Protocol analysis [ Easy to understand ]
Thousand words take you to know what is RPC agreement
边栏推荐
- Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]
- 含重复元素取不重复子集[如何取子集?如何去重?]
- 开户注册股票炒股安全吗?有没有风险的?靠谱吗?
- Privacy computing helps secure data circulation and sharing
- Le cours d'apprentissage de la machine 2022 de l'équipe Wunda arrive.
- 生词生词生词生词[2]
- 英语句式参考
- Is it safe for Apple mobile phone to speculate in stocks? Is it a fraud to get new debts?
- 第十一届中国云计算标准和应用大会 | 云计算国家标准及白皮书系列发布 华云数据全面参与编制
- 在通达信上做基金定投安全吗?
猜你喜欢
Memory leak of viewpager + recyclerview
Privacy computing helps secure data circulation and sharing
Nacos distributed transactions Seata * * install JDK on Linux, mysql5.7 start Nacos configure ideal call interface coordination (nanny level detail tutorial)
Whether to take a duplicate subset with duplicate elements [how to take a subset? How to remove duplicates?]
第十一届中国云计算标准和应用大会 | 华云数据成为全国信标委云计算标准工作组云迁移专题组副组长单位副组长单位
解决 contents have differences only in line separators
案例分享|金融业数据运营运维一体化建设
瞅一瞅JUC提供的限流工具Semaphore
Can communication of nano
使用JMeter录制脚本并调试
随机推荐
English sentence pattern reference
LeetCode 6111. 螺旋矩阵 IV
Insufficient picture data? I made a free image enhancement software
The origin of PTS, DTS and duration of audio and video packages
Generate classes from XML schema
关于服装ERP,你想知道的都在这里了
How can cluster deployment solve the needs of massive video access and large concurrency?
Cronab log: how to record the output of my cron script
第十一届中国云计算标准和应用大会 | 华云数据成为全国信标委云计算标准工作组云迁移专题组副组长单位副组长单位
音视频包的pts,dts,duration的由来.
Use QT to traverse JSON documents and search sub objects
Trust counts the number of occurrences of words in the file
Notes on common management commands of openshift
Memory management chapter of Kobayashi coding
含重复元素取不重复子集[如何取子集?如何去重?]
ConvMAE(2022-05)
怎么自动安装pythn三方库
分享:中兴 远航 30 pro root 解锁BL magisk ZTE 7532N 8040N 9041N 刷机 刷面具原厂刷机包 root方法下载
[QNX hypervisor 2.2 user manual]6.3.2 configuring VM
Can communication of nano