Spring Cloud Stream与RabbitMQ 死信队列

本文介绍了如何使用Spring Cloud Stream与RabbitMQ集成,特别是关于消息的TTL设置和死信队列的配置。通过设置TTL,消息在未被消费超过指定时间后会变成死信。文章详细讲解了两种配置方式:一种是使用默认死信队列,另一种是创建自定义死信队列,并展示了相应的YAML配置、接口定义和消费死信队列的方法。
摘要由CSDN通过智能技术生成

RabbitMQ的TTL全称为Time-To-Live,表示的是消息的有效期。消息如果在队列中一直没有被消费并且存在时间超过了TTL,消息就会变成"死信" (Dead Message),后续无法再被消费。

引入Spring Cloud Stream与RabbitMQ整合的pom依赖,其版本为3.0.7.RELEASE

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
    <version>3.0.7.RELEASE</version>
</dependency>

第一种:延迟队列和默认的死信队列

application.yml配置文件中的配置如下:

spring:
  cloud:
    stream:
      binders:
        local_rabbit:
          type: rabbit
          environment:
            spring:
              rabbitmq:
                host: 192.168.1.105
                port: 5672
                username: admin
                password: admin
      bindings:
        #延迟队列管道
        delay_output_channel:
          destination: delay_exchange
          producer:
            required-groups: delay_queue_group
          binder: local_rabbit
      rabbit:
        bindings:
          delay_output_channel:
            producer:
              #设置消息的有效期,单位是毫秒,如果消息超过了ttl,则该条消息会进入到死信队列
              ttl: 10000
              #启动死信队列
              autoBindDlq: true

对于上述配置中,此处有一个参数ttl需要进行特别说明,该参数生效的前提是spring.cloud.stream.bindings里里面对应的管道已经声明了requiredGroups

定义的延迟队列管道接口类:

import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;

public interface Processor{

    //延迟队列生产者管道
    String MESSAGE_OUTPUT = "delay_output_channel";

    @Output(MESSAG
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值