使用區域端點設定資料位置

本頁說明如何設定 Firestore (Datastore 模式) 用戶端程式庫,以使用單一或多地區端點。

使用 Datastore 模式的 Firestore 用戶端程式庫時,您可以採用下列任一端點:

  • 全域端點:根據預設,Datastore 模式下的 Firestore 用戶端程式庫會將 API 要求傳送至名為 datastore.googleapis.com 的全域服務端點。全域服務端點會將要求轉送至資料庫。在路由期間,要求可能會通過與資料庫位置不同的位置中的伺服器。

  • 區域端點:區域端點會強制執行限制,確保資料在指定 Google Cloud 區域中傳輸、儲存及處理。 如要確保服務端點會在與資料庫相同的區域中,處理應用程式的 Firestore in Datastore 模式要求,請在用戶端程式庫中指定區域端點

  • 多地區端點:多地區端點會強制執行限制,確保資料在指定的 Google Cloud 多地區傳輸、儲存及處理。 如要確保服務端點在與資料庫相同的多區域中處理應用程式的 Firestore (Datastore 模式) 請求,請在用戶端程式庫中指定多區域端點

設定區域或多區域端點

設定區域或多區域端點的方法相同:初始化用戶端程式庫時,您會提供端點字串。下列範例說明如何使用區域端點 (datastore.us-central1.rep.googleapis.com) 設定端點字串。如要使用多區域端點,請提供與資料庫位置對應的多區域端點字串 (例如 nam5datastore.us.rep.googleapis.com)。

Java

如要瞭解如何安裝及使用 Datastore 模式的用戶端程式庫,請參閱「Datastore 模式用戶端程式庫」。 詳情請參閱 Datastore 模式 Java API 參考文件

如要向 Datastore 模式進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。


import com.google.cloud.datastore.Datastore;
import com.google.cloud.datastore.DatastoreOptions;

public class RegionalEndpoint {

  public Datastore createClient() throws Exception {
    // Instantiates a client
    DatastoreOptions options =
        DatastoreOptions.newBuilder().setHost("https://datastore.us-central1.rep.googleapis.com").build();
    Datastore datastore = options.getService();
    return datastore;
  }
}

Python

如要瞭解如何安裝及使用 Datastore 模式的用戶端程式庫,請參閱「Datastore 模式用戶端程式庫」。 詳情請參閱 Datastore 模式 Python API 參考文件

如要向 Datastore 模式進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。

from google.cloud import datastore
from google.api_core.client_options import ClientOptions

ENDPOINT = "https://datastore.africa-south1.rep.googleapis.com"
client_options = ClientOptions(api_endpoint=ENDPOINT)
client = datastore.Client(client_options=client_options)

query = client.query(kind="Task")
results = list(query.fetch())
for r in results:
    print(r)

單一區域與多區域端點語意

區域端點 (REP):

Datastore 模式的 Firestore 支援單一地區端點,適用於此處列出的單一地區位置:Cloud Firestore (Datastore 模式) 位置

請使用以下格式定義地區端點:

Java

    datastore.REGION_NAME.rep.googleapis.com:443

請確認通訊埠號碼已與端點一併定義。

Python

    datastore.REGION_NAME.rep.googleapis.com

Go

    datastore.REGION_NAME.rep.googleapis.com:443

請確認通訊埠號碼已與端點一併定義。

REGION_NAME 替換為區域主機名稱。

主機名稱範例如下:

  • datastore.us-central1.rep.googleapis.com
  • datastore.europe-west1.rep.googleapis.com

多區域端點 (MREP)

如果是多區域端點,請使用 us 代表位置 nam5nam7,並使用 eu 代表位置 eur3 (請參閱「多區域位置」)。

Java

    datastore.us.rep.googleapis.com:443
    datastore.eu.rep.googleapis.com:443

請確認通訊埠號碼已與端點一併定義。

Python

    datastore.us.rep.googleapis.com
    datastore.eu.rep.googleapis.com

Go

    datastore.us.rep.googleapis.com:443
    datastore.eu.rep.googleapis.com:443

請確認通訊埠號碼已與端點一併定義。

位置端點 (已淘汰)

位置端點現已淘汰。請改用區域或多區域端點。

Firestore (Datastore 模式) 先前支援下列格式的區域端點:

Java

  https://REGION_NAME-datastore.googleapis.com:443

請務必使用完整的 https 網址,並定義端點和通訊埠編號。

Python

  https://REGION_NAME-datastore.googleapis.com

請務必將完整的 https 網址設為位置端點。

Go

  REGION_NAME-datastore.googleapis.com:443

請確認通訊埠號碼已與端點一併定義。

REGION_NAME 替換為區域或多區域主機名稱。

主機名稱範例如下:

  • eur3-datastore.googleapis.com
  • nam5-datastore.googleapis.com
  • europe-west6-datastore.googleapis.com
  • asia-northeast2-datastore.googleapis.com

如需多區域和區域主機名稱的完整清單,請參閱「Firestore (Datastore 模式) 位置」。

限制全域 API 端點用量

如要強制使用區域和多區域端點,請使用 constraints/gcp.restrictEndpointUsage 組織政策限制,封鎖對全域 API 端點的要求。詳情請參閱「限制端點用量」。

後續步驟