2. 基础设置

在使用 Spring Cloud AWS 模块之前,开发者必须选择所需的依赖项并配置 Spring Cloud AWS 模块。接下来的章节将介绍依赖管理以及 Spring AWS Cloud 项目的基础配置。spring-doc.cadn.net.cn

2.1. Spring Cloud AWS Maven 依赖管理

Spring Cloud AWS 模块依赖项可直接在 Maven 中使用,并通过直接配置特定模块来实现。Spring Cloud AWS 模块包含 Spring 模块所需的所有传递依赖项以及 Amazon SDK,以支持模块的正常运行。通用依赖配置如下:spring-doc.cadn.net.cn

<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-aws-context</artifactId>
    <version>{spring-cloud-version}</version>
  </dependency>
</dependencies>

不同的模块可以通过将模块名称替换为相应的名称来包含(例如,用 spring-cloud-aws-messaging 替代 spring-cloud-aws-contextspring-doc.cadn.net.cn

上面的示例适用于 Maven 中央仓库。若要使用 Spring Maven 仓库(例如用于里程碑版本或开发者快照),您需要在 Maven 配置中指定仓库位置。对于正式发布版:spring-doc.cadn.net.cn

<repositories>
    <repository>
        <id>io.spring.repo.maven.release</id>
        <url>https://repo.spring.io/release/</url>
        <snapshots><enabled>false</enabled></snapshots>
    </repository>
</repositories>
<repositories>
    <repository>
        <id>io.spring.repo.maven.milestone</id>
        <url>https://repo.spring.io/milestone/</url>
        <snapshots><enabled>false</enabled></snapshots>
    </repository>
</repositories>

2.2. Amazon SDK 依赖版本管理

Amazon SDK 的发布频率高于 Spring Cloud AWS。如果您需要使用比 Spring Cloud AWS 配置的版本更新的 AWS SDK 版本,请在依赖管理部分添加 AWS SDK BOM,确保其在任何其他配置 AWS SDK 依赖项的 BOM 依赖项之前声明。spring-doc.cadn.net.cn

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-bom</artifactId>
            <version>${aws-java-sdk.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

2.3. Amazon SDK 配置

Spring Cloud AWS 的配置目前是通过 Spring Cloud AWS 命名空间提供的自定义元素来完成的。JavaConfig 支持即将推出。配置设置直接在 Spring XML 配置文件中完成,以便这些元素可以直接使用。Spring Cloud AWS 的每个模块都提供自定义命名空间,以支持模块化使用这些模块。下面概述了一个典型的 XML 配置示例,用于使用 Spring Cloud AWS:spring-doc.cadn.net.cn

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aws-context="http://www.springframework.org/schema/cloud/aws/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/cloud/aws/context
        http://www.springframework.org/schema/cloud/aws/context/spring-cloud-aws-context.xsd">

           <aws-context:context-region region="..."/>
</beans>

在应用启动时,Spring Cloud AWS 为了内部目的会检查应用是否运行在 AWS 云环境中,该检查通过 AWS SDK 提供的 EC2MetadataUtils 类完成。从版本 1.11.678 开始,当此检查在非 AWS 环境中执行时,AWS SDK 会记录一条包含异常的警告信息。可通过将 ERROR 类的日志级别设置为 com.amazonaws.util.EC2MetadataUtils 来隐藏该警告信息。spring-doc.cadn.net.cn

logging.level.com.amazonaws.util.EC2MetadataUtils=error

2.3.1. SDK 凭据配置

为了调用亚马逊网络服务(Amazon Web Service),必须为亚马逊 SDK 配置凭据。Spring Cloud AWS 提供了支持,可为应用程序上下文配置特定的凭据,这些凭据将用于 Spring Cloud AWS 组件发起的每次服务调用,但参数存储(Parameter Store)和密钥管理器(Secrets Manager)配置除外。因此,整个应用程序上下文必须且只能配置一个凭据。spring-doc.cadn.net.cn

当未定义专用凭证提供者时,所有客户端均使用 com.amazonaws.auth.DefaultAWSCredentialsProviderChain。这将本质上使用以下认证信息spring-doc.cadn.net.cn

根据总体凭据策略,有多种方式可配置凭据。可能的选项在以下各小节中进行了说明。spring-doc.cadn.net.cn

简单凭证配置

Amazon SDK 的凭据包括一个访问密钥(可能被共享)和一个秘密密钥(绝对不可共享)。这两个安全属性均可通过 Spring Cloud AWS 模块为每个 Amazon SDK 服务创建的 XML 命名空间进行配置。整体配置如下spring-doc.cadn.net.cn

<beans ...>
  <aws-context:context-credentials>
   <aws-context:simple-credentials access-key="AKIAIO" secret-key="wJalrXUtnFEMI/K7M" />
  </aws-context:context-credentials>
</beans>

访问密钥(access-key)和秘密密钥(secret-key)应被外部化到属性文件中(例如 Spring Boot 应用配置文件),而不能提交到源代码管理系统中。spring-doc.cadn.net.cn

实例配置文件

实例配置文件(instance profile configuration)允许在启动 EC2 实例时分配一个由角色授权的配置文件。此后,从 EC2 实例发出的所有调用都将通过该实例配置文件特有的用户角色进行身份验证。因此,配置中无需专用的访问密钥(access-key)和秘密密钥(secret-key)。在 Spring Cloud AWS 中,实例配置文件的配置如下:spring-doc.cadn.net.cn

<beans ...>
    <aws-context:context-credentials>
        <aws-context:instance-profile-credentials/>
    </aws-context:context-credentials>
</beans>
混合两种安全配置

在某些情况下,将两种身份验证策略结合使用会很有用,以便应用程序可以使用实例配置文件,并以显式访问密钥和秘密密钥配置作为备用方案。如果应用程序在 EC2(例如在测试服务器上)中进行测试,并且在本地进行测试时,这种组合非常有用。下面的代码片段展示了这两种安全配置的组合方式。spring-doc.cadn.net.cn

<beans ...>
   <aws-context:context-credentials>
       <aws-context:instance-profile-credentials/>
       <aws-context:simple-credentials access-key="${accessKey:}" secret-key="${secretKey:}"/>
   </aws-context:context-credentials>
</beans>

访问密钥(access-key)和秘密密钥(secret-key)通过占位符表达式进行定义,并提供默认值,以避免在未配置属性时出现引导错误。spring-doc.cadn.net.cn

Parameter Store 和 Secrets Manager 配置凭据和区域配置

参数存储和 Secrets Manager 配置支持使用一个引导上下文来配置一个默认的 AWSSimpleSystemsManagement 客户端,该客户端使用 com.amazonaws.auth.DefaultAWSCredentialsProviderChaincom.amazonaws.regions.DefaultAwsRegionProviderChain
如果您希望覆盖此设置,则需要
定义您自己的 Spring Cloud 引导配置类
并添加一个类型为 AWSSimpleSystemsManagement 的 Bean,该 Bean 配置为使用您选择的凭据和/或区域提供程序。
由于此上下文是在您的 Spring Cloud 引导上下文创建时生成的,因此您无法在常规的 @Configuration 类中简单地重写该 Bean。spring-doc.cadn.net.cn

2.3.2. 区域配置

Amazon Web Services 在不同 区域 可用。根据自定义需求,用户可将应用程序部署到不同的 Amazon 区域。spring-cloud-aws-context 模块提供了一种为整个应用上下文定义区域的方式。spring-doc.cadn.net.cn

显式区域配置

该区域可以使用 XML 元素显式配置。这在区域无法自动推导时特别有用,例如应用程序未托管在 EC2 实例上(如本地测试)或需要手动覆盖区域时。spring-doc.cadn.net.cn

<beans ...>
    <aws-context:context-region region="eu-west-1"/>
</beans>

也允许使用表达式或占位符来外部化配置,以确保该区域可以通过属性文件或系统属性进行重新配置。spring-doc.cadn.net.cn

自动区域配置

如果应用上下文在 EC2 实例内部启动,则区域可自动从实例元数据中获取,因此无需静态配置。配置将如下所示:spring-doc.cadn.net.cn

<beans ...>
  <aws-context:context-region auto-detect="true" />
</beans>
服务特定区域配置

区域也可以为特定服务进行重写,如果一个应用上下文从不同区域消费服务。</p><p>配置可以像上面所述那样全局进行,也可以通过 region 属性为每个服务单独配置。</p><p>对于数据库服务(稍后将详细描述),配置可能如下所示:spring-doc.cadn.net.cn

<beans ...>
 <aws-context:context-region region="eu-central-1" />
 <jdbc:data-source ... region="eu-west-1" />
</beans>

虽然理论上可以在单个应用中使用多个区域,但我们强烈建议编写仅部署在单一区域内的应用程序;如果应用同时部署在不同区域,则应将应用进行拆分。spring-doc.cadn.net.cn

2.3.3. Spring Boot 自动配置

在 Spring Cloud umbrella 项目之后,Spring Cloud AWS 也提供了专用的 Spring Boot 支持。Spring Cloud AWS 可以通过 Spring Boot 属性进行配置,并且还会根据整体设置自动推断出任何合理的配置。spring-doc.cadn.net.cn

Maven 依赖

Spring Cloud AWS 提供了一个专用模块,以启用 Spring Boot 支持。该模块必须添加到应用程序的通用 Maven 依赖项中。典型的配置如下所示spring-doc.cadn.net.cn

<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-aws-autoconfigure</artifactId>
    <version>{spring-cloud-version}</version>
  </dependency>
</dependencies>

要启用诸如消息传递和 JDBC 等特定功能,必须添加额外的依赖项。Spring Cloud AWS 仅会配置 Spring Boot 应用程序类路径中可用的类。spring-doc.cadn.net.cn

配置凭据

Spring Boot 提供了使用属性文件或 YAML 配置文件的标准方式来定义属性。 Spring Cloud AW S 提供了支持,可以使用 Spring Boot 应用程序配置文件来配置凭据信息。 Spring Cloud AW S 为整个应用程序提供了以下用于配置凭据设置的属性。spring-doc.cadn.net.cn

除非将cloud.aws.credentials.use-default-aws-credentials-chain设置为true,否则Spring Cloud AWS将配置以下凭据链:spring-doc.cadn.net.cn

  1. AWSStaticCredentialsProvider if cloud.aws.credentials.access-key is providedspring-doc.cadn.net.cn

  2. (代码) 0,除非(代码) 被设置为 (代码) 2spring-doc.cadn.net.cn

  3. ProfileCredentialsProviderspring-doc.cadn.net.cn

属性 例举 描述

cloud.aws.credentials.access-keyspring-doc.cadn.net.cn

AKIAIOSFODNN7EXAMPLEspring-doc.cadn.net.cn

静态提供程序使用的访问密钥spring-doc.cadn.net.cn

cloud.aws.credentials.secret-keyspring-doc.cadn.net.cn

wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEYspring-doc.cadn.net.cn

使用静态提供程序的密钥spring-doc.cadn.net.cn

cloud.aws.credentials.instance-profilespring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

配置实例配置文件凭据提供程序,无需进一步配置spring-doc.cadn.net.cn

cloud.aws.credentials.profile-namespring-doc.cadn.net.cn

默认spring-doc.cadn.net.cn

在指定配置文件中的配置名称spring-doc.cadn.net.cn

cloud.aws.credentials.profile-pathspring-doc.cadn.net.cn

~/.aws/credentialsspring-doc.cadn.net.cn

配置文件的文件路径。默认值为 ~/.aws/credentials,如果未提供值。spring-doc.cadn.net.cn

cloud.aws.credentials.use-default-aws-credentials-chainspring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

使用DefaultAWSCredentialsChain而不是配置自定义凭据链spring-doc.cadn.net.cn

配置区域

像凭据一样,Spring Cloud AWS模块也支持在Spring Boot配置文件中配置区域。可以自动检测区域或显式配置(例如,在针对AWS云的本地测试情况下)。spring-doc.cadn.net.cn

要配置区域的属性如下spring-doc.cadn.net.cn

属性 例举 描述

cloud.aws.region.autospring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

启用基于EC2元数据服务的自动区域检测spring-doc.cadn.net.cn

cloud.aws.region.use-default-aws-region-chainspring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

使用DefaultAWSRegion Chain而不是配置自定义区域链spring-doc.cadn.net.cn

cloud.aws.region.staticspring-doc.cadn.net.cn

eu-west-1spring-doc.cadn.net.cn

配置应用的静态区域。可能的区域(目前)是us-east-1、us-west-1、us-west-2、eu-west-1、eu-central-1、ap-southeast-1、ap-southeast-1、ap-northeast-1、sa-east-1、cn-north-1和任何使用自己的区域元数据配置的自定义区域spring-doc.cadn.net.cn