当前位置:网站首页>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
边栏推荐
- Acwing 2022 daily question
- HUAWEI nova 10系列发布 华为应用市场筑牢应用安全防火墙
- 淘宝商品评价api接口(item_review-获得淘宝商品评论API接口),天猫商品评论API接口
- Rotary transformer string judgment
- 迷失在Mysql的锁世界
- How to implement Devops with automatic tools
- El tree combined with El table, tree adding and modifying operations
- 哈希表(Hash Tabel)
- gtest从一无所知到熟练使用(2)什么是测试夹具/装置(test fixture)
- 复数在数论、几何中的用途 - 曹则贤
猜你喜欢
close系统调用分析-性能优化
GTEST from ignorance to proficiency (3) what are test suite and test case
Zhiyang innovation signed a cooperation agreement with Huawei to jointly promote the sustainable development of shengteng AI industry
B站大量虚拟主播被集体强制退款:收入蒸发,还倒欠B站;乔布斯被追授美国总统自由勋章;Grafana 9 发布|极客头条
[advanced C language] array & pointer & array written test questions
From repvgg to mobileone, including mobileone code
Redis 排查大 key 的3种方法,优化必备
Use blocconsumer to build responsive components and monitor status at the same time
迷失在Mysql的锁世界
力扣98:验证二叉搜索树
随机推荐
Monitor the shuttle return button
案例分享|金融业数据运营运维一体化建设
玩转gRPC—深入概念与原理
淘宝商品评价api接口(item_review-获得淘宝商品评论API接口),天猫商品评论API接口
NAACL-22 | 在基于Prompt的文本生成任务上引入迁移学习的设置
TCP protocol three times handshake process
并列图的画法,多排多列
历史最全混合专家(MOE)模型相关精选论文、系统、应用整理分享
What is business intelligence (BI), just look at this article is enough
Keep on fighting! The city chain technology digital summit was grandly held in Chongqing
《命令行上的数据科学第二版》校对活动重新启动
服装企业为什么要谈信息化?
close系统调用分析-性能优化
Redis 排查大 key 的3种方法,优化必备
并发网络模块化 读书笔记转
Analysis of maker education technology in the Internet Era
[early knowledge of activities] list of recent activities of livevideostack
Rotary transformer string judgment
删库不必跑路!详解 MySQL 数据恢复
广电五舟与华为签署合作协议,共同推进昇腾AI产业持续发展