簡體   English   中英

禁用Spring Cloud 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 Config Server中:

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

您可以使用cloud.aws.region.static任何值,但必須有一個。

我在不同的地方閱讀了這兩種解決方案,並認為它可能有助於將來將這些解決方案結合在一起。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM