在 cloud.goog 網域上設定 DNS

部署 API 後,您的 API 使用者需要透過網域名稱而非 IP 位址存取該 API。你可以採取下列任一做法:

  • .endpoints.PROJECT_ID.cloud.goog 設為網域名稱 (其中 PROJECT_ID 是您的 Google Cloud專案 ID)。
  • 您也可以註冊自己的網域名稱 (例如 example.com),這牽涉到下列作業:
    • 設定 DNS 名稱伺服器 (或使用 Cloud DNS)。
    • 更新註冊資料庫位址。
    • 建立及維護 DNS 記錄。

    如果您已有 DNS 基礎架構,或者想要註冊自己的網域名稱,請參閱使用您的網域名稱提供 API 一文瞭解詳情。

本頁說明如何設定 Cloud Endpoints API,以便使用 .endpoints.PROJECT_ID.cloud.goog 做為網域名稱。本頁的設定步驟適用於使用在 Compute Engine、Google Kubernetes Engine 或 Kubernetes 上執行之 gRPC 的 API。

.cloud.goog 是由 Google 代管的網域,可供 Google Cloud客戶共用。 Google Cloud 專案的專案 ID 一定不會重複,因此採用 .endpoints.PROJECT_ID.cloud.goog 格式的網域名稱即為您的 API 專屬網域名稱。您可以選擇是否設定 .endpoints.PROJECT_ID.cloud.goog 網域名稱。如有需要,您也可以註冊自己的網域名稱。

必備條件

本頁一開始假設您已建立 Cloud Endpoints API,並已將其部署至 Compute Engine、Google Kubernetes Engine 或 Kubernetes。如果您需要 API 以進行測試,可以使用其中一個教學課程來瞭解如何設定和部署範例 API。

設定 DNS

以下程序說明如何針對以 .endpoints.PROJECT_ID.cloud.goog 做為 Endpoints 服務名稱的 Endpoints API 設定 DNS。為方便起見,此程序會將您的 gRPC 服務設定檔稱為 service.yaml

如何設定 DNS:

  1. 開啟 service.yaml,並新增 endpoints 欄位至檔案,如以下程式碼片段所示:
    type: google.api.Service
    name: API_NAME.endpoints.PROJECT_ID.cloud.goog
    endpoints:
    - name: API_NAME.endpoints.PROJECT_ID.cloud.goog
      target: "IP_ADDRESS"
      

    一般來說,name 欄位與 endpoints.name 欄位是相同的。

  2. API_NAME 替換為您的 API 名稱 (例如 bookstoremy-cool-api)。
  3. 使用 IPv4 位址取代 IP_ADDRESS。IP 位址是一個字串,且必須加上引號。

    舉例來說,如果您將 Endpoints API 服務部署到 Compute Engine 虛擬機器執行個體,則可使用該虛擬機器的外部 IP 位址。或者,如果您是在負載平衡器後面的一組虛擬機器執行個體 (或 GKE pod) 上執行程式碼,則可以使用負載平衡器的 IP 位址。

  4. 使用以下指令將更新後的 gRPC 設定檔部署到 Service Management:
    gcloud endpoints services deploy service.yaml service.pb
    

舉例來說,如果在 service.yaml 檔案中指定以下內容:

type: google.api.Service
name: my-cool-api.endpoints.my-project-id.cloud.goog
endpoints:
- name: my-cool-api.endpoints.my-project-id.cloud.goog
  target: "192.0.2.1"

您使用前述的 gcloud 指令部署 service.yaml 檔案時,Service Management 會建立 DNS A 記錄 my-cool-api.endpoints.my-project-id.cloud.goog,這個記錄會解析為目標 IP 位址 192.0.2.1。您可能需要稍候幾分鐘,等待新的 DNS 設定傳播完畢。

後續步驟