App Engine 标准上的 Firebase Admin SDK

Firebase Admin SDK on app engine standard

是否可以在 App Engine 标准环境中使用 Firebase?我知道标准环境线程功能非常有限,并且由于 Firebase SDK 运行后台同步线程,因此它可能不兼容。我试了一下,但遇到了以下错误,我似乎无法克服:

java.security.AccessControlException: access denied ("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");
    }
}

是我做错了还是标准环境的限制导致的?我选择了标准环境而不是灵活的,因为还不推荐将灵活的版本用于生产环境。

谢谢,简

编辑: appengine-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

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

我们最近修复了在 App Engine (release notes) 上使用 SDK 时出现的问题,因此请务必使用最新版本。

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