RabbitMQ五种模式

RabbitMQ五种模式

1. 简单模式
当生产者发送消息到交换机,交换机根据消息属性发送到队列,消费者监听绑定队列实现消息的接收和消费逻辑编写.简单模式下,强调的一个队列queue只被一个消费者监听消费.

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YUu9mlOd-1639466571608)(https://z3.ax1x.com/2021/04/12/cBqJOJ.png)]


[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BQWQMI6U-1639466571609)(https://z3.ax1x.com/2021/04/12/cBqjhV.png)]

    //定义对列名称
    private final String DEMO8_QUEUE = "demo8_queue";
    //定义对列名称
    private final String DEMO9_QUEUE = "demo9_queue";
    @Bean
    public Queue queue8() {
        return new Queue(DEMO8_QUEUE, true);
    }

    @Bean
    public Queue queue9() {
        return new Queue(DEMO9_QUEUE, true);
    }
    /**
     * @Author wzj
     * @Param
     * @param: requestJSON
     * @Description 简单模式/争抢模式
     * @Date 10:55 2021/4/12
     **/
    @RequestMapping(value = "/demo1", method = RequestMethod.POST)
    public void demo1(String requestJSON) {
        Message message = null;
        try {
            message = MessageBuilder.withBody(requestJSON.getBytes("UTF-8")).build();
            //持久化消息
            message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
            rabbitTemplate.convertAndSend("demo8_queue", message);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }
生产者:生成消息,发送到交换机
交换机:根据消息属性,将消息发送给队列
消费者:监听这个队列,发现消息后,获取消息执行消费逻辑

应用场景:发短信、发邮件。

2. 争抢模式

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MJrB4JeS-1639466571610)(https://z3.ax1x.com/2021/04/12/cBL3Nt.png)]


强调的也是后端队列与消费者绑定的结构

cBLY38.png

    //定义对列名称
    private final String DEMO8_QUEUE = "demo8_queue";
    //定义对列名称
    private final String DEMO9_QUEUE = "demo9_queue";
    @Bean
    public Queue queue8() {
        return new Queue(DEMO8_QUEUE, true);
    }

    @Bean
    public Queue queue9() {
        return new Queue(DEMO9_QUEUE, true);
    }
    /**
     * @Author wzj
     * @Param
     * @param: requestJSON
     * @Description 简单模式/争抢模式
     * @Date 10:55 2021/4/12
     **/
    @RequestMapping(value = "/demo1", method = RequestMethod.POST)
    public void demo1(String requestJSON) {
        Message message = null;
        try {
            message = MessageBuilder.withBody(requestJSON.getBytes("UTF-8")).build();
            //持久化消息
            message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
            rabbitTemplate.convertAndSend("demo8_queue", message);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }
生产者:发送消息到交换机
交换机:根据消息属性将消息发送给队列
消费者:多个消费者,同时绑定监听一个队列,之间形成了争抢消息的效果

应用场景:抢红包,资源分配系统

3. 路由模式
前面简单模式和争抢模式,着重强调的是消费端。而路由模式,看到“路由”二字,大家也许会猜到,这个和交换机有关了。
路由模式指交换机通过消息的key将消息发送到指定的队列。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bhZmFT8A-1639466571611)(https://z3.ax1x.com/2021/04/12/cBOAbj.png)]


[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WMOPtgNQ-1639466571611)(https://z3.ax1x.com/2021/04/12/cBO1r4.png)]

    //定义交换机名称
    private final String DIRECT_EXCHANGE_NAME = "directExchange";
    //定义交换机名称
    //配置交换器
    @Bean
    DirectExchange directExchange() {
        return new DirectExchange(DIRECT_EXCHANGE_NAME, true, false);
    }
    //定义对列名称
    private final String DEMO6_QUEUE = "demo6_queue";
    //定义对列名称
    private final String DEMO7_QUEUE = "demo7_queue";
    @Bean
    public Queue queue6() {
        return new Queue(DEMO6_QUEUE, true);
    }

    @Bean
    public Queue queue7() {
        return new Queue(DEMO7_QUEUE, true);
    }
    // 绑定队列到交换器
    @Bean
    Binding bindingDirectExchangeQueue6(Queue queue6, DirectExchange directExchange) {
        return BindingBuilder.bind(queue6).to(directExchange).with(DIRECT_KEY);
    }

    // 绑定队列到交换器
    @Bean
    Binding bindingDirectExchangeQueue7(Queue queue7, DirectExchange directExchange) {
        return BindingBuilder.bind(queue7).to(directExchange).with(DIRECT_KEY);
    }
    /**
     * @Author wzj
     * @Param
     * @param: requestJSON
     * @Description 路由键模式
     * @Date 10:55 2021/4/12
     **/
    @RequestMapping(value = "/demo2", method = RequestMethod.POST)
    public void demo5(String requestJSON) {
        Message message = null;
        try {
            message = MessageBuilder.withBody(requestJSON.getBytes("UTF-8")).build();
            //持久化消息
            message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
            rabbitTemplate.convertAndSend(DIRECT_EXCHANGE_NAME, DIRECT_KEY, message);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

应用场景:
1.将手机号作为路由key ,将其发送到指定队列,由持有该手机号的客户端监听队列中的手机短信。
2.将系统的异常消息发送到指定队列,由负责日志记录的应用记录日志。

4. 发布订阅模式
这里强调的也是交换机,将受到的消息发送给多个队列,而不单单指一个。

cBOBse.png


[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eUpaioNG-1639466571612)(https://z3.ax1x.com/2021/04/12/cBOydA.png)]

    //定义交换机名称
    private final String FANOUT_EXCHANGE_NAME = "fanoutExchange";
    //配置交换器
    @Bean
    FanoutExchange fanoutExchange() {
        return new FanoutExchange(FANOUT_EXCHANGE_NAME, true, false);
    }
    //定义对列名称
    private final String DEMO1_QUEUE = "demo1_queue";
    //定义对列名称
    private final String DEMO2_QUEUE = "demo2_queue";
    //定义对列名称
    private final String DEMO3_QUEUE = "demo3_queue";
    //定义对列名称
    private final String DEMO4_QUEUE = "demo4_queue";
    //定义对列名称
    private final String DEMO5_QUEUE = "demo5_queue";
    //配置对列
    @Bean
    public Queue queue1() {
        return new Queue(DEMO1_QUEUE, true);
    }

    @Bean
    public Queue queue2() {
        return new Queue(DEMO2_QUEUE, true);
    }

    @Bean
    public Queue queue3() {
        return new Queue(DEMO3_QUEUE, true);
    }

    @Bean
    public Queue queue4() {
        return new Queue(DEMO4_QUEUE, true);
    }

    @Bean
    public Queue queue5() {
        return new Queue(DEMO5_QUEUE, true);
    }
    // 绑定队列到交换器
    @Bean
    Binding bindingFanoutExchangeQueue1(Queue queue1, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(queue1).to(fanoutExchange);
    }

    // 绑定队列到交换器
    @Bean
    Binding bindingFanoutExchangeQueue2(Queue queue2, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(queue2).to(fanoutExchange);
    }

    // 绑定队列到交换器
    @Bean
    Binding bindingFanoutExchangeQueue3(Queue queue3, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(queue3).to(fanoutExchange);
    }

    // 绑定队列到交换器
    @Bean
    Binding bindingFanoutExchangeQueue4(Queue queue4, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(queue4).to(fanoutExchange);
    }

    // 绑定队列到交换器
    @Bean
    Binding bindingFanoutExchangeQueue5(Queue queue5, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(queue5).to(fanoutExchange);
    }
    /**
     * @Author wzj
     * @Param
     * @param: requestJSON
     * @Description 订阅模式
     * @Date 10:55 2021/4/12
     **/
    @RequestMapping(value = "/demo3", method = RequestMethod.POST)
    public void demo3(String requestJSON) {
        Message message = null;
        try {
            message = MessageBuilder.withBody(requestJSON.getBytes("UTF-8")).build();
            //持久化消息
            message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
            rabbitTemplate.convertAndSend(FANOUT_EXCHANGE_NAME, null, message);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
生产端:发送消息到交换机
交换机:由于是发布订阅模式,会将这个消息发送同步到后端所有与其绑定的队列
消息端:简单模式 1个队列绑定一个消费者 争抢模式 1个队列绑定多个消费者

应用场景:邮件的群发,广告的群发

5.主题模式
和路由模式很类似,区别在于主题模式的路由规则是通配匹配。指交换机通过消息的key将消息发送到某类队列中(类似于快递分拣)。

cBOvyF.png


cBXUTs.png

    //定义交换机名称
    private final String TOPIC_EXCHANGE_NAME = "topicExchange";
    //配置交换器
    @Bean
    TopicExchange topicExchange() {
        return new TopicExchange(TOPIC_EXCHANGE_NAME, true, false);
    }
    //定义对列名称
    private final String DEMO9_QUEUE = "demo9_queue";
    //定义对列名称
    private final String DEMO10_QUEUE = "demo10_queue";
    //定义对列名称
    private final String DEMO11_QUEUE = "demo11_queue";
    @Bean
    public Queue queue9() {
        return new Queue(DEMO9_QUEUE, true);
    }

    @Bean
    public Queue queue10() {
        return new Queue(DEMO10_QUEUE, true);
    }

    @Bean
    public Queue queue11() {
        return new Queue(DEMO11_QUEUE, true);
    }
    // 绑定队列到交换器
    @Bean
    Binding bindingTopicExchangeQueue9(Queue queue9, TopicExchange topicExchange) {
        return BindingBuilder.bind(queue9).to(topicExchange).with("aaa.aaa.*");
    }

    // 绑定队列到交换器
    @Bean
    Binding bindingTopicExchangeQueue10(Queue queue10, TopicExchange topicExchange) {
        return BindingBuilder.bind(queue10).to(topicExchange).with("aaa.#");
    }

    // 绑定队列到交换器
    @Bean
    Binding bindingTopicExchangeQueue11(Queue queue11, TopicExchange topicExchange) {
        return BindingBuilder.bind(queue11).to(topicExchange).with("aaa");
    }
    /**
     * @Author wzj
     * @Param
     * @param: requestJSON
     * @Description 主题模式
     * @Date 10:55 2021/4/12
     **/
    @RequestMapping(value = "/demo4", method = RequestMethod.POST)
    public void demo4(String requestJSON) {
        Message message = null;
        try {
            message = MessageBuilder.withBody(requestJSON.getBytes("UTF-8")).build();
            //持久化消息
            message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
            rabbitTemplate.convertAndSend(TOPIC_EXCHANGE_NAME, TOPIC_KEY, message);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
交换机绑定队列,不在使用具体的路由key,可以使用符号代替路由key值的规则

应用场景:物流分拣。

总结

五种工作模式中,前两种(简单模式、争抢模式)和消费者相关,后三种(路由模式、发布订阅模式、主题模式)和交换机相关。需要根据不同的场景使用不用的工作模式。
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值