gRPC
gRPC is a modern, open source remote procedure call (RPC) framework
- 簡單來講就是,server 端跟 client 端都是固定的兩方,然後透過資料格式解析
- 雙方都特定處理某件事情,簡單來講就是類似 socket server,但並非是
- 有點類似透過 protocol buffer 在本地端直接調用 func 並得到回傳值
- 基於 HTTP/2 協議標準設計, 所以也是有 Request 與 Response
- HTTP/2 是一個二進位協議, 意味著可讀性是 0
- 基於 ProtoBuf(Protocol Buffers) 序列化協議開發
- 實踐方法有四種模式
- simple RPC
- Client-side streaming RPC
- Server-side streaming RPC
- Bidirectional streaming RPC
為了解決?
- 低延遲, 高擴充性, 分散式系統
- 可開發移動端的 clients, 與 cloud server 進行溝通
- 準確、高校且獨立的 protocol
- 分層設計以實現擴展,例如。 身份驗證,負載平衡,日誌記錄和監視等
如何使用與開發
- 說明書 : https://grpc.io/docs/quickstart/
- Github org page : https://github.com/grpc
- Document : https://grpc.io/docs/
- 中文 : https://doc.oschina.net/grpc?t=58008
- Example : https://github.com/smallnest/grpc-examples
支援語言
C++, Java (incl. support for Android), Objective-C (for iOS), Python, Ruby, Go, C#, Node.js
可以應用在 web 端的服務嗎?
- 透過 gRPC-Web : https://github.com/grpc/grpc-web
- gRPC-Web 提供 Javascript library 讓瀏覽器端可以存取使用 gRPC 的服務
可以跟 JSON, Protobuf, Thrift, XML 一起應用嗎?
Yes, 最初就支援 Protobuf, 並且可以延伸支援其他內容格式
在移動端的開發的優勢?
- 節省頻寬, 使用更少的 TCP 連線
- 節省 CPU 使用
- 省電
gRPC 為何比其他也是建立在 HTTP/2 架構上的 binary blob 來得好?
- gRPC 在跨平台上比起一般 http lib 的表現來的優秀, 例如
- interaction with flow-control at the application layer
- cascading call-cancellation
- load balancing & failover
gRPC 與 REST 的比較
- 允許 full-duplex(全雙工) streaming.
- 基於效能,使用靜態路徑,解析路徑中調用的參數。REST 中 query parameters 與 payload body 會增加延遲跟複雜性
- 定義了一系列的 errors, 相信比 HTTP status codes 更直觀適用於 API 使用情境。