为本地开发禁用 Spring 云 AWS 自动配置

Disable Spring Cloud AWS autoconfiguration for local development

我使用以下 Maven 依赖项自动配置所有必要的参数以使我的项目在 AWS 上运行:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-aws</artifactId>
    <version>1.2.2.RELEASE</version>
</dependency>

尽管我没有依赖于 AWS 的任何关键功能,它只是在运行时从 S3 加载一些文件。因此在本地开发(以及测试)期间,我不需要任何 AWS 自动配置。

我在本地 运行 时得到的逻辑错误是:

...
Caused by: java.lang.IllegalStateException: There is no EC2 meta data available, because the application is not running in the EC2 environment. Region detection is only possible if the application is running on a EC2 instance
    at org.springframework.util.Assert.state(Assert.java:392) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.cloud.aws.core.region.Ec2MetadataRegionProvider.getRegion(Ec2MetadataRegionProvider.java:39) ~[spring-cloud-aws-core-1.2.2.RELEASE.jar:1.2.2.RELEASE]
    at org.springframework.cloud.aws.core.config.AmazonWebserviceClientFactoryBean.createInstance(AmazonWebserviceClientFactoryBean.java:98) ~[spring-cloud-aws-core-1.2.2.RELEASE.jar:1.2.2.RELEASE]
    at org.springframework.cloud.aws.core.config.AmazonWebserviceClientFactoryBean.createInstance(AmazonWebserviceClientFactoryBean.java:44) ~[spring-cloud-aws-core-1.2.2.RELEASE.jar:1.2.2.RELEASE]
...

是否有适用于测试和本地开发的干净、有效的解决方案?

我已经使用 surefire 插件解决了这个问题:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.21.0</version>
    <configuration>
        <classpathDependencyExcludes>
            <classpathDependencyExcludes>org.springframework.cloud:spring-cloud-aws-autoconfigure</classpathDependencyExcludes>
        </classpathDependencyExcludes>
    </configuration>
</plugin>

本地开发已解决,将以下变量设置为 VM 参数或在 Spring 云配置服务器中:

cloud.aws.region.auto=false
cloud.aws.region.static=us-east-1

您可以为 cloud.aws.region.static 使用任何值,但必须有一个值。

我在不同的地方阅读了这两个解决方案,并认为这可能会帮助将来的人在这里看到它们的结合。