rabbitmq_02_SpringBoot整合rabbitmq、死信队列

1.准备整合环境

1.一个Consumber,和一个producer
2.pom环境和配置文件
3.docker rabbtMq实例

2.整合

1.pom.xml直接使用是SpringBoot脚手架搭建选择web和rabbitMq模块
2.配置文件

server:
  port: 8082

#配置rabbitmq服务
spring:
  rabbitmq:
    username: admin
    password: admin
    host: 192.168.204.128
    port: 5672
    virtual-host: /
3.producer生产者

1.实例化交换机、实例化队列,绑定关系
fanout模型

@Configuration
public class FanoutRabbitMqConfigurtion {
    //1.声明注册fanout模式的交换机

    @Bean
    public FanoutExchange fanoutExchange() {
        /**
         * @param 1 交换机的名字
         * @param 2 是否持久化
         * @param 3 是否自动删除
         */
        return new FanoutExchange("fanout_order_exchange", true, false);
    }

    //2.声明队列
    @Bean
    public Queue smsQueue() {
        return new Queue("sms.fanout.queue", true);
    }

    @Bean
    public Queue messageQueue() {
        return new Queue("message.fanout.queue", true);
    }

    @Bean
    public Queue emailQueue() {
        return new Queue("email.fanout.queue", true);
    }

    //3.绑定队列和交换机
    @Bean
    public Binding smsBinding() {

        return BindingBuilder.bind(smsQueue()).to(fanoutExchange());
    }

    @Bean
    public Binding messageBinding() {

        return BindingBuilder.bind(messageQueue()).to(fanoutExchange());
    }

    @Bean
    public Binding emailBinding() {

        return BindingBuilder.bind(emailQueue()).to(fanoutExchange());
    }


}

direct模型

@Configuration
public class DirectRabbitMqConfigurtion {
    //1.声明注册fanout模式的交换机

    @Bean
    public DirectExchange directExchange() {
        /**
         * @param 1 交换机的名字
         * @param 2 是否持久化
         * @param 3 是否自动删除
         */
        return new DirectExchange("direct_order_exchange", true, false);
    }

    //2.声明队列
    @Bean
    public Queue directSmsQueue() {
        return new Queue("sms.direct.queue", true);
    }

    @Bean
    public Queue directMessageQueue() {
        return new Queue("message.direct.queue", true);
    }

    @Bean
    public Queue directEmailQueue() {
        return new Queue("email.direct.queue", true);
    }

    //3.绑定队列和交换机
    @Bean
    public Binding directSmsBinding() {

        return BindingBuilder.bind(directSmsQueue()).to(directExchange()).with("sms");
    }

    @Bean
    public Binding directMessageBinding() {

        return BindingBuilder.bind(directMessageQueue()).to(directExchange()).with("message");
    }

    @Bean
    public Binding directEmailBinding() {

        return BindingBuilder.bind(directEmailQueue()).to(directExchange()).with("email");
    }
}

2.以一笔订单为例子 发送信息

@Service
public class OrderService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void makeOrderFanout(String userId, String productId, int num) {
        //1. 生成订单号
        String orderId = UUID.randomUUID().toString();
        //2. 保存订单
        System.out.println("保存订单成功orderId=" + orderId);
        //3.通过rabbitmq 发送消息
        String exchangeName = "fanout_order_exchange";
        String routingKey = "";
        rabbitTemplate.convertAndSend(exchangeName, routingKey,orderId);

    }

    public void makeOrderDirect(String userId, String productId, int num) {
        //1. 生成订单号
        String orderId = UUID.randomUUID().toString();
        //2. 保存订单
        System.out.println("保存订单成功orderId=" + orderId);
        //3.通过rabbitmq 发送消息
        String exchangeName = "direct_order_exchange";
        String routingKey = "sms";
        rabbitTemplate.convertAndSend(exchangeName, routingKey,orderId);
    }
}
4.Consumber消费者监听队列
@RabbitListener(queues = {"sms.fanout.queue"})
@Service
public class FanoutSmsConsumber {
    @RabbitHandler
    public void reviceMessage(String message) {
        System.out.println("sms收到了订单信息---->>>"+message);
    }
}
5.Topic基于注解实现消费者和生产者
//创建交换机,队列并绑定关系
@RabbitListener(bindings = @QueueBinding(
        value = @Queue(value = "sms.topic.queue",durable = "true",autoDelete = "false"),
        exchange = @Exchange(value = "topic_order_exchange",type = ExchangeTypes.TOPIC),
        key = "#.sms.#"
))
@Service
public class TopicSmsConsumber {
    @RabbitHandler
    public void reviceMessage(String message) {
        System.out.println("sms收到了订单信息 Topic ---->>>" + message);
    }
}

6.TTL(Time To Live)队列的生存时间

RabbitMQ支持队列的过期时间, 从消息入队列开始计算, 只要超过了队列的超时时间配置, 那么消息会自动清除。
定义一个Fanout类型TTL的交换机和一个queue

@Configuration
public class TtlRabbitMqConfigurtion {
    //1.声明注册fanout模式的交换机

    @Bean
    public DirectExchange ttlDirectExchange() {
        /**
         * @param 1 交换机的名字
         * @param 2 是否持久化
         * @param 3 是否自动删除
         */
        return new DirectExchange("ttl_direct_exchange", true, false);
    }

    //2.声明队列
    @Bean
    public Queue TtlDirectQueue() {
        HashMap<String, Object> args = new HashMap<>();
        //设置队列的过期时间 5000 毫秒
        args.put("x-message-ttl", 5000);
        return new Queue("ttl.direct.queue", true, false, false, args);
    }


    @Bean
    public Binding ttlDirectBinding() {

        return BindingBuilder.bind(TtlDirectQueue()).to(ttlDirectExchange()).with("ttl");
    }
} 

在这里插入图片描述

7.TTL(Time To Live) 消息的生存时间
 public void makeOrderTtlMessage(String userId, String productId, int num) {
        //1. 生成订单号
        String orderId = UUID.randomUUID().toString();
        //2. 保存订单
        System.out.println("保存订单成功orderId=" + orderId);
        //3.通过rabbitmq 发送消息
        String exchangeName = "ttl_direct_exchange";
        String routingKey = "ttlMessage";

        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                //设置消息的过期时间
                message.getMessageProperties().setExpiration("5000");
                message.getMessageProperties().setContentEncoding("UTF-8");
                return message;
            }
        };
        rabbitTemplate.convertAndSend(exchangeName, routingKey, orderId,messagePostProcessor);
    } 

在这里插入图片描述
过期消息和过期队列的区别
过期消息,超时后消息会自动被删除,而过期队列设置后,消息会被移除到死信队列中。

7.死信队列

DLX,全称为Dead- Letter- Exchange,可以称之为死信交换机,也有人称之为死信邮箱。当消息在一个队列中变成死信( dead message)之后,它能被重新发送到另一个交换机中,这个交換机就是DLX,绑定DLX的队列就称之为死信队列消息变成死信,可能是由于以下的原因:

  1. 消息被拒绝
  2. 消息过期
  3. 队列到达最大长度
    在这里插入图片描述
    声明死信队列
@Configuration
public class DeadRabbitMqConfigurtion {
    //1.声明注册Direct模式的交换机

    @Bean
    public DirectExchange deadDirectExchange() {
        /**
         * @param 1 交换机的名字
         * @param 2 是否持久化
         * @param 3 是否自动删除
         */
        return new DirectExchange("dead_direct_exchange", true, false);
    }

    //2.声明队列
    @Bean
    public Queue deadDirectQueue() {
        return new Queue("dead.direct.queue", true);
    }

    @Bean
    public Binding deadDirectBinding() {

        return BindingBuilder.bind(deadDirectQueue()).to(deadDirectExchange()).with("dead");
    }

}

在Direct中绑定死信队列

@Configuration
public class TtlRabbitMqConfigurtion {
    //1.声明注册Direct模式的交换机

    @Bean
    public DirectExchange ttlDirectExchange() {
        /**
         * @param 1 交换机的名字
         * @param 2 是否持久化
         * @param 3 是否自动删除
         */
        return new DirectExchange("ttl_direct_exchange", true, false);
    }

    //2.声明队列
    @Bean
    public Queue TtlDirectQueue() {
        HashMap<String, Object> args = new HashMap<>();
        //设置队列的最大长度  超过队列最大长度的 消息会被放入 死信队列中
        //args.put("x-max-length", 10);
        //设置队列的过期时间 5000 毫秒
        args.put("x-message-ttl", 5000);
        //配置死信队列的 交换机
        args.put("x-dead-letter-exchange", "dead_direct_exchange");
        //配置 Direct 的routingKey  Fanout类型不需要配置
        args.put("x-dead-letter-routing-key", "dead");
        return new Queue("ttl.direct.queue", true, false, false, args);
    }

    @Bean
    public Binding ttlDirectBinding() {

        return BindingBuilder.bind(TtlDirectQueue()).to(ttlDirectExchange()).with("ttl");
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值