van*_*uez 1 spring-batch spring-boot spring-cloud-aws
我正在开发一个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})
但还是行不通,有什么主意吗?
提前致谢!
需要在资源的application.properties文件中设置2个Properties
cloud.aws.region.static=us-east-2
cloud.aws.stack.auto=false
请在属性文件中指定正确的区域。
这将使用第一个属性为您的应用程序配置区域,第二个属性将停止应用程序的自动配置。
至于Spring Cloud Brixton.SR7的版本,要手动配置您将在application.properties中设置的区域(或使用的是.xml,.yml)。除非您确实需要指定此参数,否则该参数仅对您的开发环境有效。
cloud.aws.region.static=us-east-1
我还必须为开发人员设置下一个属性,但也许您不需要它。
cloud.aws.stack.auto=false
有关更多参考信息,请参见http://cloud.spring.io/spring-cloud-static/spring-cloud-aws/1.1.4.RELEASE/#_region_configuration
| 归档时间: | 
 | 
| 查看次数: | 2746 次 | 
| 最近记录: |