当前位置:网站首页>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
.proto
message Person {
string name = 1;
int32 id = 2;
bool has_ponycopter = 3;
}
protoc
name()
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;
}
protoc
3 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_EXCEEDED
4.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
边栏推荐
- 更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
- 网上开户哪家证券公司佣金最低,我要开户,网上开户安全吗
- ACM Multimedia 2022 | 视觉语言预训练模型中社会偏见的反事实衡量和消除
- 服务线上治理
- MySQL存储数据加密
- [early knowledge of activities] list of recent activities of livevideostack
- HDU - 1078 fatmouse and cheese (memory search DP)
- Machine learning notes mutual information
- Zhiyang innovation signed a cooperation agreement with Huawei to jointly promote the sustainable development of shengteng AI industry
- 玩转gRPC—深入概念与原理
猜你喜欢
PMO:比较25种分子优化方法的样本效率
NAACL-22 | 在基于Prompt的文本生成任务上引入迁移学习的设置
如何借助自动化工具落地DevOps
[optimtool.unconstrained] unconstrained optimization toolbox
KDD2022 | 什么特征进行交互才是有效的?
Visual task scheduling & drag and drop | scalph data integration based on Apache seatunnel
Master the use of auto analyze in data warehouse
广电五舟与华为签署合作协议,共同推进昇腾AI产业持续发展
Enlightenment of maker thinking in Higher Education
开源之夏专访|Apache IoTDB社区 新晋Committer谢其骏
随机推荐
Nat. Commun.| 机器学习对可突变的治疗性抗体的亲和力和特异性进行共同优化
The use of complex numbers in number theory and geometry - Cao Zexian
Shutter WebView example
Visual task scheduling & drag and drop | scalph data integration based on Apache seatunnel
HDU - 1078 FatMouse and Cheese(记忆化搜索DP)
El tree combined with El table, tree adding and modifying operations
[optimtool.unconstrained] unconstrained optimization toolbox
MySQL存储数据加密
Use blocconsumer to build responsive components and monitor status at the same time
Solve the problem of data disorder caused by slow asynchronous interface
Enlightenment of maker thinking in Higher Education
MongoDB中的索引操作总结
i. Mx6ull driver development | 24 - platform based driver model lights LED
el-tree结合el-table,树形添加修改操作
GTEST from ignorance to proficient use (2) what is test fixture
# 2156. 查找给定哈希值的子串-后序遍历
置信区间的画法
How to remove the black dot in front of the title in word document
删库不必跑路!详解 MySQL 数据恢复
1807. 替换字符串中的括号内容