使用入门:Compute Engine 上使用 ESP 的 Cloud Endpoints

本教程介绍了如何在 Compute Engine 上配置和部署在预建 Docker 容器中运行的示例 API 和 Extensible Service Proxy (ESP)。示例 API 是使用 OpenAPI 规范描述的。本教程还介绍了如何创建 API 密钥,以及如何在向 API 发送请求时使用此密钥。

如需大致了解 Cloud Endpoints,请参阅关于 Endpoints 和 Endpoints 架构。

目标

学习本教程时,请使用以下概要任务列表。为确保请求成功发送到 API,您必须完成所有任务。

  1. 设置 Google Cloud 项目。请参阅准备工作。
  2. 创建 Compute Engine 虚拟机实例。请参阅创建 Compute Engine 实例。
  3. 下载示例代码。请参阅获取示例代码。
  4. 配置 openapi.yaml 文件,该文件用于配置 Endpoints。请参阅配置 Endpoints。
  5. 部署 Endpoints 配置以创建 Endpoints 服务。请参阅部署 Endpoints 配置。
  6. 在 Compute Engine 虚拟机上部署 API 和 ESP。请参阅部署 API 后端。
  7. 使用 IP 地址向 API 发送请求。请参阅使用 IP 地址发送请求。
  8. 为示例 API 配置 DNS 记录。请参阅为 Endpoints 配置 DNS。
  9. 使用完全限定的域名向 API 发送请求。请参阅使用 FQDN 发送请求。
  10. 跟踪 API 活动。请参阅跟踪 API 活动。
  11. 避免您的 Google Cloud 账号产生费用。请参阅清理。

费用

在本文档中,您将使用 Google Cloud的以下收费组件:

如需根据您的预计使用情况来估算费用,请使用价格计算器。

新 Google Cloud 用户可能有资格申请免费试用。

完成本文档中描述的任务后,您可以通过删除所创建的资源来避免继续计费。如需了解详情,请参阅清理。

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud新手,请 创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  5. Verify that billing is enabled for your Google Cloud project.

  6. 记下项目 ID,您稍后需要用到。
  7. 您需要一个应用来向示例 API 发送请求。

    • Linux 和 macOS 用户:本教程提供了一个使用 curl 的示例,它通常已预装在您的操作系统中。如果您没有 curl,可以从 curl 版本和下载页面中下载。
    • Windows 用户:本教程提供了一个使用 Invoke-WebRequest 的示例,PowerShell 3.0 及更高版本支持该命令。
  8. 下载 Google Cloud CLI。
  9. 更新 gcloud CLI 并安装 Endpoints 组件。
    gcloud components update
  10. 确保 Google Cloud CLI (gcloud) 有权访问您在 Google Cloud上的数据和服务:
    gcloud auth login
    在浏览器打开的新标签页中,选择一个账号。
  11. 将默认项目设置为您的项目 ID。