在 API Gateway 上部署 Cloud Endpoints API
Cloud Endpoints 是一個 API 管理系統,可協助您保護、監控、分析 API,以及設定 API 配額。Endpoints 會使用可擴充服務 Proxy (ESP) 或可擴充服務 Proxy V2 (ESPv2) 代管 API。
使用 Endpoints 時,您有三種定義 API 的方式:
使用 API Gateway 開發 API
Endpoints 和 API Gateway 都支援使用 OpenAPI 規格 (OpenAPI 2.0 或 OpenAPI 3.x) 說明的 API。如果您使用 OpenAPI 定義 Endpoints API,表示您也可以在 API Gateway 上部署 Endpoints API。
在 API Gateway 上部署 Endpoints OpenAPI 規格
API Gateway 支援的 OpenAPI 規格定義格式和選項,與 Cloud Endpoints for OpenAPI 支援的格式和選項相同。 也就是說,您可以使用 Endpoints OpenAPI 規格中的相同安全性、配額和其他定義,在 API Gateway 中定義 API。
Endpoints 和 API Gateway 的唯一差異在於處理 OpenAPI 規格定義中的 host 屬性方式:
- 在 Endpoints 中,將
host屬性設為部署 ESP 或 ESPv2 時建立的網址主機名稱部分,其中 ESP 或 ESPv2 是用於代管 Endpoints API 的服務。 - 在 API Gateway 中,省略
host或將其設為已部署 API 的 DNS 名稱。API 供應商與 API 使用者共用 OpenAPI 規格時,通常會將其設為 DNS 名稱。不過,API Gateway 不會強制執行host屬性的值。
舉例來說,以下是 Endpoints 用來定義 API 的 OpenAPI 定義部分內容,可存取部署在 Cloud Run functions 上的後端服務:
OpenAPI 2.0
swagger: '2.0' info: title: Cloud Endpoints + GCF description: Sample API on Cloud Endpoints with a Google Cloud Functions backend version: 1.0.0 host: HOST schemes: - https ...
OpenAPI 3.x
openapi: 3.0.4 info: title: Cloud Endpoints + GCF description: Sample API on Cloud Endpoints with a Google Cloud Functions backend version: 1.0.0 servers: - url: https://HOST x-google-endpoint: {} ...
如要進一步瞭解如何為 API 定義建立 OpenAPI 規格,請參閱 OpenAPI 總覽。