程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Spring Boot,Spring Cloud AWS和AWS SQS未从队列读取大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Spring Boot,Spring Cloud AWS和AWS SQS未从队列读取?

开发过程中遇到Spring Boot,Spring Cloud AWS和AWS SQS未从队列读取的问题如何解决?下面主要结合日常开发的经验,给出你关于Spring Boot,Spring Cloud AWS和AWS SQS未从队列读取的解决方法建议,希望对你解决Spring Boot,Spring Cloud AWS和AWS SQS未从队列读取有所启发或帮助;

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

Spring没有加载QueueListener类,因为它没有service / Component批注,因此:

@service
public class SqsQueueSender
{
...
}

解决了这个问题。

解决方法

我正在尝试使用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 @@R_450_8798@geMapping is not found :(
    // I would have expected that cloud-starter-aws would have taken care of it
    compile("org.springframework.cloud:spring-cloud-aws-@R_450_8798@ging: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.@R_450_8798@ging.handler.Annotation.@R_450_8798@geMapping;
import com.test.sqs.model.Test@R_450_8798@ge;

public class QueueListener
{
    @@R_450_8798@geMapping("test_queue")
    private void receive@R_450_8798@ge(Test@R_450_8798@ge test@R_450_8798@gE)
    {
        System.out.println("Test @R_450_8798@ge received: " + test@R_450_8798@ge.get@R_450_8798@ge());
    }
}

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 @@R_450_8798@geMapping is not found :(
// I would have expected that cloud-starter-aws would have taken care of it
compile("org.springframework.cloud:spring-cloud-aws-@R_450_8798@ging: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自动加载。

我想念什么?谢谢!

大佬总结

以上是大佬教程为你收集整理的Spring Boot,Spring Cloud AWS和AWS SQS未从队列读取全部内容,希望文章能够帮你解决Spring Boot,Spring Cloud AWS和AWS SQS未从队列读取所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。