java - 为什么我的 Cloud Run - 到 Firebase 调用需要这么长时间?

标签 java firebase google-cloud-platform google-cloud-firestore

我一直在尝试从 Cloud Run 中运行的 Java 应用程序连接到 Firebase 数据库。我的应用的 Firebase 事务的执行似乎有很多分钟的延迟(即使如此,我也没有得到所需的结果)。

当我的应用程序启动时,将调用以下方法:

    public void init() throws Exception {
        LOGGER.info("FirebaseRepo-init starting...");

        FileInputStream serviceAccount =
                  new FileInputStream("/MyDatabase.json");

        FirebaseOptions options = new FirebaseOptions.Builder()
                  .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                  .setDatabaseUrl("https://mydatabase-222123.firebaseio.com")
                  .build();

        LOGGER.info("FirebaseRepo-init 1");

        fbApp = FirebaseApp.initializeApp(options);

        LOGGER.info("FirebaseRepo-init 2");

        dbRef = FirebaseDatabase.getInstance().getReference();

        LOGGER.info("FirebaseRepo-init 3");

        final DatabaseReference entityRef = dbRef.child("entities");

        LOGGER.info("FirebaseRepo-init 4");

        Entity e = testEntity();

        entityRef.runTransaction(new Transaction.Handler() {

            @Override
            public void onComplete(DatabaseError error, boolean committed, DataSnapshot currentData) {
                LOGGER.info("FirebaseRepo-init tx onComplete; com=" + committed + "; err=" + error);
            }

            @Override
            public Result doTransaction(MutableData data) {
                LOGGER.info("FirebaseRepo-init tx start");
                MutableData childData = data.child(e.getGuid().toString());
                if (childData != null) {
                    LOGGER.info("FirebaseRepo-init tx progress");
                    childData.setValue(e);
                } else {
                    LOGGER.info("FirebaseRepo-init tx null child");
                }
                LOGGER.info("FirebaseRepo-init tx will return");
                return Transaction.success(data);
            }
        });

        LOGGER.info("...FirebaseRepo-init ending");
    }

部署应用后检查日志,我们看到:

2020-03-30T01:51:40.318532ZINFO: FirebaseRepo-init starting...
2020-03-30T01:51:40.749160ZINFO: FirebaseRepo-init 1
2020-03-30T01:51:40.781135ZINFO: FirebaseRepo-init 2
2020-03-30T01:51:40.947959ZINFO: FirebaseRepo-init 3
2020-03-30T01:51:40.949045ZINFO: FirebaseRepo-init 4
2020-03-30T01:51:40.952295ZINFO: ...FirebaseRepo-init ending
2020-03-30T01:54:10.647748ZINFO: FirebaseRepo-init tx start
2020-03-30T01:54:10.945862ZINFO: FirebaseRepo-init tx progress
2020-03-30T01:54:12.546400ZINFO: FirebaseRepo-init tx will return
2020-03-30T01:56:03.046096ZINFO: FirebaseRepo-init tx start
2020-03-30T01:56:03.245789ZINFO: FirebaseRepo-init tx progress
2020-03-30T01:56:03.547215ZINFO: FirebaseRepo-init tx will return
2020-03-30T01:56:10.446070ZINFO: FirebaseRepo-init tx onComplete; com=true; err=null

该事务在应用程序启动后大约两分半钟被调用...然后大约五分钟后再次调用,最后完成。此外,当我转到 Firebase 仪表板时,我在数据库中看不到任何内容;尽管我没有看到任何错误报告,但没有添加任何内容。

对我所犯的 n00b 错误有什么想法吗?

最佳答案

考虑到问题中的评论,Cloud Run 似乎确实按预期工作,但是,Cloud Run 可能不是此特定用例的推荐产品。根据Doug的回答Cloud Run isn't meant to run arbitrary code on deployment. It's meant to run code in response to incoming HTTP requests as your run a web server on port 8080. ,这对于所面临的延迟来说是有意义的。可能值得检查其他产品。

关于java - 为什么我的 Cloud Run - 到 Firebase 调用需要这么长时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60923113/

相关文章:

java - 在派生类中哪里调用基类的实现?

java - Spring Boot 应用程序在启动时终止

java - 从oracle中选择日期之间的数据(日期类型)

android - Android 中基于字段查询 Firebase 并返回父级

google-cloud-platform - 使用java sdks在GCP中创建实例时如何接收公共(public)和私有(private)IP?

java - 数组中的精确正整数

swift - Firestore - 如果文档不存在,GetDocument() 会使应用程序崩溃

linux - google-cloud-sdk 安装在 CentOS/usr/local/bin 中找不到正确的 Python 2.7 版本

ssh - 如何从 Circleci ssh 到 GCP 计算引擎实例?

IOS- 与 Firebase 的通用链接和使用 location.href 的错误?