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 模块的所有可传递依赖项,并且 以及作模块所需的亚马逊 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-context)spring-doc.cadn.net.cn

上面的示例适用于Maven Central存储库。要使用 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>

对于里程碑:spring-doc.cadn.net.cn

<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. 亚马逊 SDK 配置

Spring Cloud AWS 配置目前是使用 Spring Cloud AWS 命名空间提供的自定义元素完成的。 JavaConfig 即将受支持。配置设置直接在 Spring XML 配置文件中完成 以便可以直接使用元素。Spring Cloud AWS 的每个模块都提供自定义命名空间,以允许模块化 模块的使用。下面概述了使用 Spring Cloud AWS 的典型 XML 配置: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 云环境中运行 通过使用EC2MetadataUtils类。从版本 1.11.678 开始,AWS 开发工具包会记录一条警告消息,当在 AWS 环境之外进行此检查时,会出现异常。 可以通过设置ERROR日志记录级别com.amazonaws.util.EC2MetadataUtils类。spring-doc.cadn.net.cn

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

2.3.1. SDK 凭证配置

为了调用 Amazon Web Service,必须为 Amazon 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>

访问密钥和密钥应外部化为属性文件(例如 Spring Boot 应用程序配置) 并且不会签入源管理系统。spring-doc.cadn.net.cn

实例配置文件配置

实例配置文件配置允许分配 在启动 EC2 实例时由角色授权的配置文件。然后,从 EC2 实例发出的所有调用都会被身份验证 使用实例配置文件特定用户角色。因此,配置中不需要专用的访问密钥和密钥。 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>

访问密钥和密钥是使用占位符表达式以及默认值来定义的,以避免引导 如果根本未配置属性,则会出错。spring-doc.cadn.net.cn

参数存储和 Secrets Manager 配置凭据和区域配置

Parameter Store 和 Secrets Manager 配置支持使用引导上下文来配置默认AWSSimpleSystemsManagementclient,它使用com.amazonaws.auth.DefaultAWSCredentialsProviderChaincom.amazonaws.regions.DefaultAwsRegionProviderChain. 如果要覆盖此设置,则需要使用类型为AWSSimpleSystemsManagement配置为使用所选凭据和/或区域提供程序。 因为这个上下文是在创建 Spring Cloud Bootstrap 上下文时创建的,所以你不能简单地覆盖 bean 在常规@Configuration类。spring-doc.cadn.net.cn

2.3.2. 区域配置

亚马逊云科技在不同区域可用。基于 根据自定义要求,用户可以在不同的亚马逊区域托管应用程序。这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>
服务特定区域配置

如果一个应用程序上下文使用来自不同区域的服务,也可以覆盖特定服务的区域。可以全局完成配置,如上所述,并使用区域属性为每个服务进行配置。对于数据库服务,配置可能如下所示(稍后介绍)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 伞式项目之后,Spring Cloud AWS 还提供了专门的 Spring Boot 支持。春云可以使用 Spring Boot 属性配置 AWS,并且还会根据常规设置自动猜测任何合理的配置。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 配置文件定义属性的标准方法。春云AWS 支持使用 Spring Boot 应用程序配置文件配置凭据信息。Spring Cloud AWS 提供以下属性来配置整个应用程序的凭据设置。spring-doc.cadn.net.cn

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

  1. AWSStaticCredentialsProvider如果cloud.aws.credentials.access-key提供spring-doc.cadn.net.cn

  2. EC2ContainerCredentialsProviderWrapper除非cloud.aws.credentials.instance-profile设置为falsespring-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

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

配置区域

与凭据一样,Spring Cloud AWS 模块也支持 Spring 内部区域的配置 启动配置文件。可以自动检测或显式配置区域(例如,在本地测试的情况下 针对 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 链而不是配置自定义区域链spring-doc.cadn.net.cn

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

欧盟-西-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