如何从Ubuntu VM对AWS SQS实例运行Spring Boot程序?

时间:2016-04-27 00:35:58

标签: java spring ubuntu amazon-web-services virtual-machine

我有一个Spring Boot应用程序,我试图针对来自Ubuntu VM的SQS队列运行。我已经设置了Application.java来从AmazonSQSClient创建QueueMessagingTemplate,而AmazonSQSClient又是从凭证文件中读取的BasicAWSCredentials。我还在“〜/ .aws / credentials”中使用我的访问键创建了一个凭证文件。但是,Spring Boot失败并显示错误消息:

ERROR 9858 --- [main] o.s.boot.SpringApplication: Application startup failed org.springframework.context.ApplicationContextException: Failed to start bean 'simpleMessageListenerContainer'; nested exception is com.amazonaws.AmazonClientException: Unable to load AWS credentials from any provider in the chain

以下是我的Application.java:

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.sqs.AmazonSQSClient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.aws.autoconfigure.context.ContextStackAutoConfiguration;
import org.springframework.cloud.aws.context.config.annotation.EnableContextCredentials;
import org.springframework.cloud.aws.context.config.annotation.EnableContextRegion;
import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableContextCredentials(accessKey = "${AWS_ACCESS_KEY}", secretKey = "${AWS_SECRET_KEY}")
@EnableContextRegion(region = "${AWS_REGION}")
@EnableAutoConfiguration(exclude= ContextStackAutoConfiguration.class)
@ComponentScan
public class Application {

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);
    }

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setDateFormat(new ISO8601DateFormat());
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        return mapper;
    }

    public BasicAWSCredentials credentialsProvider(String accessKey, String secretKey) {
        BasicAWSCredentials awsCredentialsProvider = new BasicAWSCredentials(accessKey, secretKey);
        return awsCredentialsProvider;
    }

    public AmazonSQSClient sqs(String accessKey, String secretKey){
        return new AmazonSQSClient(credentialsProvider(accessKey, secretKey));
    } 

    @Bean
    public QueueMessagingTemplate queueTemplate(
    @Value("${AWS_ACCESS_KEY}") String accessKey,
            @Value("${AWS_SECRET_KEY}") String secretKey) {
        return new QueueMessagingTemplate(sqs(accessKey, secretKey));
    } 
}

0 个答案:

没有答案