gpt4 book ai didi

spring - 从 Spring Boot 连接到 AWS SQS 时出错

转载 作者:行者123 更新时间:2023-12-02 02:34:38 27 4
gpt4 key购买 nike

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

这是我的文件。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'simpleMessageListenerContainer' defined in class path resource [org/springframework/cloud/aws/messaging/config/annotation/SqsConfiguration.class]: Invocation of init method failed; nested exception is 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 文件中给出了正确的访问 key 、 secret 访问 key 和端点。

@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);
}

}

最佳答案

我遇到了与描述相同的问题。我的解决方案要求为 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

关于spring - 从 Spring Boot 连接到 AWS SQS 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43674465/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com