我正在尝试在我的Spring Boot应用程序(Kotlin)中实现SQS侦听器。我正在使用spring-cloud-aws-messagingHere是引导我完成实现的文章。
问题:应用程序无法启动。
日志说:

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-compose.yml
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
docker-entrypoint-initaws.sh
#!/usr/bin/env bash
set -x
awslocal sqs create-queue --queue-name my-queue-name
set +x
InfrastructureConfiguration.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
  }
}
AmazonSqs.kt
val logger = KotlinLogging.logger {}

@Lazy
@Component
class AmazonSqs {
  companion object {
    const val QUEUE_NAME = "my-queue-name"
  }

  @SqsListener(QUEUE_NAME)
  fun receiveMessage(message: String?) {
    logger.info("Received message: {}")
  }
}

最佳答案

问题出在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"

10-07 19:46
查看更多