Spring Boot、Spring Cloud AWS 和 AWS SQS 未从队列中读取

Posted

技术标签:

【中文标题】Spring Boot、Spring Cloud AWS 和 AWS SQS 未从队列中读取【英文标题】:Spring Boot, Spring Cloud AWS and AWS SQS not reading from the queue 【发布时间】:2016-09-14 19:50:17 【问题描述】:

我正在尝试使用 Spring Boot 和 Spring Cloud AWS SQS 构建一个最小的 gradle java 项目,但我无法从队列中读取它。

这些是我的项目文件:

build.gradle:

apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "spring-boot"
apply plugin: "io.spring.dependency-management"

sourceCompatibility = 1.8
targetCompatibility = 1.8

buildscript 
    repositories 
        mavenCentral()
    
    dependencies 
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.5.RELEASE")
        classpath("io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE")
    


dependencyManagement 
     imports 
          mavenBom("org.springframework.cloud:spring-cloud-aws:1.1.0.RELEASE")
     


repositories 
    mavenCentral()


dependencies 
    compile("org.springframework.boot:spring-boot-starter-actuator:1.3.5.RELEASE")
    compile("org.springframework.cloud:spring-cloud-starter-aws:1.1.0.RELEASE")
    // if I don't add the line below, the annotation @MessageMapping is not found :(
    // I would have expected that cloud-starter-aws would have taken care of it
    compile("org.springframework.cloud:spring-cloud-aws-messaging:1.1.0.RELEASE")
    // this has been added to fix an exception happening, please read below
    compile("org.springframework.data:spring-data-commons:1.12.1.RELEASE")

Application.java:

package com.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application
    
    public static void main(String[] args)
    
        SpringApplication.run(Application.class, args);
    

QueueListener.java:

package com.test.sqs;

import java.util.logging.Logger;
import org.springframework.messaging.handler.annotation.MessageMapping;
import com.test.sqs.model.TestMessage;

public class QueueListener

    @MessageMapping("test_queue")
    private void receiveMessage(TestMessage testMessage)
    
        System.out.println("Test message received: " + testMessage.getMessage());
    

src/main/resources 中的application.yaml:

cloud:
    aws:
        credentials:
            accessKey: **********************
            secretKey: **********************
        region:
            static: us-west-2

应用程序在启动时抛出异常(但你可以在日志调试模式下看到异常!):

org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.amazonaws.auth.profile.ProfilesConfigFile]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: AWS credential profiles file not found in the given path: C:\src\collector\default

但总是在日志中我可以看到它确实从 yaml 文件中获取了我的凭据:

2016-05-19 11:15:14.546 DEBUG 11704 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Searching for key 'cloud.aws.credentials.accessKey' in [systemProperties]
2016-05-19 11:15:14.546 DEBUG 11704 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Searching for key 'cloud.aws.credentials.accessKey' in [systemEnvironment]
2016-05-19 11:15:14.546 DEBUG 11704 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Searching for key 'cloud.aws.credentials.accessKey' in [random]
2016-05-19 11:15:14.546 DEBUG 11704 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Searching for key 'cloud.aws.credentials.accessKey' in [applicationConfigurationProperties]
2016-05-19 11:15:14.546 DEBUG 11704 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Found key 'cloud.aws.credentials.accessKey' in [applicationConfigurationProperties] with type [String] and value '***'
2016-05-19 11:15:14.546 DEBUG 11704 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Searching for key 'cloud.aws.credentials.secretKey' in [systemProperties]
2016-05-19 11:15:14.546 DEBUG 11704 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Searching for key 'cloud.aws.credentials.secretKey' in [systemEnvironment]
2016-05-19 11:15:14.546 DEBUG 11704 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Searching for key 'cloud.aws.credentials.secretKey' in [random]
2016-05-19 11:15:14.546 DEBUG 11704 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Searching for key 'cloud.aws.credentials.secretKey' in [applicationConfigurationProperties]
2016-05-19 11:15:14.546 DEBUG 11704 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Found key 'cloud.aws.credentials.secretKey' in [applicationConfigurationProperties] with type [String] and value '***'

所以我不确定它为什么会寻找其他地方?

它还会抛出此异常(只有在您处于日志记录调试模式时才始终可见):

    java.lang.ClassNotFoundException: org.springframework.data.web.config.EnableSpringDataWebSupport

所以我不得不在 build.gradle 中添加

compile("org.springframework.data:spring-data-commons:1.12.1.RELEASE")

但是现在异常已经不存在了,但是程序开始和结束时什么都不做,并且不再打印日志了!

其他一些事实:

Application.java 在正确的包中让组件扫描工作,所以 Spring 应该看到 QueueListener 类, application.yaml 被正确读取,如果放置了错误的区域,它会抱怨, 如果我输入了错误的凭据(错误的 accessKey 或/和错误的 secretKey),它不会抱怨,所以我认为它根本不会尝试连接到 AWS。

我不确定 build.gradle 的第 34 行是否:

compile("org.springframework.cloud:spring-cloud-starter-aws:1.1.0.RELEASE")
// if I don't add the line below, the annotation @MessageMapping is not found :(
// I would have expected that cloud-starter-aws would have taken care of it
compile("org.springframework.cloud:spring-cloud-aws-messaging:1.1.0.RELEASE")
// this has been added to fix an exception happening, please read below
compile("org.springframework.data:spring-data-commons:1.12.1.RELEASE")

可能是问题的征兆,我希望 cloud-starter-aws 自动加载所有需要的库。

我错过了什么?谢谢!

【问题讨论】:

【参考方案1】:

发现了问题,这当然是愚蠢的。

Spring 没有加载 QueueListener 类,因为它没有 Service / Component 注解,所以:

@Service
public class SqsQueueSender

...

修复了问题。

【讨论】:

以上是关于Spring Boot、Spring Cloud AWS 和 AWS SQS 未从队列中读取的主要内容,如果未能解决你的问题,请参考以下文章

Spring Cloud 对应 Spring Boot 版本

spring-boot 指标与 spring-cloud 指标

Spring Cloud Spring Boot mybatis分布式微服务云架构使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程

Spring Cloud Spring Boot mybatis分布式微服务云架构使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程

Spring Cloud 学习 Spring Boot Admin

Spring Boot与Spring Cloud技术选型版本对应关系