如果您使用 Pub/Sub 发布的数据量很大的消息,则可以在发布方客户端发出发布请求之前,使用 gRPC 压缩数据,以节省网络开销。适用于 gRPC 的 Pub/Sub 压缩使用 Gzip 算法。
本文档介绍了如何压缩发布到主题的消息。
关于压缩邮件
压缩比 与使用 gRPC 客户端压缩功能时的 并取决于以下因素:
数据量。压缩比在 载荷从几百字节增加到几千字节。批次 发布请求中包含的数据量决定了 每个发布请求我们建议您同时启用批量设置和 gRPC 压缩,以获得最佳效果。
数据类型。基于文本的数据(例如 JSON 或 XML)的压缩率更高 与图片等二进制数据相比。
如果您的发布者客户端在 Google Cloud 上,您可以使用
已发送字节数 (instance/network/sent_bytes_count
) 指标
衡量发布吞吐量(以字节为单位)。如果您的发布商客户端位于其他应用中,您必须使用特定于客户端的工具进行衡量。
本部分中的代码示例展示了一个示例 Java 客户端库代码段 (还包括 gRPC 压缩)
准备工作
在配置发布工作流程之前,请确保您已完成以下操作 任务:
所需的角色
如需获得压缩消息所需的权限,请让您的管理员为您授予主题的 Pub/Sub Publisher (roles/pubsub.publisher
) IAM 角色。
如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
您需要额外的权限才能创建或更新主题和订阅。
压缩消息
C++
在尝试此示例之前,请按照《快速入门:使用客户端库》中的 C++ 设置说明进行操作。如需了解详情,请参阅 Pub/Sub C++ API 参考文档。
Java
在尝试此示例之前,请按照《快速入门:使用客户端库》中的 Java 设置说明进行操作。 如需了解详情,请参阅 Pub/Sub Java API 参考文档。
后续步骤
如需了解如何配置高级发布选项,请参阅以下内容: