按照Ktor tutorial将 Ktor Kotlin应用程序部署到AppEngine时,Firestore服务器身份验证不起作用,因此数据没有写入到指定的Firestore数据库中。
在直接在IntelliJ IDE中运行应用程序以及通过gradle appengineRun
命令使用 ktor 的实现运行应用程序时,数据都会按预期的方式写入Firestore。 / p>
针对 staging 和 production 环境,有两组AppEngine / Firebase项目。在使用gradle appengineDeploy
命令进行部署之前,已通过命令gcloud config configurations list.
奇怪的是,使用这些策略部署的一些应用程序确实写入了Firestore,但是在再次部署该应用程序时,Firestore并未显示正在写入的新数据。
我有标准的 ktor 必需文件。我也有一个来自较旧实现的旧 MANIFEST.MF 文件。会造成问题吗?
src / main / resources / application.conf
ktor {
application {
modules = [ Initialization.main ]
}
}
src / main / resources / webapp / WEB-INF /
appengine-web.xml
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<threadsafe>true</threadsafe>
<runtime>java8</runtime>
</appengine-web-app>
web.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<servlet>
<display-name>KtorServlet</display-name>
<servlet-name>KtorServlet</servlet-name>
<servlet-class>io.ktor.server.servlet.ServletApplicationEngine</servlet-class>
<!-- path to application.conf file, required -->
<init-param>
<param-name>io.ktor.config</param-name>
<param-value>application.conf</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>KtorServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
logging.properties
.level = INFO
src / main / META-INF / MANIFEST> MF
Manifest-Version: 1.0
Main-Class: Initialization
对于#1-3 下面概述的身份验证策略,使用Firebase Admin库:compile 'com.google.firebase:firebase-admin:6.5.0'
对于身份验证策略#4 ,使用了Google Cloud Firestore库:compile 'com.google.cloud:google-cloud-firestore:0.58.0-beta'
build.gradle
group 'coinverse'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.2.61'
ext.junitJupiterVersion = '5.0.3'
ext.ktor_version = '0.9.4'
ext.appengine_version = '1.9.60'
ext.appengine_plugin_version = '1.3.4'
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.3'
classpath "com.google.cloud.tools:appengine-gradle-plugin:$appengine_plugin_version"
}
}
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'war'
apply plugin: 'com.google.cloud.tools.appengine'
sourceSets {
main.kotlin.srcDirs = [ 'src/main/kotlin' ]
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
jcenter()
maven { url "https://kotlin.bintray.com/ktor" }
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}")
testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}")
testCompile("org.assertj:assertj-core:3.10.0")
testCompileOnly('org.apiguardian:apiguardian-api:1.0.0')
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.3.0'
compile 'io.reactivex.rxjava2:rxjava:2.2.0'
compile 'com.google.cloud:google-cloud-firestore:0.58.0-beta'
// Or compile 'com.google.cloud:google-cloud-firestore:0.58.0-beta'
compile 'com.google.firebase:firebase-admin:6.5.0'
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile "io.ktor:ktor-server-servlet:$ktor_version"
compile "io.ktor:ktor-html-builder:$ktor_version"
providedCompile "com.google.appengine:appengine:$appengine_version"
}
kotlin.experimental.coroutines = 'enable'
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
task run(dependsOn: appengineRun)
appengine {
deploy {
version = 'price-staging-1021653pm'
stopPreviousVersion = false
}
}
1。 Initialize on Google Cloud Platform
此方法很有希望,因为凭据是自动管理的。
// Use the application default credentials
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(credentials)
.setProjectId(projectId)
.build();
FirebaseApp.initializeApp(options);
Firestore db = FirestoreClient.getFirestore();
2。 Initialize on your own server
我已经在GCP IAM和admin > 服务帐户中确认密钥ID与用于身份验证的Json对象匹配。
我正在另一个部署到AppEngine的Firestore连接的应用程序中成功使用此策略。工作的应用程序以 .Jar 的形式构建,并直接部署到AppEngine中,而无需使用 ktor ,而是采用here中概述的步骤。
// Use a service account
InputStream serviceAccount = new FileInputStream("path/to/serviceAccount.json");
GoogleCredentials credentials = GoogleCredentials.fromStream(serviceAccount);
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(credentials)
.build();
FirebaseApp.initializeApp(options);
Firestore db = FirestoreClient.getFirestore();
在正在工作的 .Jar 内置应用中,我以编程方式传递了 Json 对象,以避免找不到文件问题。我为此 ktor 应用程序尝试了相同的程序实现。它可以与gradle appengineRun
一起使用,但不能在部署时使用。
val credentials = GoogleCredentials.fromStream(Gson().toJson(FirebaseCredentials(
"service_account",
"project-name",
"asdfghjkl",
"keyStringHere",
"firebase-adminsdk-dhr30@project-name.iam.gserviceaccount.com",
"1234567890",
"https://accounts.google.com/o/oauth2/auth",
"https://oauth2.googleapis.com/token",
"https://www.googleapis.com/oauth2/v1/certs",
"https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-dhr30%40project-name-staging.iam.gserviceaccount.com"
)).byteInputStream())
val options = FirebaseOptions.Builder()
.setCredentials(credentials)
.setDatabaseUrl("https://project-name-staging.firebaseio.com")
.build()
FirebaseApp.initializeApp(options)
3。在您自己的服务器上初始化(Firebase控制台设置)
#2 之间的唯一区别是此设置会添加.setDatabaseUrl("https://yourProjectName.firebaseio.com")
。
FirestoreOptions firestoreOptions =
FirestoreOptions.getDefaultInstance().toBuilder()
.setProjectId(projectId)
.build();
Firestore db = firestoreOptions.getService();
对于#1-3 ,Firebase应用立即通过应用的 main()方法进行了初始化。然后,从对象访问 Firestore 对象。
FirebaseClient.Kt
object FirebaseClient {
val firestore: Firestore
init {
firestore = FirestoreClient.getFirestore()
}
}
对于#4 ,Firestore对象是在Kotlin对象的init{...}
中创建的,并作为值存储在该对象中。
FirebaseClient.Kt
object FirebaseClient {
val firestore: Firestore
init {
val firestoreOptions = FirestoreOptions.getDefaultInstance().toBuilder()
.setTimestampsInSnapshotsEnabled(true)
.setProjectId("project-name")
.build()
firestore = firestoreOptions.service
}
}
FirebaseClient.firestore.collection(someCollection).document(someDocument).collection(anotherCollection).add(someObject)
答案 0 :(得分:1)
在将Firebase身份验证用于其他项目之后,我发现这与Firebase身份验证无关,而与应用程序的 main 方法有关。因此,将以上Firebase身份验证的各种实现部署到AppEngine后,即可按预期工作。
我希望应用程序的 main 方法一旦将应用程序部署到AppEngine即可运行,类似于在IntelliJ中运行时调用应用程序的main方法的方式。但是我意识到 main 仅在调用应用程序的托管路由后才被调用。
即:https://[yourProjectName].appspot.com
我创建了一个新的 StackOverflow 帖子,以确定how to run a Ktor app's main method automatically once deployed。