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:快速入门

第 2 章 开始使用 gRPC gRPC 入门

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

关于 gRPC 的理论已经讲得够多了;让我们应用在第 1 章中学到的知识,从头开始构建一个真实的 gRPC 应用程序。在本章中,你将使用 Go 和 Java 构建一个简单的 gRPC 服务和一个调用你开发的服务的客户端应用程序。在此过程中,你将学习如何使用协议缓冲区指定 gRPC 服务定义、生成服务器骨架和客户端存根、实现服务的业务逻辑、使用所实现的服务运行 gRPC 服务器,以及通过 gRPC 客户端应用程序调用服务。

让我们使用第 1 章中的在线零售系统,我们需要构建一个负责管理零售店产品的服务。该服务可以远程访问,该服务的消费者可以向系统添加新产品,也可以通过提供产品 ID 从系统中检索产品详细信息。我们将使用 gRPC 对该服务和消费者进行建模。您可以选择自己喜欢的编程语言来实现,但在本章中,我们将同时使用 Go 和 Java 语言来实现这个示例。

备注

您可以在本书的源代码库中试用该示例的 Go 和 Java 实现。

图 2-1 展示了ProductInfo 服务在每次方法调用时的客户端与服务器通信模式。服务器托管的 gRPC 服务提供两种远程方法:addProduct(product)getProduct(productId) 。客户端可以调用其中任何一个远程方法。

client-server interaction of Product Info service
图 2-1. ProductInfo 服务的客户端与服务器交互

让我们创建ProductInfo gRPC 服务的服务定义,开始构建这个示例。

创建服务定义

正如你在第一章中学到的,当你开发一个 gRPC 应用程序时,首先要做的就是定义服务接口,其中包含允许消费者远程调用的方法、调用这些方法时使用的方法参数和消息格式等。所有这些服务定义都会被记录为协议缓冲区的定义,也就是 gRPC 中使用的接口定义语言(IDL)。

备注

我们将在第 3 章进一步深入探讨不同消息传递模式的服务定义技术。我们还将在第 4 章中介绍协议缓冲区和 gRPC 实现细节。

一旦确定了服务的业务功能,就可以定义服务接口来满足业务需求。在我们的示例中,我们可以确定ProductInfo 服务中的两个远程方法(addProduct(product)getProduct(productId) ),以及这两个方法接受和返回的两种消息类型(ProductProductID )。

下一步是将这些服务定义指定为协议缓冲区定义。通过协议缓冲区,我们可以定义服务和消息类型。服务由方法组成,每个方法由其类型、输入和输出参数定义。消息由字段组成,每个字段由其类型和唯一索引值定义。让我们深入了解定义消息结构的细节。

定义信息

消息是客户端和服务之间交换的数据结构。如图 2-1 所示,我们的ProductInfo 用例有两种消息类型。一种是产品信息 (Product),在向系统添加新产品时需要使用,在检索特定产品时会返回。另一种是产品的唯一标识 (ProductID),从系统中检索特定产品时需要使用,添加新产品时也会返回:

ProductID

ProductID 是产品的唯一标识符,可以是字符串值。我们既可以定义自己的包含字符串字段的消息类型,也可以使用协议缓冲库提供的著名消息类型 ...

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