在 API Gateway 上部署 Cloud Endpoints API
Cloud Endpoints 是 API 管理系統,可協助您保護、監控、分析 API,並設定配額。Endpoints 會使用可擴充服務 Proxy (ESP) 或可擴充服務 Proxy V2 Beta (ESPv2 Beta) 來代管 API。
使用 Endpoints 時,您有三種方法可定義 API:
使用 API Gateway 開發 API
API Gateway 支援使用 OpenAPI 規範 2.0 版說明的 API。也就是說,您可以透過 API Gateway 的 Endpoints 部署 OpenAPI 規格。
在 API Gateway 上部署 Endpoints OpenAPI 規格
API Gateway 支援與 OpenAPI 版 Cloud Endpoints 相同的 OpenAPI 規格定義格式和選項。也就是說,您可以使用 Endpoints OpenAPI 規格中的相同安全性、配額和其他定義,在 API Gateway 中定義 API。
Endpoints 和 API Gateway 唯一的差異在於如何處理 OpenAPI 規格定義中的 host
屬性:
- 在 Endpoints 中,將
host
屬性設為部署 ESP 時建立的網址的主機名稱部分,其中 ESP 是用來代管 Endpoints API 的服務。 - 在 API Gateway 中,請省略
host
或將其設為已部署 API 的 DNS 名稱。當 API 供應商與 API 使用者分享 OpenAPI 規格時,通常會將其設為 DNS 名稱。不過,API Gateway 不會強制執行host
屬性的值。
舉例來說,以下是 Endpoints 用來定義 API 的 OpenAPI 規範部分,可用於存取在 Cloud Run 函式上部署的後端服務:
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: ESP_HOST schemes: - https produces: - application/json paths: /hello: ...
針對 Endpoints,您必須:
部署 ESP,系統會建立您用於存取部署至 ESP 的 API 的網址。例如:
https://gateway-12345-uc.a.run.app
編輯 OpenAPI 規格,將
host
屬性設為 ESP 網址的網域名稱。例如host: gateway-12345-uc.a.run.app
將 OpenAPI 規格部署至 ESP。
使用 ESP 網址存取 API。在上述 OpenAPI 規格中,
paths
元素已設為/hello
,因此您可以使用下列網址存取此 API:https://gateway-12345-uc.a.run.app/hello
如您所見,如要設定 Endpoints 的 host
屬性值,您必須先部署 ESP。
如要使用這個 API 定義為 API Gateway 建立 API 設定,您可以完全刪除 host
屬性,也可以將其保留在檔案中。API Gateway 不會強制執行 host
屬性的值。
部署閘道的網址,也就是用戶端用來存取 API 的網址,會在您建立閘道時定義,格式如下:
https://GATEWAY_ID-HASH.REGION_CODE.gateway.dev
其中 GATEWAY_ID 是閘道名稱、HASH 是部署 API 時產生的不重複雜湊碼,而 REGION_CODE 則是部署閘道所在的Google Cloud 區域代碼。
將 API 部署至閘道執行個體後,用來存取 /hello
的網址如下:
https://my-gateway-a12bcd345e67f89g0h.uc.gateway.dev/hello
如要進一步瞭解如何部署 API,請參閱「API 閘道部署模型」。
如要進一步瞭解如何為 API 定義建立 OpenAPI 規格,請參閱 OpenAPI 總覽。