Skip to Content
gRPC:快速入门
book

gRPC:快速入门

by Kasun Indrasiri, Danesh Kuruppu
May 2025
Intermediate to advanced
204 pages
2h 24m
Chinese
O'Reilly Media, Inc.
Content preview from gRPC:快速入门

第 8 章 gRPC 生态系统

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

在本章中,我们将探讨一些不属于核心 gRPC 实现的项目,但它们在构建和运行实际用例中的 gRPC 应用程序时可能非常有用。这些项目是 gRPC Ecosystem 父项目的一部分,这里提到的技术都不是运行 gRPC 应用程序的必备技术。如果您有与某个项目类似的需求,请探索并评估这些技术。

让我们从 gRPC 网关开始讨论。

gRPC 网关

gRPC 网关插件使协议缓冲编译器能够读取 gRPC 服务定义并生成反向代理服务器,从而将 RESTful JSON API 转换为 gRPC。该插件专为 Go 编写,支持从 gRPC 和 HTTP 客户端应用程序调用 gRPC 服务。图 8-1 展示了它如何提供以 gRPC 和 RESTful 两种方式调用 gRPC 服务的能力。

如图所示,我们有一个ProductInfo 服务合同,并使用该合同构建了一个名为ProductInfoService 的 gRPC 服务。之前我们创建了一个 gRPC 客户端来与该 gRPC 服务对话。但在这里,我们将不构建 gRPC 客户端,而是构建一个反向代理服务,为 gRPC 服务中的每个远程方法提供 RESTful API,并接受来自 REST 客户端的 HTTP 请求。一旦收到 HTTP 请求,它就会将请求转化为 gRPC 消息,并调用后端服务中的远程方法。后端服务器的响应信息会再次转换成 HTTP 响应并回复客户端。

gRPC gateway
图 8-1. gRPC 网关

要为服务定义生成反向代理服务,我们首先需要通过更新服务定义将 gRPC 方法映射到 HTTP 资源。让我们获取与创建时相同的ProductInfo 服务定义,添加映射条目。例 8-1显示了更新后的协议缓冲区定义。

例 8-1. 更新 ProductInfo 服务的协议缓冲区定义
syntax = "proto3";

import "google/protobuf/wrappers.proto";
import "google/api/annotations.proto"; 1

package ecommerce;

service ProductInfo {
   rpc addProduct(Product) returns (google.protobuf.StringValue) {
       option (google.api.http) = { 2
           post: "/v1/product"
           body: "*"
       };
   }
   rpc getProduct(google.protobuf.StringValue) returns (Product) {
        option (google.api.http ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

软件架构师速成指南

软件架构师速成指南

Gregor Hohpe
企业级Java开发中的应用人工智能 (Chinese Edition)

企业级Java开发中的应用人工智能 (Chinese Edition)

Alex Soto Bueno, Markus Eisele, Natale Vinto
高性能MySQL:第4版

高性能MySQL:第4版

Silvia Botros, Jeremy Tinley

Publisher Resources

ISBN: 9798341658004