当前位置:网站首页>RPC and rest
RPC and rest
2022-07-29 08:22:00 【Rain melody】
One 、 background
Before leader Ask me feign Support is http Of rest Call or tcp/ip Of rpc call , In fact, I'm not very clear about this problem until now , Remember only by impression feign Is based on http Protocol implemented , I answered at that time http,leader say feign It's really based on http Remote call service framework , But it also supports rpc, In fact, as long as you call the remote interface like calling the local interface , Namely rpc. Remember their differences, but for feign Don't divide them too much , But remember that it is based on http Protocol implemented .
In fact, I was quite confused at that time , Mainly for myself rpc and rest Completely confused , Even think they are one thing , Now let's learn the differences between them .
Two 、RPC call
- RPC Is the abbreviation of remote call procedure , It's an agreement , In the fifth layer of network communication protocol : The session layer , The next is TCP/IP agreement , In the communication session protocol based on it .RPC Defines the mode of interaction , And applications use these patterns , To access other servers , It doesn't need to be related to specific details on the network .
2.1、RPC Basic knowledge of
1.RPC Pattern
①RPC use C/S Pattern , The client sends the request , Server response .
- The server - The client , namely Client-Server(C/S) structure .C/S The structure usually has two layers . The server is responsible for data management , The client is responsible for completing the interactive tasks with users .
② Based on the underlying protocol , such as TCP/IP Pattern .
- RPC Not equal to RPC frame ,RPC It's a concept , It's a distributed communication mode
- be based on RPC A lot of RPC frame :Dubbo、Netty、gRPC、BRPC、Thrift、JSON-RPC wait
- RPC Frame pair RPC It has rich functions , Include : Service registration 、 Service discovery 、 Service governance 、 Service monitoring 、 Service load balancing and other functions
- Two machines interact with each other , So network communication protocol is indispensable ,TCP / IP It becomes a point that can't be bypassed , So the way that our ancestors first thought of was through TCP / IP To call remote methods
2. Design purpose
① Through a fixed agreement , Call non native methods
② Realize the communication between different program languages
③ There is no need to understand the underlying protocol , Tune it like a local method . it Completely encapsulated network transmission , And other details .
2.2、RPC The process is explained in detail

- from RPC From the perspective of , should There are service providers , The producers ; And the caller of the service , The consumer .
- When it comes to consumers , stay RPC During invocation , Use the 1 Step 、 The first 2 Step 、 The first 3 Step 、 The first 4 Step is transparent , Others are used RPC Framework to encapsulate these things . When the application starts calling RPC The way to , Will go to the container to get Bean object , So we should register first Bean Object into the container , We go through Java Dynamic proxy for , Encapsulate the proxy process into the proxy object , Proxy objects implement interfaces , Create instances into containers . Corresponding , When calling the object method of a remote object , It will call the methods in the dynamic proxy , This is the role of the agent layer .
- The proxy object is getting the request method 、 Interface and parameters , Will use the serialization layer , Encapsulate this information into a request message , Then let the communication layer transmit the contents of the message to the server , Then came the producer .
- The corresponding service must have a listener , To listen for requests from other services , Generally, containers are used for message listening , Will call the corresponding Bean Object method , To process the response request . Of course ,RPC The framework will not let every framework in the container be called , So only those who have registered Bean Will be RPC The request of is called to . then , Through the class in the request 、 Method 、 Parameters , Reflection calls the corresponding Bean, After getting the results , Through the serialization layer , Encapsulate the result message , The communication layer of the server feeds back the message to the caller , The caller resolves to the return value , The dynamic proxy class returns the result , End of call .
- such , A complete RPC Call the feedback chain to complete .
2.2.1、 Consumer Design

① Broker layer :
- The consumer will have the corresponding interface , adopt RPC The proxy of the framework to generate an object to Spring In the container . The proxy layer will proxy the interface to generate the object of the interface , This object handles the object passed during the call 、 Method 、 Parameters , Encapsulated through the serialization layer , Call the network layer .
② Serialization layer :
- Serialize the requested parameters into messages ; Deserialize the returned message into an object ;
③ The network layer :
- Communicate the message with the server ; Receive return result .
2.2.2、 Producer design

① Broker layer :
An application provides services , There must be a network monitoring module , Most of this module has open source containers to handle network monitoring ; The service requires registration , Only registered services can be called ; The registered service needs to be launched and called by us to , To deal with it .
② Serialization layer :
It is to deserialize the request and serialize the result accordingly .
③ The network layer :
Receive client message ; Return the serialized results to the client .
2.3、RPC Pattern summary
2.3.1、 Module design

1.Proxy Broker layer
Proxy for object ; Reflection call of object ;RPC Process control .
2.Serialize Serialization layer
Serialize the request and deserialize the result .
3.Invoke Network module
It is mainly used for relevant processing of network communication .
4.Container Container components
This layer is mainly used for the agent layer to listen to network requests .
- Components
① Three elements : A dynamic proxy 、 Serialization and deserialization protocols 、 Network communication protocol
② The network communication protocol can be TCP、UDP, It can also be HTTP 1.x、HTTP 2, There's even the ability to be a custom protocol
2.3.2、RPC The evolution of 
3、 ... and 、REST call
REST Represents the state transition of the presentation layer (representational state transfer), from Roy Fielding In his paper .REST It is used to describe that the client obtains the data of the server through some form , The format of these data resources is usually JSON or XML. meanwhile , The performance or collection of these resources can be modified , With behaviors and relationships can be found through multimedia . in my opinion , A simple understanding is : In the design API when , Use the path to locate the resource , Method definition operation , adopt Content-Type and Accept To negotiate the type of resource .REST There are also restrictions :
- REST It's stateless , There is no persistent session information between requests
- The response needs to be declared cacheable
- REST Focus on consistency , If you use HTTP, It needs to be used as much as possible HTTP Characteristics of , Instead of inventing new conventions
These restrictions allow REST Architecturally API A more stable .
- REST adopt HTTP The general verb method defined by the protocol (GET、PUT、DELETE、POST), With URI Unique identification of network resources , The responder will respond according to the different requirements of the requester , Through stateless communication , Express the resources it requests .
- Satisfy REST Framework of constraints and principles , It's called RESTful framework . It's like URL All are URI( Unified resource identification ) In the same form ,RESTful Is in accordance with REST The expression of the principle .
Four 、REST and RPC difference
- REST It's a design style , Many of its ways of thinking are similar to RPC Is completely conflicting .
- RPC The idea is to map local functions to API, That is to say a API It corresponds to a function, It can be called through some agreed protocol , As for this agreement is Socket、 yes HTTP Or something else doesn't matter ;
- RPC The subjects in are 【 action , Verb 】.
- and REST Otherwise , its URL The subject is 【 resources , Noun 】. And it only supports HTTP agreement , Specified use HTTP
Method Express what you are doing this time , Generally, there are no more than four or five types . - These actions express the only ways to transform resources .
- RPC It means remote procedure call ,HTTP It is an application layer transport protocol . The two are not at the same level , So we usually call RPC For the framework ,HTTP For the agreement , stay RPC You can choose HTTP As a transport protocol .
- RPC, Remote procedure calls can be confusing , I can't feel what this is intuitively . Actually , Let's be simple , Remote procedure call actually describes one thing , How a client calls a server . In this matter , It includes a lot of content , for example : How to deal with serialization and deserialization protocols , What kind of transmission protocol to choose . These are introduced above RPC It has been introduced .
So let's go back to the beginning Feign - Feign yes Spring Cloud Recommended for use in family buckets RPC frame , however Feign It's also used HTTP As a transport layer protocol .
- As described above RPC, And listed some common RPC frame . in general ,RPC and HTTP The two are not at the same level , stay RPC You can choose to use HTTP As its transport layer protocol , In the microservice system , No matter use Feign Or use RestTemplate, The transport layer is based on HTTP Protocol for transmission .
边栏推荐
- 网络安全之安全基线
- To create a thread pool for the rate, start the core thread
- Detailed steps of installing MySQL 5.7 for windows
- 简易计算器微信小程序项目源码
- 产品推广的渠道和策略,化妆品品牌推广方法及步骤
- Day4: the establishment of MySQL database and its simplicity and practicality
- [beauty of software engineering - column notes] 24 | technical debt: continue to make do with it, or overthrow it and start over?
- Multifunctional signal generator based on AD9850
- Smart energy management system solution
- 【OpenCV】-算子(Sobel、Canny、Laplacian)学习
猜你喜欢

Day15 (day16 extension): file contains vulnerability

Day4: SQL server is easy to use
![[beauty of software engineering - column notes] 29 | automated testing: how to kill bugs in the cradle?](/img/e1/8a61f85bf93801d842e78ab4f7edc7.png)
[beauty of software engineering - column notes] 29 | automated testing: how to kill bugs in the cradle?

STM32 serial port garbled

Simplefoc parameter adjustment 2- speed and position control

Day15: the file contains the vulnerability range manual (self use file include range)
![[robomaster] a board receives jy-me01 angle sensor data -- Modbus Protocol & CRC software verification](/img/0e/e5be0fffb154d081c20b09832530d4.png)
[robomaster] a board receives jy-me01 angle sensor data -- Modbus Protocol & CRC software verification

【OpenCV】-算子(Sobel、Canny、Laplacian)学习

A problem encountered in SQL interview

Data warehouse layered design and data synchronization,, 220728,,,,
随机推荐
commonjs导入导出与ES6 Modules导入导出简单介绍及使用
Day5: PHP simple syntax and usage
Week 1 task deep learning and pytorch Foundation
Usage of torch.tensor.to
Lora opens a new era of Internet of things -asr6500s, asr6501/6502, asr6505, asr6601
Collation of ml.net related resources
【OpenCV】-算子(Sobel、Canny、Laplacian)学习
SQL 面试碰到的一个问题
阿里巴巴政委体系-第四章、政委建在连队上
Huawei wireless device configuration uses WDS technology to deploy WLAN services
Simplefoc parameter adjustment 2- speed and position control
Brief introduction and use of commonjs import and export and ES6 modules import and export
Unity shader learning (VI) achieving radar scanning effect
Nrf52832-qfaa Bluetooth wireless chip
torch.Tensor.to的用法
To create a thread pool for the rate, start the core thread
Proteus simulation based on 51 MCU ADC0808
[academic related] why can't many domestic scholars' AI papers be reproduced?
Deep learning (1): prediction of bank customer loss
[beauty of software engineering - column notes] 25 | what methods can improve development efficiency?