禁用 Spring Cloud AWS 自动配置以进行本地开发

问题描述 投票:0回答:3

我使用以下 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]
...

是否有一个干净、可行的解决方案用于测试和本地开发?

java spring maven spring-cloud
3个回答
25
投票

我已经使用 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
使用任何值,但必须有一个。

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


2
投票

当您在启用了 aws cloud autoconfig 的情况下本地运行应用程序时,会出现此错误

对我来说,以下设置有帮助:

spring:
    autoconfigure:
        exclude:
            - org.springframework.cloud.aws.autoconfigure.context.ContextInstanceDataAutoConfiguration
            - org.springframework.cloud.aws.autoconfigure.context.ContextStackAutoConfiguration
            - org.springframework.cloud.aws.autoconfigure.context.ContextRegionProviderAutoConfiguration

cloud:
    aws:
        region:
            static: ap-south-1
        stack:
            auto: false

您可以在这里了解更多信息:Doc


0
投票

禁用它的另一种解决方案是使用测试容器:

package com.example;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.localstack.LocalStackContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

import static org.testcontainers.containers.localstack.LocalStackContainer.Service.S3;
import static org.testcontainers.containers.localstack.LocalStackContainer.Service.SQS;

@SpringBootTest
@Testcontainers
class ApplicationIT {

    @Container
    static final LocalStackContainer localStack = new LocalStackContainer(DockerImageName.parse("localstack/localstack:3.0.2"))
            .withServices(SQS, S3);

    @DynamicPropertySource
    static void overrideProperties(DynamicPropertyRegistry registry) {
        registry.add("spring.cloud.aws.region.static", localStack::getRegion);
        registry.add("spring.cloud.aws.credentials.access-key", localStack::getAccessKey);
        registry.add("spring.cloud.aws.credentials.secret-key", localStack::getSecretKey);
        registry.add("spring.cloud.aws.sqs.endpoint", () -> localStack.getEndpointOverride(SQS).toString());
        registry.add("spring.cloud.aws.s3.endpoint", () -> localStack.getEndpointOverride(S3).toString());
    }

    @Test
    void contextLoads() {
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.