从春季启动连接到AWS SQS时出错

时间:2017-04-28 07:28:48

标签: spring amazon-web-services spring-boot spring-cloud amazon-sqs

我正在尝试使用Spring cloud AWS将AWS SQS集成到我的springboot应用程序中,但是不断收到此错误(在下面发布),有人可以提供帮助吗?

这是我的档案。

  

org.springframework.beans.factory.BeanCreationException:错误   创建名称为' simpleMessageListenerContainer'定义于   类路径资源   [组织/ springframework的/云/ AWS /消息/配置/注解/ SqsConfiguration.class]:   调用init方法失败;嵌套异常是   java.lang.NoSuchMethodError:   com.amazonaws.http.ExecutionContext.setCredentials(LCOM / amazonaws / AUTH / AWSCredentials;)V

@Configuration
public class AWSConfig {
    @Value("${amazon.dynamodb.endpoint}")
    private String amazonDynamoDBEndpoint;

    @Value("${amazon.aws.accesskey}")
    private String amazonAWSAccessKey;

    @Value("${amazon.aws.secretkey}")
    private String amazonAWSSecretKey;

    @Value("${amazon.sqs.endpoint}")
    private String amazonSqsEndpoint;
    @Bean
    @Primary
    public AmazonSQSAsyncClient amazonSQSAsyncClient() {

        AmazonSQSAsyncClient amazonSQSAsyncClient = new AmazonSQSAsyncClient(amazonAWSCredentials());
        if (!StringUtils.isEmpty(amazonSqsEndpoint)) {
            amazonSQSAsyncClient.setEndpoint(amazonSqsEndpoint);
        }

        return amazonSQSAsyncClient;

    }

    @Bean
    public AWSCredentials amazonAWSCredentials() {
        return new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey);
    }
}

我可以使用dynamodb但无法连接到SQS。 我在application.properties文件中给出了正确的访问密钥,秘密访问密钥和结束点。

@Component
    @EnableSqs
    public class SQSDao {
        private static final Logger logger = LoggerFactory.getLogger(SQSDao.class);

        private  QueueMessagingTemplate queueMessagingTemplate;

        @Autowired
        public SQSDao(AmazonSQSAsync amazonSqs) {
            this.queueMessagingTemplate = new QueueMessagingTemplate(amazonSqs);
        }

        public void send(String message) {
            System.out.println(queueMessagingTemplate.getDefaultDestination());
            queueMessagingTemplate.convertAndSend("test-queue",  MessageBuilder.withPayload(message).build());
        }

        @SqsListener(value = "test-queue", deletionPolicy = SqsMessageDeletionPolicy.NEVER)
        public void receive(String message)
        {
            System.out.println("message: " + message);
        }

    }

4 个答案:

答案 0 :(得分:0)

修正了它,显然问题与依赖混淆有关,如here

所述

答案 1 :(得分:0)

我能够通过在本地测试期间向我的application.yml添加最后一行来解决此问题

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

答案 2 :(得分:0)

我通过删除重复的依赖项来解决它:

  • aws-xray-recorder-sdk-aws-sdk
  • aws-xray-recorder-sdk-aws-sdk-v2

  • aws-xray-recorder-sdk-aws-sdk-instrumentor
  • aws-xray-recorder-sdk-aws-sdk-v2-仪器

之前:

<!-- AWS X-Ray -->
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-xray-recorder-sdk-core</artifactId>
</dependency>
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-xray-recorder-sdk-apache-http</artifactId>
</dependency>
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-xray-recorder-sdk-aws-sdk</artifactId>
</dependency>
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-xray-recorder-sdk-aws-sdk-v2</artifactId>
</dependency>
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-xray-recorder-sdk-aws-sdk-instrumentor</artifactId>
</dependency>
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-xray-recorder-sdk-aws-sdk-v2-instrumentor</artifactId>
</dependency>
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-xray-recorder-sdk-sql-postgres</artifactId>
</dependency>
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-xray-recorder-sdk-spring</artifactId>
</dependency>

之后:

<!-- AWS X-Ray -->
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-xray-recorder-sdk-core</artifactId>
</dependency>
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-xray-recorder-sdk-apache-http</artifactId>
</dependency>
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-xray-recorder-sdk-aws-sdk-v2</artifactId>
</dependency>
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-xray-recorder-sdk-aws-sdk-v2-instrumentor</artifactId>
</dependency>
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-xray-recorder-sdk-sql-postgres</artifactId>
</dependency>
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-xray-recorder-sdk-spring</artifactId>
</dependency>

答案 3 :(得分:0)

我遇到了与上述相同的问题。我的解决方案要求为Config类实现一些额外的方法:

imports [...]

@Configuration
@RefreshScope
public class SpringCloudSQSConfig {

@Value("${cloud.aws.credentials.accessKeyId:default}")
private String accessKeyId;

@Value("${cloud.aws.credentials.secretKey:default}")
private String secretKey;

@Value("${cloud.aws.region.static:default}")
private String region;

private Logger logger = LoggerFactory.getLogger(this.getClass());

@Bean
public QueueMessagingTemplate queueMessagingTemplate() {
    return new QueueMessagingTemplate(amazonSQSAsync());
}

public AmazonSQSAsync amazonSQSAsync() {
    return AmazonSQSAsyncClientBuilder.standard().withRegion(Regions.US_EAST_2)
            .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKeyId, secretKey)))
            .build();
}

@Bean
public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory() {
    SimpleMessageListenerContainerFactory msgListenerContainerFactory = new SimpleMessageListenerContainerFactory();
    msgListenerContainerFactory.setAmazonSqs(amazonSQSAsync());
    return msgListenerContainerFactory;
}

@Bean
public QueueMessageHandler queueMessageHandler() {
    QueueMessageHandlerFactory queueMsgHandlerFactory = new QueueMessageHandlerFactory();
    queueMsgHandlerFactory.setAmazonSqs(amazonSQSAsync());
    QueueMessageHandler queueMessageHandler = queueMsgHandlerFactory.createQueueMessageHandler();
    List<HandlerMethodArgumentResolver> list = new ArrayList<>();
    HandlerMethodArgumentResolver resolver = new PayloadArgumentResolver(new MappingJackson2MessageConverter());
    list.add(resolver);
    queueMessageHandler.setArgumentResolvers(list);
    return queueMessageHandler;
}
}

对于实现的依赖项:

        <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-aws</artifactId>
        <version>2.2.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-aws-messaging</artifactId>
        <version>2.2.1.RELEASE</version>
    </dependency>

并在属性文件中添加下一个属性:

spring.main.allow-bean-definition-overriding=true