【问题标题】:Unable to consume SQS messages无法使用 SQS 消息
【发布时间】:2023-02-03 01:49:13
【问题描述】:

似乎我的配置不监听 SQS 上的消息。该 bean 被拾取但没有消息被路由。 debug=1 没有显示任何有趣的东西,也没有提到 SqsAutoConfig。尝试使用 cloud.aws.sqs.enabled: true 显式启用 SQS 支持,但无济于事。该应用程序配置为使用 Localstack。

这是我使用的:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.amazonaws</groupId>
                <artifactId>aws-java-sdk-bom</artifactId>
                <version>1.12.396</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>io.awspring.cloud</groupId>
            <artifactId>spring-cloud-aws-starter</artifactId>
            <version>2.4.3</version>
        </dependency>
        <dependency>
            <groupId>io.awspring.cloud</groupId>
            <artifactId>spring-cloud-aws-messaging</artifactId>
            <version>2.4.3</version>
        </dependency>
@Configuration
public class AwsSqsConfig {
    @Bean
    AWSStaticCredentialsProvider awsCredentialsProvider() {
        final var credentials = new BasicAWSCredentials(
            awsProperties.credentials().accessKey(),
            awsProperties.credentials().secretKey());

        return new AWSStaticCredentialsProvider(credentials);
    }

    @Bean
    AwsClientBuilder.EndpointConfiguration endpointConfiguration() {
        return new AwsClientBuilder.EndpointConfiguration(awsProperties.uri(), awsProperties.region().getStatic());
    }

    @Bean
    public AmazonSQSAsync sqsAsync(
        final AWSStaticCredentialsProvider credentialsProvider,
        final AwsClientBuilder.EndpointConfiguration endpointConfiguration) {
        return AmazonSQSAsyncClientBuilder.standard()
            .withEndpointConfiguration(endpointConfiguration)
            .withCredentials(credentialsProvider)
            .build();
    }

    @Bean
    public QueueMessagingTemplate queueMessagingTemplate(final AmazonSQSAsync sqsAsync) {
        return new QueueMessagingTemplate(sqsAsync);
    }
}

@Component
public class EventConsumerDispatcher {

    private final Map<String, EventConsumer> consumersByEvent;

    public EventConsumerDispatcher(final List<EventConsumer> eventConsumers) {
        consumersByEvent = eventConsumers.stream()
            .collect(Collectors.toMap(EventConsumer::supported, Function.identity()));
    }

    @SqsListener(value = "${consumer.queue.name}", deletionPolicy = ON_SUCCESS)
    public void consume(final BusEvent genericEvent) {
        Optional.ofNullable(consumersByEvent.get(genericEvent.getType()))
            .ifPresent(consumer -> consumer.consume(genericEvent));
    }
}

【问题讨论】:

  • 您使用的是哪个 Spring Boot 版本?
  • @TomazFernandes Spring Boot 3.0.2,AWS Starter 2.4.3,AWS SDK 1.12.396

标签: spring-boot amazon-sqs


【解决方案1】:

Spring Cloud AWS 2.x 仅兼容 Spring Boot 2.x,对于 Spring Boot 3.x,Spring Cloud AWS 3.0 是必需的。

请参阅兼容性 here

3.0 RC 版本刚刚发布,欢迎试用并提供反馈!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 2020-02-18
    • 2015-12-04
    • 2019-06-10
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    相关资源
    最近更新 更多