禁用Spring Cloud AWS自动配置以进行本地开发 [英] Disable Spring Cloud AWS autoconfiguration for local development
问题描述
我使用以下Maven依赖关系,该依赖关系会自动配置所有必需的参数,以使我的项目在AWS上工作:
I use the following Maven dependency which autoconfigures all necessary parameters to make my project work on AWS:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-aws</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
不过,根据AWS,我没有任何关键功能,只是在运行时从S3加载一些文件.因此,在本地开发(以及测试)期间,我不需要任何AWS自动配置.
I don't have any critical functionality depending on AWS though, it's just to load a few files from S3 at runtime. So during local development (and also testing), I don't need any AWS autoconfiguration.
我在本地运行时遇到的逻辑错误是:
The logical error I get when running locally is:
...
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]
...
是否有用于测试和本地开发的干净,可行的解决方案?
Is there a clean, working solution for both testing and local development?
推荐答案
我已经使用surefire插件解决了此问题:
I've solved this for tests using the surefire plugin:
<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 Config Server中解决了本地开发问题:
Local development was solved setting the following variables as VM parameters or in the Spring Cloud Config Server:
cloud.aws.region.auto=false
cloud.aws.region.static=us-east-1
您可以为cloud.aws.region.static
使用任何值,但是必须有一个值.
You can use any value for cloud.aws.region.static
, but there has to be one.
我在不同的地方阅读了这两种解决方案,并认为将来将它们结合在一起可能会对某人有所帮助.
I read both solutions in different places and thought it might help someone in the future to see them combined here.
这篇关于禁用Spring Cloud AWS自动配置以进行本地开发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!