Spring Cloud Stream与RabbitMQ 死信队列

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

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


    org.springframework.cloud
    spring-cloud-starter-stream-rabbit
    3.0.7.RELEASE

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

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

你可能感兴趣的:(RabbitMQ)