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

第 7 章 在生产中运行 gRPC 在生产中运行 gRPC

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

在前几章中,我们重点讨论了设计和开发基于 gRPC 的应用程序的各个方面。现在,是时候深入了解在生产中运行 gRPC 应用程序的细节了。在本章中,我们将讨论如何为 gRPC 服务和客户端开发单元测试或集成测试,以及如何将它们与持续集成工具集成。然后,我们将进入 gRPC 应用程序的持续部署,在此我们将探索虚拟机(VM)、Docker 和 Kubernetes 上的一些部署模式。最后,要在生产环境中运行 gRPC 应用程序,您需要有一个可靠的可观察性平台。在这里,我们将讨论用于 gRPC 应用程序的不同可观察性工具,并探讨 gRPC 应用程序的故障排除和调试技术。 让我们从测试这些应用程序开始讨论。

测试 gRPC 应用程序

您开发的任何软件应用程序(包括 gRPC 应用程序)都需要进行相关的单元测试。由于 gRPC 应用程序总是与网络交互,因此测试也应涵盖服务器和客户端 gRPC 应用程序的网络 RPC 方面。我们将从测试 gRPC 服务器开始。

测试 gRPC 服务器

gRPC 服务测试通常使用 gRPC 客户应用程序作为测试用例的一部分。服务器端测试包括用所需的 gRPC 服务启动一个 gRPC 服务器,然后使用你实现测试用例的客户端应用程序连接到服务器。 让我们来看看为我们的ProductInfo 服务的 Go 实现编写的测试用例示例。在 Go 中,gRPC 测试用例的实现应使用testing 包作为 Go 的通用测试用例来实现(见例 7-1)。

例 7-1. 使用 Go 进行 gRPC 服务器端测试
func TestServer_AddProduct(t *testing.T) { 1
	grpcServer := initGRPCServerHTTP2() 2
	conn, err := grpc.Dial(address, grpc.WithInsecure()) 3
	if err != nil {

           grpcServer.Stop()
           t.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c := pb.NewProductInfoClient(conn)

	name := "Sumsung S10"
	description := "Samsung Galaxy S10 is the latest smart phone, launched in
	February 2019"
	price := float32(700.0)
	ctx, cancel := context.WithTimeout(
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