Spring 启动 GCP:"Google Credentials" 将 PubSub 应用程序部署到 App Engine 标准环境时出现异常

Spring Boot GCP: "Google Credentials" Exception When Deploying PubSub Application to App Engine Standard Environment

我正在尝试将 Spring 启动应用程序部署到 GCP 的 App Engine 标准环境。该应用程序能够在 运行 在我的开发机器上本地接收来自 PubSub 的已发布消息。我已将应用程序配置为通过 $GOOGLE_APPLICATON_CREDENTIALS 环境变量使用服务凭据进行身份验证。

但是,当我尝试将此应用程序发布到 App Engine,然后跟踪日志(通过 gcloud app logs tail -s test-app-service)时,我看到以下错误: Factory method 'googleCredentials' threw exception; nested exception is java.io.FileNotFoundException: src/main/resources/app-engine-service-creds.json (No such file or directory)

并且应用程序无法启动。当我 运行 gcloud deploy CLI 命令时都会发生这种情况:

gcloud app deploy build/libs/test-app-*.jar --appyaml=src/main/appengine/app.yaml

以及 Gradle GCP 插件任务:

./gradlew appengineDeploy

当我在 src/main/resources 中包含 JSON 密钥文件并在我的 application.yaml 文件中使用 spring.cloud.gcp.credentials.location 参数引用它时,也会发生此错误。

关于实际 部署 Spring 将应用程序引导到 App Engine 的文档少得惊人,我在这里没有想法。

app.yaml:

runtime: java11
service: "test-app-service"
env_variables:
  SPRING_PROFILES_ACTIVE: "gcp"

如有任何建议,我们将不胜感激。谢谢!

编辑 其他(可能)相关文件: application-gcp.yaml

spring:
  cloud:
    gcp:
      project-id: "my-project"

build.gradle.kts

import com.google.cloud.tools.gradle.appengine.appyaml.AppEngineAppYamlExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath("com.google.cloud.tools:appengine-gradle-plugin:2.2.0")
    }
}

plugins {
    id("org.springframework.boot") version "2.4.1"
    id("io.spring.dependency-management") version "1.0.10.RELEASE"
    id("org.jetbrains.kotlin.plugin.allopen") version "1.4.21"
    kotlin("jvm") version "1.4.21"
    kotlin("plugin.spring") version "1.4.21"
}

group = "com.myGroup"
java.sourceCompatibility = JavaVersion.VERSION_11

if (project.hasProperty("projVersion")) {
    project.version = project.properties["projVersion"]!!
} else {
    project.version = "1.0.0"
//  throw Exception("Project Version must be passed in ex.   ./gradlew clean build -PprojVersion=1.0.0")
}

repositories {
    mavenCentral()
    jcenter()
    maven { url = uri("https://repo.spring.io/milestone") }
}

apply {
    plugin("com.google.cloud.tools.appengine")
}

// exclude the app-engine-service-creds
sourceSets {
    main {
        resources {
            exclude("app-engine-service-creds.json")
        }
    }
}

extra["springCloudGcpVersion"] = "2.0.0-RC2"
extra["springCloudVersion"] = "2020.0.0-M6"

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-webflux")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.springframework.boot:spring-boot-starter-data-redis-reactive")
    implementation("com.google.cloud:spring-cloud-gcp-starter-pubsub")
    implementation("org.springframework.boot:spring-boot-starter-integration")
    implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("io.github.microutils:kotlin-logging:1.12.0")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.amshove.kluent:kluent:1.64")
    testImplementation("io.projectreactor:reactor-test")
    testImplementation("org.springframework.integration:spring-integration-test")
}

dependencyManagement {
    imports {
        mavenBom("com.google.cloud:spring-cloud-gcp-dependencies:${property("springCloudGcpVersion")}")
        mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
    }
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "11"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

configure<AppEngineAppYamlExtension> {
    deploy {
        projectId = "my-project"
        version = "GCLOUD_CONFIG"
        stopPreviousVersion = true // etc
    }
}

事实证明,从 gcloud app logs tail -s {service-name} 命令返回的尾部日志可能有点误导。尽管我正在部署应用程序的新版本,并且所有流量都 100% 定向到这个新实例,但应用程序在收到请求之前不会“启动”。由于我仍然不完全理解的原因,日志拖尾了应用程序的 非常 陈旧版本(显然,我不是唯一对此感到困惑的人:"gcloud app logs tail" shows week old data).

实际上,该应用程序毕竟没有遇到任何凭据问题,而只是 运行 在启动时内存不足,因此,我无法验证消息是否已从 PubSub 成功提取通过我的申请。

我通过将以下参数添加到我的 app.yaml 文件中来修复此问题: instance_class: f4

此问题已自行解决。