当前位置:网站首页>Understanding RPC and rest
Understanding RPC and rest
2022-06-13 10:26:00 【Hua Weiyun】
What is? RPC?
Remote procedure call , Just two services A、B, An application is deployed in A Server , Want to call B Functions provided by the application on the server / Method , Because it's not in a memory space , Can't call directly , We need to express the semantics and data of the call through the network .
RPC Will hide the underlying communication details ( It doesn't need to be dealt with directly Socket Communication or Http Communications )
RPC It's a request response model . Client initiates request , Server returns response ( Be similar to Http How it works )
that :
- First , To solve the problem of communication , 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 .
- second , To solve the problem of addressing , in other words ,A How does the application on the server tell the underlying RPC frame , How to connect to B The server ( Such as mainframe or IP Address ) And specific ports , What is the name of the method , This completes the call . For example, based on Web Service protocol stack RPC, We need to provide a endpoint
URI, Or from UDDI Find... On the service . If it is RMI Call it , I need one more RMI Registry To register the service address . - Third , When A When an application on the server initiates a remote procedure call , The parameters of the method need to pass the underlying network protocol such as TCP Pass on to B The server , Because the network protocol is based on binary , The values of parameters in memory should be sequenced into binary form , That is serialization (Serialize) Or group (marshal), Send serialized binaries to by addressing and transferring B The server .
- Fourth ,B After the server receives the request , You need to deserialize the parameters ( The reverse operation of serialization ), Revert to in memory expression , Then find the corresponding method ( Part of addressing ) Make a local call , And then you get the return value .
- The fifth , The return value is also sent back to the server A Application on , It also needs to be sent by serialization , The server A When received , Deserialization , Revert to in memory expression , hand A Applications on the server .

altogether 9 Step :
- 1) Service consumer (client) The invocation invokes the service as a local invocation ;
- 2)client stub Responsible for receiving the call 、 The parameters are assembled into a message body capable of network transmission ;
- 3)client stub Find the service address , And send the message to the server ;
- 4)server stub Decode the message upon receipt ;
- 5)server stub The local service is invoked based on the decoding results ;
- 6) The local service executes and returns the result to server stub;
- 7)server stub The returned result is packaged into a message and sent to the consumer ;
- 8)client stub Message received , And decode it ;
- 9) Service consumers get the end result .
quote :https://www.zhihu.com/question/25536695/answer/109977506
What is? REST?
REST It's an architectural style , A set of architectural constraints and principles . Applications or designs that meet these constraints and principles are RESTful.REST Norms treat everything as a resource , Everything on the Internet is full of resources .
REST No new technology has been created , Components or services , Just use Web The existing characteristics and capabilities of . Can pass through completely HTTP Protocol implementation , Use HTTP Protocol deals with data communication .REST The operation of architecture on resources includes obtaining 、 establish 、 The operations of modifying and deleting resources are exactly the same HTTP Provided by agreement GET、POST、PUT and DELETE Method .
REST and RPC Comparison :
RPC To realize some pain points of inter service invocation :
- 1.RPC The way the service provider and the caller interface depend on each other is too strong , It will lead to the complexity of coding , and REST Interface comparison RPC More lightweight , The dependency between the service provider and the caller depends only on a contract , There is no strong code level dependency .
- 2.RPC Services are platform sensitive , It is difficult to simply reuse :REST Cross platform , The caller of any language can implement... According to the interface definition .
Unified interface
RESTful Architectural style rules , Meta operation of data , namely CRUD(create, read, update and delete, That is, the addition, deletion, search and modification of data ) operation , Corresponding to HTTP Method :GET Used to obtain resources ,POST Used to create a new resource ( Can also be used to update resources ),PUT Used to update resources ,DELETE Used to delete resources , In this way, the interface of data operation is unified , Only through HTTP Method , You can complete all the work of adding, deleting, checking and modifying data .
namely :
- GET(SELECT): Get resources from the server ( One or more ).
- POST(CREATE): Create a new resource on the server .
- PUT(UPDATE): Update resources on the server ( The client provides complete resource data ).
- PATCH(UPDATE): Update resources on the server ( The client provides the resource data that needs to be modified ).
- DELETE(DELETE): Remove resources from server .
URI
You can use a URI( Uniform resource locator ) Point to resources , each URI All correspond to a specific resource . To get this resource , Visit its URI Can , therefore URI It becomes the address or ID of each resource .
General , At least one for each resource URI With the corresponding , Most typical URI namely URL
No state
The so-called stateless , That is, all resources , Both can pass URI location , And this positioning has nothing to do with other resources , Nor will it change because of changes in other resources . The difference between stateful and stateless , Take a simple example to illustrate . For example, query the salary of employees , If you need to log in to the system to query salary , Enter the salary query page , After performing the relevant operations , How much do you get paid , Then there is a state , Because every step of querying salary depends on the previous step , As long as the pre operation is not successful , Subsequent operations cannot be performed ; If you enter a url You can get the salary of the designated employee , Then this situation is stateless , Because getting paid doesn't depend on other resources or status , And in this case , Employee pay is a resource , By a url With the corresponding , Can pass HTTP Medium GET Method to get resources , This is typical RESTful style .
RPC And RESTful The difference is shown in the following two figures :

Dubbo Adopted RPC Service call for ,SpringCloud Adopted REST.

How to choose micro service communication
We still need to consider the actual situation , Personal understanding
- There are strict requirements for performance :RPC
- Consider the cost of learning , The difficulty of team members and the cost of development efficiency :REST
- Demand for opening up ,rpc Need further conversion , and rest It can be directly opened to the outside world :REST
- Code coupling requires loose coupling :REST
- Integration with other frameworks is less difficult , The microservice framework basically supports rest:REST
- Asynchronous requirements ,rest Additional means of implementation are required , Such as through middleware :RPC
Suggest :
REST It's very convenient to call and test ,RPC It seems a little cumbersome , however RPC There is no doubt about the efficiency of , Therefore, it is recommended to use... For internal calls between multiple systems RPC. Services provided to the outside world ,Rest More appropriate .
边栏推荐
猜你喜欢

ThingsBoard教程(二一):使用消息类型和数据处理节点对数据处理后保存

虚拟机内存结构简述

Node red series (24): use MySQL nodes in node red to add, delete, modify and query databases

deepin系统中Qt5.12无法输入中文(无法切换中文输入法)解决办法

中国SaaS产业全景图谱

Index query list injects MySQL and executes Oracle
![[bearing fault decomposition] ITD bearing fault signal decomposition based on MATLAB [including Matlab source code 1871]](/img/4a/3beaeb1b55acc9e61d3b07dff56352.png)
[bearing fault decomposition] ITD bearing fault signal decomposition based on MATLAB [including Matlab source code 1871]

Node-RED系列(二七):扩展节点之s7节点的使用说明

UNIX Environment advanced programming --8- process control ---8.5 function exit-8.6 function wait and waitpid

基于SSM实现水果商城批发平台
随机推荐
matlab轮毂电机分析模糊pid控制垂向振动分析
How to calculate ASCII code value and how to calculate arccos value
Record several interesting XSS vulnerability discoveries
ADG备库MRP0状态WAIT_FOR_GAP
递归想法和实现介绍,消除递归
Knowledge points of silicon steel sheet
测试人员必须掌握的测试用例
Docker部署Mysql
About the problem of database: it can't be found after repeated inspection
Classical convolutional neural network model
C 11 more practical NAMEOF
第一章 第一节
一文读懂pgstat【这次高斯不是数学家】
Vivo large scale kubernetes cluster automation operation and maintenance practice
[image denoising] image denoising based on MATLAB Gaussian + mean + median + bilateral filtering [including Matlab source code 1872]
ADG standby mrp0 status wait_ FOR_ GAP
低代码开发一个基础模块
PyTorch基础(二)-- 张量与梯度
Programming principles
等个有“源”人|OpenHarmony 成长计划学生挑战赛报名启动