150. Spring Cloud GCP 核心

每个 Spring Cloud GCP 模块使用GcpProjectIdProviderCredentialsProvider来获取 GCP 项目 ID 和访问凭据。

Spring Cloud GCP 提供了一个 Spring Boot 启动器来自动配置核心组件。

使用 Spring Cloud GCP BOM 进行 Maven 坐标:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-gcp-starter</artifactId>
</dependency>

Gradle coordinates:

dependencies {
    compile group: 'org.springframework.cloud', name: 'spring-cloud-gcp-starter'
}

150.1 项目 ID

GcpProjectIdProvider是返回 GCP 项目 ID 字符串的功能接口。

public interface GcpProjectIdProvider {
	String getProjectId();
}

Spring Cloud GCP 启动器会自动配置GcpProjectIdProvider。如果指定了spring.cloud.gcp.project-id属性,则提供的GcpProjectIdProvider返回该属性值。

spring.cloud.gcp.project-id=my-gcp-project-id

否则,将基于规则的有序列表查找项目 ID:

  • GOOGLE_CLOUD_PROJECT环境变量指定的项目 ID

  • Google App Engine 项目 ID

  • GOOGLE_APPLICATION_CREDENTIALS环境变量指向的 JSON 凭证文件中指定的项目 ID

  • Google Cloud SDK 项目 ID

  • 来自 Google Compute Engine 元数据服务器的 Google Compute Engine 项目 ID

150.2 Credentials

CredentialsProvider是一个功能性接口,可返回凭据以认证和授权对 Google Cloud Client 库的调用。

public interface CredentialsProvider {
  Credentials getCredentials() throws IOException;
}

Spring Cloud GCP 启动程序会自动配置CredentialsProvider。它使用spring.cloud.gcp.credentials.location属性来定位 Google 服务帐户的 OAuth2 私钥。请记住,此属性是 Spring 资源,因此可以从许多different locations获得凭证文件,例如文件系统,Classpath,URL 等。下一个示例指定文件系统中的凭证位置属性。

spring.cloud.gcp.credentials.location=file:/usr/local/key.json

或者,您可以通过直接指定spring.cloud.gcp.credentials.encoded-key属性来设置凭据。该值应该是 JSON 格式的 base64 编码的帐户私钥。

如果未通过属性指定凭据,则启动程序将尝试从地方数量查找凭据:

  • GOOGLE_APPLICATION_CREDENTIALS环境变量指向的凭证文件

  • Google Cloud SDK gcloud auth application-default login命令提供的凭据

  • Google App Engine 内置凭据

  • Google Cloud Shell 内置凭据

  • Google Compute Engine 内置凭据

如果您的应用程序在 Google App Engine 或 Google Compute Engine 上运行,则在大多数情况下,您应省略spring.cloud.gcp.credentials.location属性,而应让 Spring Cloud GCP Starter 获取这些环境的正确凭据。在 App Engine 标准版上,使用应用程序身份服务帐户凭据,在 App Engine Flexible 上,使用灵活的服务帐户凭证,在 Google Compute Engine 上,使用Compute Engine 默认服务帐户

150.2.1 Scopes

默认情况下,Spring Cloud GCP Starter 提供的凭据包含 Spring Cloud GCP 支持的每个服务的范围。

ServiceScope
Spannerhttps://www.googleapis.com/auth/spanner.admin, https://www.googleapis.com/auth/spanner.data
Datastorehttps://www.googleapis.com/auth/datastore
Pub/Subhttps://www.googleapis.com/auth/pubsub
存储(只读)https://www.googleapis.com/auth/devstorage.read_only
Storage (Write/Write)https://www.googleapis.com/auth/devstorage.read_write
Runtime Confighttps://www.googleapis.com/auth/cloudruntimeconfig
Trace (Append)https://www.googleapis.com/auth/trace.append
Cloud Platformhttps://www.googleapis.com/auth/cloud-platform
Visionhttps://www.googleapis.com/auth/cloud-vision

Spring Cloud GCP 启动程序允许您为提供的凭据配置自定义范围列表。为此,请在spring.cloud.gcp.credentials.scopes属性中指定以逗号分隔的Google OAuth2 范围列表。

spring.cloud.gcp.credentials.scopes是用于 Google Cloud Platform 服务的Google OAuth2 范围的逗号分隔列表,由提供的CredentialsProvider支持返回的凭据。

spring.cloud.gcp.credentials.scopes=https://www.googleapis.com/auth/pubsub,https://www.googleapis.com/auth/sqlservice.admin

您还可以使用DEFAULT_SCOPES占位符作为范围来表示 Starters 的默认范围,并附加需要添加的其他范围。

spring.cloud.gcp.credentials.scopes=DEFAULT_SCOPES,https://www.googleapis.com/auth/cloud-vision

150.3 Environment

GcpEnvironmentProvider是由 Spring Cloud GCP 启动程序自动配置的功能接口,它返回GcpEnvironment枚举。提供程序可以通过编程帮助确定在哪个 GCP 环境(App Engine Flexible,App Engine Standard,Kubernetes Engine 或 Compute Engine)中部署应用程序。

public interface GcpEnvironmentProvider {
	GcpEnvironment getCurrentEnvironment();
}

150.4 Spring Initializr

此启动器可从Spring InitializrGCP Support条目获得。