当前位置:网站首页>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
边栏推荐
- 基于can总线的A2L文件解析(3)
- Nanjing University: Discussion on the training program of digital talents in the new era
- 彻底理解为什么网络 I/O 会被阻塞?
- [QNX Hypervisor 2.2用户手册]6.3.2 配置VM
- node_exporter内存使用率不显示
- Clickhouse (03) how to install and deploy Clickhouse
- Image classification, just look at me!
- 开户注册股票炒股安全吗?有没有风险的?靠谱吗?
- Personal understanding of convolutional neural network
- How to obtain the coordinates of the aircraft passing through both ends of the radar
猜你喜欢

Find the first k small element select_ k

使用Jmeter虚拟化table失败

pytorch yolov5 训练自定义数据

第十届全球云计算大会 | 华云数据荣获“2013-2022十周年特别贡献奖”

node_exporter内存使用率不显示

Reptile 01 basic principles of reptile

Copy the linked list with random pointer in the "Li Kou brush question plan"

@Extension、@SPI注解原理

Pytorch yolov5 training custom data

记录Pytorch中的eval()和no_grad()
随机推荐
怎么自动安装pythn三方库
[QNX Hypervisor 2.2用户手册]6.3.2 配置VM
Electron安装问题
The main thread anr exception is caused by too many binder development threads
解决 contents have differences only in line separators
Writing writing writing
OpenShift常用管理命令杂记
Privacy computing helps secure data circulation and sharing
U-Net: Convolutional Networks for Biomedical Images Segmentation
websocket 工具的使用
Penetrate the whole intranet through socks agent
Can communication of nano
sample_rate(采样率),sample(采样),duration(时长)是什么关系
FCN: Fully Convolutional Networks for Semantic Segmentation
Memory leak of viewpager + recyclerview
Matlab built-in function how different colors, matlab subsection function different colors drawing
Whether to take a duplicate subset with duplicate elements [how to take a subset? How to remove duplicates?]
Electron installation problems
buuctf-pwn write-ups (9)
第十届全球云计算大会 | 华云数据荣获“2013-2022十周年特别贡献奖”