04-23.eri-test gRPC是否比REST更好? 在哪里使用?

本文探讨了gRPC与REST两种通信协议的区别,包括速度、数据格式、API定义、代码生成等方面。gRPC在微服务、实时通信、移动应用等场景下优势明显,但浏览器支持上REST更胜一筹。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

\n

Welcome back, every one! In this lecture, we will discover some use cases of gRPC and how does it compare to REST.

\n\n

Here\'s the link to the full gRPC course playlist on Youtube
\nGitlab repository: pcbook-go and pcbook-java

\n\n

\n\n

\n\n

\n \n \n Types of gRPC\n

\n\n

There are 4 types of gRPC:

\n\n

The simplest one is Unary, where the client sends 1 single request message and the server replies with 1 single response. This looks somewhat similar to the normal HTTP REST API.

\n\n

Then we have client-streaming. In this scenario, the client will send a stream of multiple messages, and it expects the server to send back only 1 single response.

\n\n

4 types of gRPC

\n\n

Similarly, we have server-streaming, where the client sends only 1 request message, and the server replies with a stream of multiple respnoses.

\n\n

And finally, the bidirectional (or bidi) streaming. This one is the most complex because client and server will keep sending and receiving multiple messages in parallel and with arbitrary order. It\xe2\x80\x99s very flexible and no blocking, which means no sides need to wait for the response before sending the next messages.

\n\n

That\xe2\x80\x99s a very high-level overview of the 4 different ways of communication in gRPC. We will come back to this later on the hands-on lecture, where we will implement each and everyone of them to get much deeper understanding.

\n\n

\n \n \n gRPC vs REST\n

\n\n

Now, let\xe2\x80\x99s do a quick comparison of gRPC and REST to see their differences.

\n\n

First, gRPC uses HTTP/2 which is, as you know, much faster than HTTP/1.1 used in REST by default. Note that today we can enable HTTP/2 in REST as well, but normally it often goes with HTTP/1.1. You can read more about how to enable HTTP/2 for REST in these articles:

\n\n \n\n

Second, gRPC uses Protocol buffer to serialize payload data, which is binary and smaller, while REST uses JSON, which is text and larger.

\n\n

The API contract in gRPC is strict, and required to be clearly defined in the proto file. While in REST, it\xe2\x80\x99s often loose and optional. We can define it via OpenAPI if we want, but it\xe2\x80\x99s not mandatory.

\n\n

Code generation is built-in in gRPC with the help of protocol buffer compiler. While in REST, we must use third-party tool like OpenAPI and Swagger.

\n\n

gRPC vs REST

\n\n

Both gRPC and REST communications are secured with TLS/SSL.

\n\n

Streaming is bidirectional in gRPC, while only 1 way request from client to server in REST.

\n\n

So gRPC is better than REST for most of the things that we\xe2\x80\x99ve mentioned so far. However, there\xe2\x80\x99s one thing that REST is still better,

\n\n

That is browser support. While REST is fully supported by all browsers, the support for gRPC is limited and required gRPC-web with a proxy layer to convert between HTTP/1 and HTTP/2.

\n\n

\n \n \n Where to use gRPC?\n

\n\n

So gRPC has a lot of strength, but it also has its own weaknesses. So where and when should we use gRPC in order to take full advantage of it?

\n\n

As you might have guessed, microservices is where gRPC really shines, since it enables low-latency and high-throughput communication, as well as strong API contracts.

\n\n

gRPC is also suitable for polyglot environments, because it provides code generations out-of-the-box for many programming languages.

\n\n

Where to use gRPC

\n\n

Point-to-point real-time communication is also a good place for gRPC, since it has excellent support for bidirectional streaming.

\n\n

And finally, gRPC is a great choice for a network-constrained environment such as mobile application (android/ios), because of its light-weight message format.

\n\n

Alright, so now you\xe2\x80\x99ve finished all the theory lectures of the gRPC course. Congratulations! I hope you enjoy it and I will see you in the hands-on lectures.

\n\n\n
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值