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

本頁說明如何設定 Cloud Firestore 用戶端程式庫,以使用區域或多區域端點。

使用 Cloud Firestore 用戶端程式庫時,可以採用下列任一端點:

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

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

  • 多地區端點:多地區端點會強制執行限制,確保資料儲存及處理作業在指定的多個地區進行。如要確保服務端點在與資料庫相同的多區域中處理應用程式的 Cloud Firestore 要求,請在用戶端程式庫中指定多區域端點

設定區域或多區域端點

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

Java


import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.FirestoreOptions;


/**
 * Demonstrate how to set a regional endpoint.
 */
public class RegionalEndpointSnippets {

  /**
   * Create a client with a regional endpoint.
   **/
  public Firestore regionalEndpoint(String projectId, String endpoint) throws Exception {
    FirestoreOptions firestoreOptions =
        FirestoreOptions.newBuilder()
            .setProjectId(projectId)
            .setCredentials(GoogleCredentials.getApplicationDefault())
            // set endpoint like firestore.us-central1.rep.googleapis.com:443
            .setHost(endpoint)
            .build();
    Firestore dbWithEndpoint = firestoreOptions.getService();

    return dbWithEndpoint;
  }

}

Python

ENDPOINT = "firestore.africa-south1.rep.googleapis.com"
client_options = ClientOptions(api_endpoint=ENDPOINT)
db = firestore.Client(client_options=client_options)

cities_query = db.collection("cities").limit(2).get()
for r in cities_query:
    print(r)

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

區域端點 (REP):

Cloud Firestore 支援此處列出的區域位置 Cloud Firestore 位置的區域端點。

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

Java

  firestore.REGION_NAME.rep.googleapis.com:443

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

Python

  firestore.REGION_NAME.rep.googleapis.com

Go

  firestore.REGION_NAME.rep.googleapis.com:443

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

REGION_NAME 替換為區域主機名稱。

主機名稱範例如下:

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

多區域端點 (MREP)

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

Java

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

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

Python

  firestore.us.rep.googleapis.com
  firestore.eu.rep.googleapis.com

Go

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

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

限制

位置端點 (已淘汰)

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

Cloud Firestore先前支援的位置端點,格式如下:

Java

  REGION_NAME-firestore.googleapis.com:443

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

Python

  REGION_NAME-firestore.googleapis.com

Go

  REGION_NAME-firestore.googleapis.com:443

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

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

主機名稱範例如下:

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

如需多地區和地區主機名稱的完整清單,請參閱 Cloud Firestore 位置

限制全域 API 端點用量

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