【问题标题】:Spring Boot GCP: "Google Credentials" Exception When Deploying PubSub Application to App Engine Standard EnvironmentSpring Boot GCP:将 PubSub 应用程序部署到 App Engine 标准环境时出现“Google 凭据”异常
【发布时间】:2021-06-15 11:01:39
【问题描述】:

我正在尝试将 Spring Boot 应用程序部署到 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 Boot 应用程序实际部署到 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
    }
}

【问题讨论】:

  • 您是否尝试通过评论资源行spring.cloud.gcp.credentials.location进行部署?
  • 我做到了,是的。当我尝试使用指定为属性的凭据位置进行部署并且没有
  • 您是否有最小的代码示例来重现该问题?
  • @guillaumeblaquiere 添加了 application-gcp.yaml 和 build.gradle.kts 文件。还有什么其他的,在实施方面,你会觉得相关吗?感谢您为此提供帮助!
  • 你从不在代码中使用 cred 文件?它在启动时失败,对吗?

标签: spring-boot google-app-engine google-cloud-platform google-cloud-pubsub


【解决方案1】:

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

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

我通过将以下参数添加到我的 app.yaml 字段来解决此问题: instance_class: f4

问题已经自行解决了。

【讨论】:

    猜你喜欢
    • 2017-05-27
    • 2020-01-13
    • 2018-06-12
    • 2015-10-27
    • 2019-05-18
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多