On this page
150. Spring Cloud GCP 核心
每个 Spring Cloud GCP 模块使用GcpProjectIdProvider
和CredentialsProvider
来获取 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
环境变量指定的项目 IDGoogle App Engine 项目 ID
GOOGLE_APPLICATION_CREDENTIALS
环境变量指向的 JSON 凭证文件中指定的项目 IDGoogle 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 支持的每个服务的范围。
Service | Scope |
Spanner | https://www.googleapis.com/auth/spanner.admin, https://www.googleapis.com/auth/spanner.data |
Datastore | https://www.googleapis.com/auth/datastore |
Pub/Sub | https://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 Config | https://www.googleapis.com/auth/cloudruntimeconfig |
Trace (Append) | https://www.googleapis.com/auth/trace.append |
Cloud Platform | https://www.googleapis.com/auth/cloud-platform |
Vision | https://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 Initializr到GCP Support
条目获得。