2

我正在开发一个 Spring Batch 应用程序,该应用程序应仅将 aws cloud 用于特定配置文件。目前我有一个使用 aws 的配置文件和另一个不应该使用本地数据库、本地文件等在本地运行应用程序的配置文件。(同时 AWS 配置文件将使用 RDS、S3 等)

对于我使用 AWS 配置文件的配置,我有以下内容:

@Configuration
@Profile("!localDev")

public class FileReaderConfigAWS {

@Value("${cloud.aws.s3.bucket}")
private String amazonS3Bucket;

@Autowired
private ResourceLoader resourceLoader;

private static final Logger logger = LoggerFactory.getLogger(FileReaderConfigAWS.class);


@Bean
@StepScope
public FlatFileItemReader<Object> flatFileReader(@Value("#{jobParameters['inputFile']}") String inputFile,  LineMapper
    lineMapper) {
    FlatFileItemReader<Object> flatFileItemReader = new FlatFileItemReader<>();

    flatFileItemReader.setResource(resourceLoader.getResource("s3://" + this.amazonS3Bucket + "/" + inputFile));

    flatFileItemReader.setLineMapper(lineMapper);

    return flatFileItemReader;
}


@Bean
public AbstractFileValidator inputFileValidator() {
    InputS3Validator inputS3Validator = new InputS3Validator();
    inputS3Validator.setRequiredKeys(new String[]{InputFileSystemValidator.INPUT_FILE});
    return inputS3Validator;
}

}

对于我的 localDev 个人资料,我有以下内容:

@Profile("localDev")
@Configuration
public class FileReaderConfigLocalDev {


@Bean
@StepScope
public FlatFileItemReader<Object> flatFileReader(@Value("#{jobParameters['inputFile']}") String inputFile, LineMapper lineMapper) {
    FlatFileItemReader<Object> flatFileItemReader = new FlatFileItemReader<>();
    flatFileItemReader.setResource(new FileSystemResource(inputFile));

    flatFileItemReader.setLineMapper(lineMapper);

    return flatFileItemReader;
}

@Bean
public AbstractFileValidator inputFileValidator() {
    InputFileSystemValidator inputFileValidator = new InputFileSystemValidator();
    inputFileValidator.setRequiredKeys(new String[]{InputFileSystemValidator.INPUT_FILE});
    return inputFileValidator;
}

}

当我尝试使用localDev配置文件 ( -Dspring.profiles.active=localDev) 运行 Spring Boot Main 类时,我收到以下错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'amazonS3': Invocation of init method failed; nested exception is java.lang.IllegalStateException: There is not 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.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    ... 18 common frames omitted
Caused by: java.lang.IllegalStateException: There is not 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:70) ~[spring-core-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.cloud.aws.core.region.Ec2MetadataRegionProvider.getRegion(Ec2MetadataRegionProvider.java:39) ~[spring-cloud-aws-core-1.2.1.RELEASE.jar:1.2.1.RELEASE]
    at org.springframework.cloud.aws.core.config.AmazonWebserviceClientFactoryBean.createInstance(AmazonWebserviceClientFactoryBean.java:98) ~[spring-cloud-aws-core-1.2.1.RELEASE.jar:1.2.1.RELEASE]
    at org.springframework.cloud.aws.core.config.AmazonWebserviceClientFactoryBean.createInstance(AmazonWebserviceClientFactoryBean.java:44) ~[spring-cloud-aws-core-1.2.1.RELEASE.jar:1.2.1.RELEASE]
    at org.springframework.beans.factory.config.AbstractFactoryBean.afterPropertiesSet(AbstractFactoryBean.java:134) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    ... 25 common frames omitted

当我开始调试时,我发现一旦将 spring-cloud-aws-autoconfigure 添加到 pom.xml 中,似乎就无法在运行时为非 aws 环境禁用 aws 自动配置。
我尝试了以下方法:

@EnableAutoConfiguration(exclude = {ContextCredentialsAutoConfiguration.class, ContextStackAutoConfiguration.class})

但仍然无法正常工作,有什么想法吗?

提前致谢!

4

3 回答 3

10

至于 spring cloud Brixton.SR7 的版本,手动配置您将在 application.properties 中设置的区域(或 .xml、.yml 无论您使用什么)。这个参数应该只适用于你的开发环境,除非你真的需要指定它。

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

我还必须为 dev 设置下一个属性,但也许你不需要它。

cloud.aws.stack.auto=false

http://cloud.spring.io/spring-cloud-static/spring-cloud-aws/1.1.4.RELEASE/#_region_configuration中的更多参考信息

于 2017-06-07T16:11:41.523 回答
6

您需要在资源的 application.properties 文件中设置 2 个属性

cloud.aws.region.static=us-east-2
cloud.aws.stack.auto=false

请在属性文件中指定正确的区域。

这将使用第一个属性为您的应用程序配置区域,第二个属性将停止应用程序的自动配置。

于 2019-01-30T09:12:21.667 回答
2

最后,我决定从我的 pom.xml 中完全删除 spring-cloud-aws-autoconfigure 依赖项。
我在资源下创建了一个 aws-config.xml 文件,其中包含以下信息:

<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
                       http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
                       http://www.springframework.org/schema/cloud/aws/context
                       http://www.springframework.org/schema/cloud/aws/context/spring-cloud-aws-context-1.0.xsd">

    <aws-context:context-credentials>
        <aws-context:instance-profile-credentials/>
        <aws-context:simple-credentials access-key="${cloud.aws.credentials.accessKey:}" secret-key="${cloud.aws.credentials.secretKey:}"/>
    </aws-context:context-credentials>

    <aws-context:context-region region="${cloud.aws.region.static:}"/>
    <aws-context:context-resource-loader/>
</beans>

我在使用 aws 环境的 java 配置文件中引用了上述文件,如下所示:

@Configuration
@Profile("!localDev")

@ImportResource("classpath:/aws-config.xml")
public class FileReaderConfigAWS {

    @Value("${cloud.aws.s3.bucket}")
    private String amazonS3Bucket;

    @Autowired
    private ResourceLoader resourceLoader;
...
于 2017-06-13T20:55:05.667 回答