部署由 Cloud Endpoints 管理的 API
本快速入門導覽課程會逐步引導您部署 Cloud Endpoints 管理的 API 範例。程式碼範例包括:
- 可透過查詢機場的 IATA 代碼 (三個字母) 而找到機場名稱的 REST API。
- 可將 API 設定上傳至 Endpoints 的指令碼。
- 可部署託管 API 範例的 App Engine 彈性環境後端的指令碼。
傳送要求給 API 範例後,您可以在Google Cloud 主控台中查看 Endpoints 活動圖表和 Google Cloud Observability 記錄。這些工具可讓您監控 API 並深入分析各 API 的使用情形。
本快速入門導覽課程使用指令碼來簡化設定步驟,以便您快速瞭解活動圖表和記錄的實務應用。要瞭解如何設定和部署 API 範例,請選擇其中一個 API 架構的教學課程:
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
必要的角色
如要取得部署及管理 API 所需的權限,請要求系統管理員在專案中授予您下列 IAM 角色:
-
部署 Endpoints 設定:
Service Management 管理員 (
roles/servicemanagement.admin) -
啟用必要服務:
服務使用情形管理員 (
roles/serviceusage.serviceUsageAdmin) -
建立 App Engine 應用程式:
App Engine 建立者 (
roles/appengine.appCreator) -
將範例 API 部署至 App Engine:
- App Engine 部署者 (
roles/appengine.deployer) - Cloud Build 編輯者 (
roles/cloudbuild.builds.editor) - Storage 物件使用者 (
roles/storage.objectUser) - 服務帳戶使用者 (
roles/iam.serviceAccountUser)
- App Engine 部署者 (
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
啟動 Cloud Shell
在 Google Cloud 控制台中,確認您位於要用於 API 範例的專案內。
開啟 Cloud Shell。
系統會在控制台底部的新頁框中開啟 Cloud Shell 工作階段,並顯示指令列提示。Google Cloud 工作階段可能需要幾秒鐘的時間才能完成初始化。

如果您是使用現有的專案,請確認所有已安裝的
gcloud元件皆已安裝最新版本:gcloud components update
取得程式碼範例
- 在 Cloud Shell 中,輸入下列指令以取得 API 範例和指令碼:
git clone https://github.com/GoogleCloudPlatform/endpoints-quickstart
- 變更為包含程式碼範例的目錄:
cd endpoints-quickstart
部署端點配置
如要將 REST API 發佈至 Endpoints,您需要說明該 API 的 OpenAPI 設定檔。API 範例隨附一個名為 openapi.yaml 的預先設定 OpenAPI 檔案。
Endpoints 使用 Service Management 這個Google Cloud 基礎架構服務來建立和管理 API 與服務。如要使用 Endpoints 來管理 API,請將 API 的 OpenAPI 設定檔部署至 Service Management。
如何部署 Endpoints 設定:
- 在 Cloud Shell 的
endpoints-quickstart目錄中,輸入下列指令:
cd scripts
- 執行範例中包含的以下指令碼:
./deploy_api.sh
Endpoints 使用 OpenAPI 設定檔中的 host 欄位來識別服務。deploy_api.sh 指令碼會將Google Cloud 專案的 ID 設定為 host 欄位中設定的名稱的一部分。當您為自己的服務準備 OpenAPI 設定檔時,必須手動執行此操作。
指令碼接著會使用下列指令將 OpenAPI 設定部署至 Service Management:gcloud endpoints services deploy openapi.yaml
Service Management 在建立和設定服務時,會將資訊輸出至 Google Cloud 控制台。您可以放心忽略 openapi.yaml 檔案中路徑不需要 API 金鑰的警告。成功完成後,您將會看到類似下列包含服務設定 ID 和服務名稱的一行文字:
Service Configuration [2017-02-13-r2] uploaded for service [airports-api.endpoints.example-project.cloud.goog]
啟用必要服務
Endpoints 至少需要啟用下列 Google 服務:
| 名稱 | 標題 |
|---|---|
servicecontrol.googleapis.com |
Service Control API |
servicemanagement.googleapis.com |