当前位置:网站首页>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 .
边栏推荐
- Chapter I Section I
- 云计算企业崛起 甲骨文数据库市场主导地位动摇
- UNIX Environment advanced programming --8- process control ---8.5 function exit-8.6 function wait and waitpid
- 【动态规划】入门篇
- C Oracle multi table query
- Docker deployment MySQL
- 基于单片机的简易电子钟设计
- Smart210 uses SD card to burn uboot
- 2022煤矿探放水特种作业证考试题库模拟考试平台操作
- 一文读懂简单查询代价估算【这次高斯不是数学家】
猜你喜欢
C# Oracle 多表查询
UNIX Environment advanced programming --8- process control ---8.5 function exit-8.6 function wait and waitpid
MySQL中redo日志和undo日志简述
Actual combat simulation │ real time error alarm of enterprise wechat robot
Redundancy code question type -- the difference between adding 0 after
ThingsBoard教程(二一):使用消息类型和数据处理节点对数据处理后保存
【20220526】UE5.0.2 release d11782b
说说MySQL索引机制
多线程 从UE4的无锁队列开始 (线程安全)
Implementation of fruit mall wholesale platform based on SSM
随机推荐
Apple zoom! It's done so well
index查list 注入的是mysql 执行的是oracle
June training (day 13) - two way linked list
基于SSM实现水果商城批发平台
C# Oracle 多表查询
简述请求过程
Oracle自定义数据类型Type疑问
Node-RED系列(二五):集成高德地图并实现3d地图和轨迹回放
About the problem of database: it can't be found after repeated inspection
Advanced technology management - what management tools can managers use
Test cases that testers must master
Knowledge points of silicon steel sheet
类文件结构和类加载过程执行引擎简述
电解电容、钽电容、普通电容
MySQL利用E-R模型的数据库概念设计
Thingsboard tutorial (20): filtering telemetry data using regular chains
实战模拟│企业微信机器人实时报错预警
2022年劳务员-通用基础(劳务员)上岗证题目及答案
Execution order of subclass and parent constructor
Cynthia project defect management system