【问题标题】:Unable to get data from firestore when deployed spring boot app on app engine在应用程序引擎上部署 Spring Boot 应用程序时无法从 Firestore 获取数据
【发布时间】:2021-02-20 19:53:00
【问题描述】:

我在应用引擎上部署了spring boot服务,显示连接firebase成功,但无法获取任何结果。当我在本地运行它时,一切正常。这就是应用程序连接到 fireStore 的方式。我在 GOOGLE_APPLICATION_CREDENTIALS 环境变量中设置 serviceAccounts.json

FirestoreOptions firestoreOptions =
                    FirestoreOptions.getDefaultInstance().toBuilder()
                        .setProjectId(PROJECT_ID)
                        .setCredentials(GoogleCredentials.getApplicationDefault())
                        .build();

在Service bean中,fireStore实例被初始化为

public static Firestore dbFireStore = FirestoreOptions.getDefaultInstance().getService();

我正在获取这样的数据

CollectionReference collectionReference = dbFireStore.collection("Collection_Name")
.document("Document_Name")
.collection("SubCollection_Name");

ApiFuture<QuerySnapshot> querySnapshot = collectionReference.whereEqualTo("Search_Field", "Search_Value").get();

List<QueryDocumentSnapshot> queryDocumentSnapshot = querySnapshot.get().getDocuments();

此处返回的列表为空,但预期大小为 1,并且在本地工作正常。

【问题讨论】:

  • 您是否仔细检查了 project_ID 是否相同?您可以记录此命令并比较结果吗? ((ServiceAccountCredentials)GoogleCredentials.getApplicationDefault()).getProjectId()
  • 谢谢@guillaumeblaquiere 这工作,实际上我正在尝试连接到其他数据库,我认为连接已经建立,现在我正在使用同一个项目,现在一切正常

标签: spring-boot google-app-engine google-cloud-platform google-cloud-firestore


【解决方案1】:

从您的代码看来,您正在使用默认凭据初始化 Firestore。要了解,App Engine automatically retrieves the service account credentials 可以调用 Google Cloud API。

但是,如果设置了环境变量 GOOGLE_APPLICATION_CREDENTIALS,ADC(应用程序默认凭据)会使用该变量指向的服务帐户文件,如果 App Engine 找不到该服务帐户,则会出现权限问题凭据。您应该在 Google Cloud Console 上检查您的 App Engine 日志记录。

对于您的 App Engine 构建,我建议您删除该环境变量并重试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    相关资源
    最近更新 更多