In a RESTful
architecture, the focus is on resources. Standard methods are used to retrieve and manipulate information fragments when operating on resources. In an RPC
architecture, the focus is on methods. Server methods are invoked in the same manner as invoking local methods.
REST
stands for Representational State Transfer, which is a software architectural style and can also be referred to as a pattern for designing APIs. REST
uses HTTP protocol to define general verb methods such as GET
, POST
, PUT
, DELETE
, and it uniquely identifies network resources via URIs. The response side, based on the different requirements of the request side, represents the requested resource through stateless communication. An architecture that conforms to the REST
design specifications is referred to as RESTful
.
- Everything on the network is abstracted as a resource.
- Each resource has a unique resource identifier.
- Operations on resources do not change the resource identifier.
- All operations are stateless.
- The same resource has multiple representations such as
xml
,json
, etc.
Security refers to accessing REST
interfaces without causing changes to the server's resource state. Idempotent means that when the same REST
interface URI is accessed multiple times, the resource state obtained is the same.
GET
: Safe and idempotent, used to read a resource.POST
: Not safe and not idempotent, used to create resources with automatically generated instance numbers on the server, and to update partial resources.PUT
: Not safe and idempotent, used to create resources and update resources with instance numbers on the client side.DELETE
: Not safe and idempotent, used to delete resources with instance numbers on the client side.
- To query a
user
,GET https://127.0.0.1/user/1
, directly carryparams
to query the user. - To add a
user
,POST https://127.0.0.1/user
, include user registration information in the requestbody
. - To modify a
user
,PUT https://127.0.0.1/user
, include theuserid
identification information in the requestbody
. - To delete a
user
,DELETE https://127.0.0.1/user
, include theuserid
identification information in the requestbody
. - Use the request header
Accept
to obtain different forms of the same resource, such asapplication/json
andapplication/xml
. - If the version number is considered as different representations of the same resource, it should also be distinguished in the
Accept
field instead of directly adding the version number to theURI
.
RPC
stands for Remote Procedure Call, which can be simply understood as one node requesting a service provided by another node. Remote procedure call is in contrast to local procedure call. When calling a method, the method on the remote server is invoked in the same manner as calling a local method, achieving lightweight and transparent communication.
- Client: The caller of the service.
- Server: The provider of the service.
- Client Stub: Packages client request parameters into network messages and sends them to the service provider.
- Server Stub: Receives messages sent by the client, unpacks the messages, and calls local methods.
Client
1. Maps the call to a Call Id
2. Serializes the Call Id, along with parameters, into binary form
3. Sends the serialized data packet over the network
4. Awaits server response
5. When the server call is successful and returns a result, deserializes and proceeds with the next steps
Server
1. Maintains a Call Id Map locally to ensure the correspondence between Id and the called method
2. Awaits client requests
3. Upon receiving a request, deserializes the data packet to obtain the Call Id and parameters
4. Searches the Map for the function pointer corresponding to the Call Id
5. Calls the function via the function pointer, passing the deserialized parameters, and obtains the result
6. Serializes the result and returns it to the client over the network
Note:
- Here, the client refers to the local caller, which can also be a server
- Here, the server refers to the callee, which can also be a server
- Whether using sockets for TCP transmission or HTTP for transmission, both are feasible for data packet communication
- In terms of communication protocol,
RESTful
uses theHTTP
protocol for data transmission, whileRPC
generally uses theTCP
protocol for data transmission. However, the transmission protocol is not the focus ofRPC
. Typically,TCP
protocol is used for its high efficiency, while usingHTTP
protocol for transmission is completely feasible. - In terms of performance, the transmission efficiency of
RPC
is higher than that ofRESTful
becauseRPC
has an efficient and compact inter-process communication mechanism and transmits small amounts of data, resulting in higher efficiency when exchanging large amounts of messages. - In terms of flexibility, the flexibility of the
RESTful
architecture is higher than that of theRPC
architecture. Using theRESTful
architecture provides better readability, whileRPC
is slightly cumbersome to write and debug. - Using the
RESTful
architecture for data transmission provides multi-language support. TheHTTP
protocol is relatively more standardized, universal, and standard. For middleware, the first supported protocols usually include theRESTful
data transmission specification. - It is recommended to use
RPC
for internal service invocation, whileRESTful
is recommended for external interfaces. For example, the microservices architecture pattern generally adopts theRPC
for internal andRESTful
for external communication pattern.
https://github.com/WindrunnerMax/EveryDay
https://www.jianshu.com/p/7d6853140e13
https://www.jianshu.com/p/ee92c9accedd
https://www.zhihu.com/question/28570307
https://www.zhihu.com/question/25536695
https://www.runoob.com/w3cnote/restful-architecture.html
https://blog.csdn.net/bieleyang/article/details/76272699
https://blog.csdn.net/u014590757/article/details/80233901