应用引擎标准上的Firebase Admin SDK

时间:2017-02-01 17:21:25

标签: google-app-engine firebase firebase-admin

是否可以在应用引擎标准环境中使用Firebase?我知道标准环境线程功能非常有限,因为Firebase SDK运行后台同步线程,它可能不兼容。我试过了,我遇到了以下错误,我似乎无法克服:

  

java.security.AccessControlException:拒绝访问(" java.lang.RuntimePermission"" modifyThreadGroup")

这是servlet代码:

public class GeneratorServlet extends HttpServlet {

    FirebaseDatabase database = null;

    @Override
    public void init(ServletConfig config) {
//        ClassLoader classloader = Thread.currentThread().getContextClassLoader();
//        InputStream serviceAccount = classloader.getResourceAsStream("serviceAccountKey.json");

        FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredential(FirebaseCredentials.applicationDefault())
//                .setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
                .setDatabaseUrl("https://app-name.firebaseio.com/")
                .build();

        FirebaseApp defaultApp = FirebaseApp.initializeApp(options);

        this.database = FirebaseDatabase.getInstance(defaultApp);

    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter out = resp.getWriter();
        out.println(this.database);
        this.loadData();
    }

    private void loadData() {
//        The following line throws the error
        DatabaseReference ref = this.database.getReference("/publiclyReadable");
    }
}

我做错了还是由于标准环境的限制?我之所以选择标准环境而非灵活,因为不建议将灵活版本用于生产用途。

谢谢,Jan

编辑: 应用服务引擎-web.xml中

<?xml version="1.0" encoding="utf-8"?>

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
    <application>doctor-appointment-system</application>
    <version>0</version>
    <threadsafe>true</threadsafe>
    <instance-class>B1</instance-class>
    <manual-scaling>
        <instances>1</instances>
    </manual-scaling>
    <system-properties>
        <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
    </system-properties>
</appengine-web-app>

编辑:

的web.xml

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
    <servlet>
        <servlet-name>generator</servlet-name>
        <servlet-class>com.example.bookingtimes.GeneratorServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>generatorinfo</servlet-name>
        <servlet-class>com.example.bookingtimes.GeneratorInfoServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>generator</servlet-name>
        <url-pattern>/times/generate</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>generatorinfo</servlet-name>
        <url-pattern>/times/change</url-pattern>
    </servlet-mapping>
</web-app>

的build.gradle:

buildscript {    // Configuration for building
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.google.cloud.tools:appengine-gradle-plugin:+'
    }
}

repositories {   // repositories for Jar's you access in your code
    maven {
        url 'https://maven-central.storage.googleapis.com'
    }
    jcenter()
    mavenCentral()
}

apply plugin: 'java'                              // standard Java tasks
apply plugin: 'war'                               // standard Web Archive plugin
apply plugin: 'com.google.cloud.tools.appengine'  // App Engine tasks

dependencies {
    providedCompile group: 'javax.servlet', name: 'servlet-api', version: '2.5'
    compile 'com.google.appengine:appengine:+'
    compile 'com.google.firebase:firebase-admin:4.1.0'
}

appengine {  // App Engine tasks configuration
    run {      // local (dev_appserver) configuration (standard environments only)
        port = 8080                 // default
    }

    deploy {
        stopPreviousVersion = true  // default - stop the current version
        promote = true              // default - & make this the current version
    }
}

group = 'com.example.appengine'
version = '0.1'

sourceCompatibility = 1.7
targetCompatibility = 1.7

2 个答案:

答案 0 :(得分:1)

Firebase数据库客户端可以在App Engine标准环境中使用,只要您将其置于手动缩放模式即可。请参阅documentation on cloud.google.com

我们最近解决了在App Engine(release notes)上使用SDK时遇到的问题,因此请务必使用最新版本。

答案 1 :(得分:-4)

我切换到App Engine Flexible(这很简单),现在一切正常。