部署 Endpoints 設定

設定 .proto 檔案和 gRPC API 設定檔後,請進行部署,讓 Cloud Endpoints 取得管理 API 所需的資訊。如要部署 Endpoints 設定,請使用 gcloud endpoints services deploy 指令。這個指令使用了服務基礎架構,也就是 Google 的基礎服務平台。Endpoints 和其他服務都是使用這個基礎架構來建立和管理 API 及服務。本頁面說明如何將您的設定檔部署到 Endpoints。

必要條件

本頁假設您已經:

準備 Google Cloud CLI 以便部署

您可以使用 gcloud 指令列工具部署設定。如要進一步瞭解指令,請參閱 gcloud 參考資料

準備部署:

  1. 安裝並初始化 gcloud CLI
  2. 更新 gcloud CLI:
    gcloud components update
  3. 確認 gcloud CLI 已獲授權,可存取您的資料和服務:
    gcloud auth login

    系統將開啟新的瀏覽器分頁,並提示您選擇一個帳戶。

  4. 設定預設專案。將 [YOUR-PROJECT-ID] 替換為您的 GCP 專案 ID
    gcloud config set project [YOUR-PROJECT-ID]
  5. 如果您要將 API 後端部署到 Kubernetes 或 Kubernetes Engine,請透過以下指令取得新的使用者憑證,以用於應用程式預設憑證。您需要使用者憑證才能對 kubectl 進行授權。
    gcloud auth application-default login
    系統將開啟新的瀏覽器分頁,並提示您選擇一個帳戶。

部署設定檔

  1. Make sure you are in the directory where the api_descriptor.pb and api_config.yaml files are located.
  2. Confirm that the default project that the gcloud command-line tool is currently using is the Google Cloud project that you want to deploy the Endpoints configuration to. Validate the project ID returned from the following command to make sure that the service doesn't get created in the wrong project.
    gcloud config list project
    

    If you need to change the default project, run the following command:

    gcloud config set project YOUR_PROJECT_ID
    
  3. Deploy the proto descriptor file and the configuration file by using the Google Cloud CLI:
    gcloud endpoints services deploy api_descriptor.pb api_config.yaml
    

    As it is creating and configuring the service, Service Management outputs information to the terminal. When the deployment completes, a message similar to the following is displayed:

    Service Configuration [CONFIG_ID] uploaded for service [bookstore.endpoints.example-project.cloud.goog]

    CONFIG_ID is the unique Endpoints service configuration ID created by the deployment. For example:

    Service Configuration [2017-02-13r0] uploaded for service [bookstore.endpoints.example-project.cloud.goog]
    

    In the previous example, 2017-02-13r0 is the service configuration ID and bookstore.endpoints.example-project.cloud.goog is the service name. The service configuration ID consists of a date stamp followed by a revision number. If you deploy the Endpoints configuration again on the same day, the revision number is incremented in the service configuration ID.

如果服務的設定位於多個 YAML 檔案中,您可以將檔案全部傳送至 deploy 指令。例如,Bookstore 的基本設定在 api_config.yaml 中,但如果要替此服務啟用 HTTP 轉碼,您也可以部署包含其他轉碼功能相關設定的 api_config_http.yaml

gcloud endpoints services deploy api_descriptor.pb api_config.yaml api_config_http.yaml

請注意,如果您的 YAML 檔案中存在衝突值,則最後一個指定檔案中的值會覆寫其他檔案中的值。您可以在設定 gRPC 服務中,進一步瞭解 Endpoints 如何合併多個 YAML 檔案。

如果您收到錯誤訊息,請參閱排解 Endpoints 設定部署問題以瞭解如何排解錯誤。

重新部署

每次您變更 .proto 或服務設定 YAML 檔案中的某些內容後,請再次部署檔案,可擴充服務 Proxy (ESP) 才能取得您 API 服務設定的最新版本。如果您先前部署 ESP 時將 rollout 選項設為 managed,則無須重新啟動或重新部署 ESP。rollout=managed 選項可將 ESP 設為使用最新部署的服務設定。指定此選項時,在您部署新服務設定後 5 分鐘內,ESP 會偵測到變更並自動開始使用新設定。建議您指定此選項而非讓 ESP 使用特定的設定 ID。

完成初始的 Endpoints 設定部署後,您可以將角色授予使用者、服務帳戶或群組,讓他們可以重新部署 Endpoints 設定。詳情請參閱授予及撤銷 API 的存取權

後續步驟