简体   繁体   English

缺少 Stackdriver Spring Boot 有效密钥

[英]Stackdriver Spring Boot valid key missing

I am setting up a spring boot application where the logs are sent to google cloud stackdriver.我正在设置一个 Spring Boot 应用程序,将日志发送到谷歌云堆栈驱动程序。 I am using the logback appender for Stackdriver Logging and the logback file from their documentation https://cloud.google.com/logging/docs/setup/java .我正在使用用于 Stackdriver Logging 的 logback appender 和来自他们的文档https://cloud.google.com/logging/docs/setup/java的 logback 文件。

I added GOOGLE_CLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS in Intellij to the environment variables.我在 Intellij 中将 GOOGLE_CLOUD_PROJECT 和 GOOGLE_APPLICATION_CREDENTIALS 添加到环境变量中。 Further, I set them as environment variables in windows as well.此外,我也将它们设置为 Windows 中的环境变量。 When I start the application I always get: PERMISSION_DENIED: The request is missing a valid API key .当我启动应用程序时,我总是得到: PERMISSION_DENIED: The request is missing a valid API key

I installed the gcloud and there I can use the cred file as a service account with admin permissions to write and read logs.我安装了 gcloud,在那里我可以使用 cred 文件作为具有管理员权限的服务帐户来写入和读取日志。 So the file should work as well as the permissions.所以文件应该和权限一样工作。

I tried to set the key as an environment variable as well but that didn't help either.我也尝试将密钥设置为环境变量,但这也无济于事。 What am I missing that it doesn't work?我错过了什么它不起作用?

I added GOOGLE_CLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS in Intellij to the environment variables.我在 Intellij 中将 GOOGLE_CLOUD_PROJECT 和 GOOGLE_APPLICATION_CREDENTIALS 添加到环境变量中。

Are you sure the environment variables are being picked up from IntelliJ?您确定是从 IntelliJ 获取环境变量吗? Did you cross check if environment variables are set using CLI?您是否交叉检查了是否使用 CLI 设置了环境变量?

If you are using spring-cloud-gcp-starter-logging library for Stackdriver integration, then setting environment variables should work fine.如果您使用 spring-cloud-gcp-starter-logging 库进行 Stackdriver 集成,那么设置环境变量应该可以正常工作。

From docs :文档

Due to the way logging is set up, the GCP project ID and credentials defined in application.properties are ignored.由于日志记录的设置方式,application.properties 中定义的 GCP 项目 ID 和凭据将被忽略。

Instead, you should set the GOOGLE_CLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS environment variables to the project ID and credentials private key location, respectively.相反,您应该将GOOGLE_CLOUD_PROJECTGOOGLE_APPLICATION_CREDENTIALS环境变量分别设置为项目 ID 和凭证私钥位置。 You can do this easily if you're using the Google Cloud SDK, using the gcloud config set project [YOUR_PROJECT_ID] and gcloud auth application-default login commands, respectively.如果您使用 Google Cloud SDK,可以分别使用 gcloud config set project [YOUR_PROJECT_ID] 和 gcloud auth application-default login 命令轻松完成此操作。

To make Spring Boot application work with Stackdriver logging, you can set GOOGLE_CLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS environment variables as:要使 Spring Boot 应用程序使用 Stackdriver 日志记录,您可以将GOOGLE_CLOUD_PROJECTGOOGLE_APPLICATION_CREDENTIALS环境变量设置为:

export GOOGLE_CLOUD_PROJECT=`gcloud config get-value core.project`
export GOOGLE_APPLICATION_CREDENTIALS=`gcloud auth application-default login`

I installed the gcloud and there I can use the cred file as a service account with admin permissions to write and read logs.我安装了 gcloud,在那里我可以使用 cred 文件作为具有管理员权限的服务帐户来写入和读取日志。

You should not use admin permissions.您不应使用管理员权限。 You should use a service account with roles/logging.logWriter and (if needed) roles/logging.viewer roles.您应该使用具有roles/logging.logWriter和(如果需要) roles/logging.viewer角色的服务帐户。

I found the problem.我发现了问题。 The GOOGLE_APPLICATION_CREDENTIALS file needs to be set via the logback.xml with the credentialsFile tag. GOOGLE_APPLICATION_CREDENTIALS 文件需要通过带有credentialsFile标签的logback.xml 进行设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM