使用@SqsListener时,Spring Boot应用程序启动时失败

问题描述

我正在尝试在我的Spring Boot应用程序(Kotlin)中实现SQS侦听器。我正在使用spring-cloud-aws-messagingHere是一篇引导我完成实施的文章。

问题:应用程序无法启动。

日志说:

<a onclick="changeSrc('1')">Episode 1</a>
<a onclick="changeSrc('2')">Episode 2</a>

恳请帮助:)

docker-compose.yml

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 com.amazonaws.services.sqs.model.AmazonSQSException: null (Service: AmazonSQS; Status Code: 500; Error Code: 500 ; Request ID: null)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1803)
...

docker-entrypoint-initaws.sh

version: "2"

services:
  localstack:
    image: localstack/localstack:0.11.5
    ports:
      - "8085:8080"
      - "4569-4576:4569-4576"
    environment:
      - SERVICES=sqs:4573
      - DOCKER_HOST=unix:///var/run/docker.sock
      - DEFAULT_REGION=eu-west-1
      - DATA_DIR=/tmp/localstack/data
    volumes:
      - ./docker/dev/localstack-init-scripts:/docker-entrypoint-initaws.d

InfrastructureConfiguration.kt

#!/usr/bin/env bash
set -x
awslocal sqs create-queue --queue-name my-queue-name
set +x

AmazonSqs.kt

@EnableSqs
class InfrastructureConfiguration(...) {
  @Primary
  @Bean
  fun amazonSQSAsync(): AmazonSQSAsync {
    val credentials: AWSCredentials = BasicAWSCredentials("accessKey","secretKey")

    return AmazonSQSAsyncClientBuilder
      .standard()
      .withEndpointConfiguration(
        AwsClientBuilder.EndpointConfiguration(
          "http://localstack:4573",Regions.fromName("eu-west-1").toString()
      )
    ).withCredentials(AWSStaticCredentialsProvider(credentials)).build()
  }

  @Primary
  @Bean
  fun simpleMessageListenerContainerFactory(amazonSQSAsync: AmazonSQSAsync): 
  SimpleMessageListenerContainerFactory {
    val factory = SimpleMessageListenerContainerFactory()
    factory.setAmazonSqs(amazonSQSAsync)
    factory.setAutoStartup(false)
    factory.setMaxNumberOfMessages(10)
    factory.setWaitTimeOut(20)
    return factory
  }
}

解决方法

问题出在localstack上。我再次运行docker-compose,发现端口4573已弃用。

localstack_1  | Starting edge router (https port 4566)...
localstack_1  | Starting mock SQS service on http ports 4566 (recommended) and 
4573 (deprecated)...

解决方案是在docker-compose中公开端口4566,并改为使用它。

ports:
  - "8085:8080"
  - "4566-4576:4566-4576"

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...