当前位置:网站首页>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 .
边栏推荐
- 华为无线设备配置利用WDS技术部署WLAN业务
- Unity shader learning (VI) achieving radar scanning effect
- Ga-rpn: recommended area network for guiding anchors
- A problem encountered in SQL interview
- [beauty of software engineering - column notes] "one question and one answer" issue 2 | 30 common software development problem-solving strategies
- Day6: using PHP to write landing pages
- Lora opens a new era of Internet of things -asr6500s, asr6501/6502, asr6505, asr6601
- STM32 printf problem summary semihosting microlib understanding
- Inclination sensor is used for long-term monitoring of communication tower and high-voltage tower
- [noi simulation] computational geometry (convex hull, violence, and search set)
猜你喜欢

Inclination monitoring solution of Internet of things

Node: file write data (readfile, WriteFile), two modes: overwrite and increment

集群使用规范

Four pin OLED display based on stm32

Day13: file upload vulnerability

Implementation of support vector machine with ml11 sklearn

Simple calculator wechat applet project source code

Solve the problem of MSVC2017 compiler with yellow exclamation mark in kits component of QT

Day4: SQL server is easy to use

DC motor control system based on DAC0832
随机推荐
Hal library learning notes - 8 concept of serial communication
HC-SR04超声波测距模块使用方法和例程(STM32)
commonjs导入导出与ES6 Modules导入导出简单介绍及使用
[beauty of software engineering - column notes] 23 | Architect: programmers who don't want to be architects are not good programmers
Qt/pyqt window type and window flag
Component transfer participation lifecycle
Nrf52832-qfaa Bluetooth wireless chip
Si12t and si14t low power capacitive touch chips
Charging pile charging technology new energy charging pile development
Simplefoc parameter adjustment 1-torque control
[beauty of software engineering - column notes] 26 | continuous delivery: how to release new versions to the production environment at any time?
Intelligent shelf safety monitoring system
Windows 安装 MySQL 5.7详细步骤
[academic related] why can't many domestic scholars' AI papers be reproduced?
TCP——滑动窗口
Random lottery turntable wechat applet project source code
Phy6252 is an ultra-low power Bluetooth wireless communication chip for the Internet of things
随机抽奖转盘微信小程序项目源码
【Transformer】ATS: Adaptive Token Sampling For Efficient Vision Transformers
[beauty of software engineering - column notes] 21 | architecture design: can ordinary programmers also implement complex systems?