简体   繁体   English

Spring Boot默认应用上的Firebase

[英]Firebase on Spring Boot Default App

I am creating a JHipster application (Springboot + Angular) and I need to integrate Firebase. 我正在创建一个JHipster应用程序(Springboot + Angular),并且需要集成Firebase。

I've followed the instructions on firebase documentation and it works perfectly when I start the sever offline. 我遵循了Firebase文档上的说明,当我离线启动服务器时,它可以完美地工作。

But when I deploy the server to AWS using jhipster aws in console, when I try to call a function that uses the firebase SDK it says Trying to login to firebase failed. Reason: FirebaseApp name [DEFAULT] already exists! 但是,当我在控制台中使用jhipster aws将服务器部署到AWS时,当我尝试调用使用jhipster aws SDK的函数时,它说Trying to login to firebase failed. Reason: FirebaseApp name [DEFAULT] already exists! Trying to login to firebase failed. Reason: FirebaseApp name [DEFAULT] already exists!

This behaviour happens only when I deploy the application because if I call it from a localhost it works perfectly. 仅当我部署应用程序时,才会发生此行为,因为如果从本地主机调用它,它将运行完美。

I have this on my pom.xml 我在pom.xml上有这个

<dependency>
        <groupId>com.google.firebase</groupId>
        <artifactId>firebase-admin</artifactId>
        <version>6.3.0</version>
    </dependency>

    <dependency>
        <groupId>com.google.gms</groupId>
        <artifactId>google-services</artifactId>
        <version>3.1.1</version>
    </dependency>

and I'm initializing my app using 我正在使用初始化我的应用程序

try{
        InputStream serviceAccount = new ByteArrayInputStream(getFirebaseJson().getBytes(StandardCharsets.UTF_8));
        //FileInputStream serviceAccount = new FileInputStream("firebaseAuth.json");

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



        FirebaseApp.initializeApp(options);
        FirebaseDatabase.getInstance(FirebaseApp.getInstance()).setPersistenceEnabled(true);

    }catch(Exception e){
        log.debug("Trying to login to firebase failed. Reason: " + e.getMessage());
    }

I fixed it. 我修好了它。

Turns out that the FirebaseApp.initializeApp(options); 原来是FirebaseApp.initializeApp(options); function was executed once on the api when the environment was deployed to the AWS Elastic Beanstalk. 将环境部署到AWS Elastic Beanstalk时,该函数在api上执行一次。

What I did was to create a class called FirebaseUtils which has a initiateFirebase() procedure, which sets the FirebaseApp. 我所做的是创建一个名为FirebaseUtils的类,该类具有initializeFirebase initiateFirebase()过程,该过程设置了FirebaseApp。

Once I call the initiateFirebase() on each call of the API where I need access to firebase, the FirebaseApp is created with a new options configuration which is in another scope and I can access Firebase. 一旦在需要访问Firebase的API的每次调用上调用了initiateFirebase() ,FirebaseApp就会使用新的选项配置创建,该配置位于另一个作用域中,并且我可以访问Firebase。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM