当前位置:网站首页>Play with grpc - go deep into concepts and principles
Play with grpc - go deep into concepts and principles
2022-07-04 22:08:00 【InfoQ】
- 《 Get along well with gRPC—Go Use gRPC Communication practice 》
- 《 Get along well with gRPC— Communication between different programming languages 》
- 《 I'll take you to understand HTTP and RPC The similarities and differences of the agreement 》
- 《 from 1 Start , Expand Go Language backend business system RPC function 》
1 Use gRPC Basic architecture

- Service: Services provided
- Client:gRPC client
- gRPC Server:gRPC Server interface
- gRPC Stub:gRPC Client interface
- Proto Request/Proto Response(s): Intermediate document ( Code / agreement )
2 Protocol Buffers
2.1 What is? Protocol Buffers?
- Support for multiple programming languages
- Serialized data is small
- Fast deserialization
- Serialization and deserialization code are automatically generated
2.2 Protocol Buffers and gRPC What is the relationship ?
2.3 Protocol Buffers Basic grammar
.protomessage Person {
string name = 1;
int32 id = 2;
bool has_ponycopter = 3;
}
protocname()set_name()// The greeter service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
protoc3 gRPC Four service delivery methods
3.1 Unary RPC
rpc SayHello(HelloRequest) returns (HelloResponse);
3.2 Server streaming RPC
rpc LotsOfReplies(HelloRequest) returns (stream HelloResponse);
3.3 Client streaming RPC
rpc LotsOfGreetings(stream HelloRequest) returns (HelloResponse);
3.4 Bidirectional streaming RPC
rpc BidiHello(stream HelloRequest) returns (stream HelloResponse)
4 gRPC Life cycle of

4.1 Service delivery
4.2 Closing date / Overtime
DEADLINE_EXCEEDED4.3 RPC End
4.4 Cancel RPC
5 gRPC Communication principle
5.1 HTTP2
- HTTP/2 The specification of 2015 year 5 Published in , It aims to solve some scalability problems of its predecessor , Improved in many ways HTTP/1.1 The design of the , The most important thing is to provide semantic mapping on connections .
- establish HTTP The cost of connection is great . You must establish TCP Connect 、 Use TLS Protect the connection 、 Exchange headers and settings .HTTP/1.1 By treating connections as long-term 、 Reusable objects to simplify this process .HTTP/1.1 The connection remains idle , So that new requests can be sent to the same destination through existing idle connections . Although connection reuse alleviates this problem , But a connection can only process one request at a time —— They are 1:1 Coupled . If you want to send a big message , A new request must either wait for it to complete ( Lead to Queue blocking ), Or pay the price of starting another connection more often .
- HTTP/2 By providing a semantic layer above the connection :flow, This further extends the concept of persistent connections . A stream can be thought of as a series of semantically connected messages , be calledframe. The flow may be short , For example, a unary flow requesting user status ( stay HTTP/1.1 in , This may be equivalent to
GET /users/1234/status). As the frequency increases , It has a long life . The recipient may establish a long-term flow , So as to receive user status messages continuously in real time , Not to /users/1234/status The endpoint makes a separate request .The main advantage of flow is connection concurrency , That is, the ability to interleave messages on a single connection .
- flow control
- However , Concurrent flows contain some subtle pitfalls . Consider the following : Two streams on the same connection A and B. flow A Receive a lot of data , Far more than the data it can process in a short time . Final , The recipient's buffer is filled ,TCP The receiving window limits the sender . This is for TCP It's quite standard behavior , But this situation is unfavorable for flow , Because neither stream will receive more data . Ideally , flow B It should be free from flow A The impact of slow processing .
- HTTP/2 By providingflow controlThe mechanism solves this problem as part of the flow specification . Flow control is used to limit each flow ( And each connection ) Amount of unfinished data . It operates as a credit system , The receiver assigns a certain “ The budget ”, The sender “ cost ” This budget . More specifically , The receiver allocates some buffer size (“ The budget ”), The sender fills (“ cost ”) buffer . The receiver uses WINDOW_UPDATE The frame notifies the sender of the available additional buffer . When the receiver stops broadcasting additional buffers , The sender must be in the buffer ( Its “ The budget ”) Stop sending messages when exhausted .
- Use flow control , Concurrent streams can guarantee independent buffer allocation .Plus polling request sending , All sizes 、 The flow of processing speed and duration can be carried out on a single connectionMultiplexing, There is no need to care about cross flow issues .
- Smarter agents
- HTTP/2 The concurrency attribute of allows agents to have higher performance . for example , Consider a method of accepting and forwarding peak traffic HTTP/1.1 Load balancer : When there is a spike , The agent will start more connections to handle the load or queue the request . The former —— new connection —— Usually the first choice is ( In a way ); The disadvantage of these new connections is not only the time waiting for system calls and sockets , Still in happen TCP The connection time is not fully utilized during slow startup .
- by comparison , Consider a configuration for multiplexing each connection 100 A stream of HTTP/2 agent . Some peak requests will still cause new connections to be started , But with HTTP/1.1 The corresponding number of connections is only 1/100 A connection . More generally : Ifn individualHTTP/1.1 The request is sent to a proxy , ben individualHTTP/1.1 Request must go out ; Each request is a meaningful data request / Payload , The request is 1:1 The connection of . contrary , Use HTTP/2 Sent to the agentn Request needs n individualflow, but Unwantedn individualConnect !
5.2 gRPC And HTTP2

6 summary
边栏推荐
- Enabling digital economy Fuxin software attends the BRICs high level Forum on Sustainable Development
- VS2019 C# release下断点调试
- 【Acwing】第58场周赛 题解
- 面试题 01.01. 判定字符是否唯一
- TCP协议三次握手过程
- sqlserver对数据进行加密、解密
- 面试题 01.08. 零矩阵
- vim 从嫌弃到依赖(23)——最后的闲扯
- 哈希表(Hash Tabel)
- GTEST from ignorance to proficiency (3) what are test suite and test case
猜你喜欢

From repvgg to mobileone, including mobileone code

QT - double buffer plot

Redis 排查大 key 的3种方法,优化必备

el-tree结合el-table,树形添加修改操作

B站大量虚拟主播被集体强制退款:收入蒸发,还倒欠B站;乔布斯被追授美国总统自由勋章;Grafana 9 发布|极客头条

Shutter textfield example

智洋创新与华为签署合作协议,共同推进昇腾AI产业持续发展

使用 BlocConsumer 同时构建响应式组件和监听状态

El tree combined with El table, tree adding and modifying operations

Analysis of maker education technology in the Internet Era
随机推荐
【Acwing】第58场周赛 题解
Why do you have to be familiar with industry and enterprise business when doing Bi development?
GTEST from ignorance to proficient use (2) what is test fixture
QT—绘制其他问题
挖财学院股票开户安全吗?开户只能在挖财开户嘛?
# 2156. Find the substring of the given hash value - post order traversal
bizchart+slider实现分组柱状图
sqlserver对数据进行加密、解密
PMO:比较25种分子优化方法的样本效率
What is business intelligence (BI), just look at this article is enough
哈希表(Hash Tabel)
vim 从嫌弃到依赖(23)——最后的闲扯
Representation of confidence interval
Bookmark
Use blocconsumer to build responsive components and monitor status at the same time
开源之夏专访|Apache IoTDB社区 新晋Committer谢其骏
历史最全混合专家(MOE)模型相关精选论文、系统、应用整理分享
TCP shakes hands three times and waves four times. Do you really understand?
股票开户流程是什么?使用同花顺手机炒股软件安全吗?
From repvgg to mobileone, including mobileone code