You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为所有AWS SQS队列统一设置相同的TTL队列属性

你之前直接使用cfg.QueueAttributes.Add配置不生效的原因是:该属性仅作用于总线默认队列,不会自动同步到所有通过ConfigureEndpoints自动生成的消费者普通队列。要全局为所有SQS普通队列统一设置属性,需要在调用ConfigureEndpoints时传入全局端点配置委托,修改后的配置如下:

services.AddMassTransit(x =>
{
    x.AddAmazonSqsMessageScheduler();
    x.AddConsumers(assembliesWithConsumers.ToArray());
    x.UsingAmazonSqs((context, cfg) =>
    {
        cfg.UseAmazonSqsMessageScheduler();

        cfg.Host("aws", h =>
        {
            if (!String.IsNullOrWhiteSpace(mtSettings.AccessKey))
            {
                h.AccessKey(mtSettings.AccessKey);
            }

            if (!String.IsNullOrWhiteSpace(mtSettings.SecretKey))
            {
                h.SecretKey(mtSettings.SecretKey);
            }

            h.Scope($"{mtSettings.Prefix}-{mtSettings.Environment}", true);
            var sqsConfig = !String.IsNullOrWhiteSpace(mtSettings.SqsServiceUrl)
                ? new AmazonSQSConfig() { ServiceURL = mtSettings.SqsServiceUrl }
                : new AmazonSQSConfig()
                    { RegionEndpoint = RegionEndpoint.GetBySystemName(mtSettings.Region) };

            h.Config(sqsConfig);

            var snsConfig = !String.IsNullOrWhiteSpace(mtSettings.SnsServiceUrl)
                ? new AmazonSimpleNotificationServiceConfig()
                    { ServiceURL = mtSettings.SnsServiceUrl }
                : new AmazonSimpleNotificationServiceConfig()
                    { RegionEndpoint = RegionEndpoint.GetBySystemName(mtSettings.Region) };

            h.Config(snsConfig);
        });

        cfg.SendTopology.ConfigureErrorSettings = settings => settings.QueueAttributes.Add(QueueAttributeName.MessageRetentionPeriod, 1209600);
        cfg.SendTopology.ConfigureDeadLetterSettings = settings => settings.QueueAttributes.Add(QueueAttributeName.MessageRetentionPeriod, 1209600);

        // 新增全局端点配置,为所有普通接收队列设置属性
        cfg.ConfigureEndpoints(context, new BusEnvironmentNameFormatter(mtSettings.Environment), (context, endpointCfg) =>
        {
            if (endpointCfg is IAmazonSqsReceiveEndpointConfigurator sqsEndpointCfg)
            {
                sqsEndpointCfg.QueueAttributes.Add(QueueAttributeName.MessageRetentionPeriod, 1209600);
            }
        });
    });
});

该委托会在每个消费者队列初始化时执行,统一为所有普通接收队列添加消息保留周期属性。修改完成后删除原有旧队列,重启服务重新生成队列即可生效。

内容的提问来源于stack exchange,提问作者Tanuki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 08:57:02