当前位置:网站首页>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 .

 Insert picture description here
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 .
 Insert picture description here
RPC And RESTful The difference is shown in the following two figures :
 Insert picture description here  Insert picture description here
Dubbo Adopted RPC Service call for ,SpringCloud Adopted REST.
 Insert picture description here
 Insert picture description here
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 .

原网站

版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206131005111673.html